An error occurred while loading the file. Please try again.
-
Grand Francois authored6794d34e
/**
* itérateur sur un tableau dans le sens inverse (depuis la fin)
*
* utilisation :
let arr = [1,2,3];
const it = new ArrayReverseIterator<Result>(arr);
for (let r of it)
console.log( r ); // 3 2 1
*/
export class ArrayReverseIterator<T> implements IterableIterator<T> {
private _index: number;
constructor(private _arr: any[]) {
this._index = this._arr == undefined ? 0 : this._arr.length - 1;
}
public next(): IteratorResult<T> {
if (this._arr != undefined && this._index >= 0) {
return {
done: false,
value: this._arr[this._index--]
}
} else {
return {
done: true,
value: null
}
}
}
[Symbol.iterator](): IterableIterator<T> {
return this;
}
}
/**
* itérateur sur un map string<->any
utilisation :
class MaClasseAIterer implements Iterable<Machin> {
private _machinMap: { [key: string]: Machin } = {};
[Symbol.iterator](): Iterator<Machin> {
return new MapIterator(this._machinMap);
}
}
const mc = new MaClasseAIterer();
for ( const m of mc ) {
...
}
*/
export class MapIterator<T> implements Iterator<T> {
private _map: { [key: string]: T } = {};
private _keys: string[];
private _lastIndex: number = -1; // index of last element returned; -1 if no such
private _index: number = 0;
private _currentKey: string;
constructor(m: { [key: string]: T }) {
this._map = m;
if (this._map != undefined)
this._keys = Object.keys(this._map);
else
this._keys = [];
7172737475767778798081828384858687888990919293949596979899100101
}
public next(): IteratorResult<T> {
const i = this._index;
if (this._index < this._keys.length) {
this._currentKey = this._keys[this._index++];
this._index = i + 1;
this._lastIndex = i;
return {
done: this._index >= this._keys.length,
value: this._map[this._currentKey]
}
} else {
this._currentKey = undefined;
this._lastIndex = undefined;
return {
done: true,
value: undefined
}
}
}
public get index() {
return this._lastIndex;
}
public get key(): string {
return this._currentKey;
}
}