An error occurred while loading the file. Please try again.
-
Grand Francois authoredc6b23ef2
import { ParamValues } from "./param-values";
import { ParamValueMode } from "./param-value-mode";
/**
* interface implémentée par les objets pouvant renvoyer un itérateur sur une série de valeurs numériques
*/
export interface IterableValues {
readonly valuesIterator: IterableIterator<number>;
}
/**
* itérateur sur les (ou la) valeurs prises par le paramètre
*/
export class ParamValueIterator implements IterableIterator<number> {
/**
* paramètre à itérer
*/
private _param: ParamValues;
/**
* true si les valeurs sont fournies de max à min (ParamValueMode.MINMAX)
*/
private _reverse: boolean;
private _index: number;
/**
* cas de figure :
* 0 : valeur fixée
* 1 : min/max/pas
* 2 : liste de valeurs
*/
private _config: number;
constructor(prm: ParamValues, reverse: boolean = false) {
prm.check();
this._param = prm;
this.reset(reverse)
}
public reset(reverse: boolean) {
this._reverse = reverse;
switch (this._param.valueMode) {
case ParamValueMode.SINGLE:
this._config = 0;
this._index = 0;
break;
case ParamValueMode.MINMAX:
this._config = 1;
if (reverse)
this._index = this._param.max;
else
this._index = this._param.min;
break;
case ParamValueMode.LISTE:
this._config = 2;
this._index = 0;
break;
default:
throw new Error(`ParamValueIterator : mode de génération de valeurs ${ParamValueMode[this._param.valueMode]} incorrect`);
}
}
public get hasNext(): boolean {
switch (this._config) {
// valeur fixée
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
case 0:
return this._index == 0;
// min/max
case 1:
const end = this._reverse ? this._index < this._param.min : this._index > this._param.max;
return !end;
// liste
case 2:
return this._index < this._param.valueList.length;
default:
throw new Error(`ParamValueIterator.hasNext() : erreur interne`);
}
}
public next(): IteratorResult<number> {
switch (this._config) {
// valeur fixée
case 0:
if (this.hasNext) {
this._index++;
return {
done: false,
value: this._param.singleValue
};
}
else
return {
done: true,
value: undefined
};
// min/max
case 1:
const res = this._index;
if (this.hasNext) {
if (this._reverse)
this._index -= this._param.step;
else
this._index += this._param.step;
return {
done: false,
value: res
};
} else {
return {
done: true,
value: undefined
};
}
// liste
case 2:
const i = this._index;
if (this.hasNext) {
const res = this._param.valueList[this._index++];
return {
done: false,
value: res
};
} else {
return {
done: true,
value: undefined
};
}
default:
141142143144145146147148149150151152153154155156157
throw new Error(`ParamValueIterator.next() : erreur interne`);
}
}
// public get current(): number {
// if (this._config == 1)
// return this._index;
// throw new Error(`appel ParamValueIterator.current() invalide`)
// }
// interface IterableIterator
public [Symbol.iterator](): IterableIterator<number> {
return this;
}
}