diff --git a/spec/iterator.spec.ts b/spec/iterator/array_reverse_iterator.spec.ts similarity index 85% rename from spec/iterator.spec.ts rename to spec/iterator/array_reverse_iterator.spec.ts index 68dce8db1c895b1e2a8980479b3b0c9f04edd25e..a783167af6f90dbc58c709cd768c609284c33172 100644 --- a/spec/iterator.spec.ts +++ b/spec/iterator/array_reverse_iterator.spec.ts @@ -1,8 +1,8 @@ -/// <reference path="../node_modules/@types/jasmine/index.d.ts" /> +/// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> -import { ArrayReverseIterator } from "../src/util/iterator"; +import { ArrayReverseIterator } from "../../src/util/iterator"; -describe('iterator : ', () => { +describe('array reverse iterator : ', () => { it("reverse ( undefined )", () => { const arr: Object[] = undefined; const it = new ArrayReverseIterator<Object>(arr); diff --git a/spec/iterator/map_iterator.spec.ts b/spec/iterator/map_iterator.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..7bbcef4ee77bb2ec85473e1c478771e6d107c17c --- /dev/null +++ b/spec/iterator/map_iterator.spec.ts @@ -0,0 +1,49 @@ +/// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> + +import { MapIterator } from "../../src/util/iterator"; + +describe('param map iterator : ', () => { + it("sur undefined", () => { + const it: MapIterator<any> = new MapIterator(undefined); + const v1 = it.next(); + expect(v1.done).toBeTruthy(); + }); + + it("sur {}", () => { + const it: MapIterator<any> = new MapIterator({}); + const v1 = it.next(); + expect(v1.done).toBeTruthy(); + expect(it.index).toEqual(0); + }); + + it("sur {'a':1}", () => { + const o = { "a": 1 }; + const it: MapIterator<number> = new MapIterator(o); + + const v1 = it.next(); + expect(v1.done).toBeTruthy(); + expect(v1.value).toEqual(1); + expect(it.key).toEqual("a"); + + const v2 = it.next(); + expect(v2.done).toBeTruthy(); + }); + + it("sur {'a':1, 'b':2}", () => { + const o = { "a": 1, "b": 2 }; + const it: MapIterator<number> = new MapIterator(o); + + const v1 = it.next(); + expect(v1.done).toBeFalsy(); + expect(v1.value).toEqual(1); + expect(it.key).toEqual("a"); + + const v2 = it.next(); + expect(v2.done).toBeTruthy(); + expect(v2.value).toEqual(2); + expect(it.key).toEqual("b"); + + const v3 = it.next(); + expect(v3.done).toBeTruthy(); + }); +}); diff --git a/spec/iterator/param_equation.spec.ts b/spec/iterator/param_equation.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e4fb7ed33595f67cd31646e31338bf9ed5f495b --- /dev/null +++ b/spec/iterator/param_equation.spec.ts @@ -0,0 +1,34 @@ +/// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> + +//import { MapIterator } from "../../src/util/iterator"; +import { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri"; +import { ParamDefinition } from "../../src/index"; + +describe("iterator ConduiteDistribParams : ", () => { + it("test 1", () => { + const cdp: ConduiteDistribParams = new ConduiteDistribParams(1, 2, 3, 4, 5) + + let n = 0; + for (let p of cdp) { + n++; + expect(p.v == n); + } + expect(n === 5); + }); + + it("test 2", () => { + const cdp: ConduiteDistribParams = new ConduiteDistribParams(1, 2, 3, 4, 5) + + const symbs = ["Q", "D", "J", "Lg", "Nu"]; + let n = 0; + const it = cdp[Symbol.iterator](); + do { + var itnext = it.next(); + const p: ParamDefinition = itnext.value; + expect(p.v == n + 1); + expect(p.symbol == symbs[n]); + n++; + } while (!itnext.done) + expect(n === 5); + }); +}); diff --git a/src/param.ts b/src/param.ts index 32700fab30f383f1dabd7fd2042fc97cc39de408..5b615e9969619421d7ad1c926caf473b11a37606 100644 --- a/src/param.ts +++ b/src/param.ts @@ -3,6 +3,7 @@ import { Debug } from "./base"; import { DefinedNumber } from "./util/definedvalue"; import { Interval } from "./util/interval"; import { Message, MessageCode } from "./util/message"; +import { MapIterator } from "./util/iterator"; /** * domaine de définition du paramètre @@ -368,8 +369,7 @@ export class ParamDefinition extends BaseParam { * liste des paramètres d'une équation */ // tslint:disable-next-line:max-classes-per-file -export abstract class ParamsEquation { - +export abstract class ParamsEquation implements Iterable<ParamDefinition> { protected _paramMap: { [key: string]: ParamDefinition } = {}; public hasParameter(name: string): boolean { @@ -435,6 +435,9 @@ export abstract class ParamsEquation { } } + [Symbol.iterator](): Iterator<ParamDefinition> { + return new MapIterator(this._paramMap); + } } /** diff --git a/src/util/iterator.ts b/src/util/iterator.ts index 71a7cfa8105f4a0dc0c86ccab1ccffa76e866ea1..f7f27b52ea284f25f42de972552570dcfb74265b 100644 --- a/src/util/iterator.ts +++ b/src/util/iterator.ts @@ -1,3 +1,12 @@ +/** + * 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; @@ -23,3 +32,62 @@ export class ArrayReverseIterator<T> implements 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 _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 = []; + } + + public next(): IteratorResult<T> { + if (this._index < this._keys.length) { + this._currentKey = this._keys[this._index++]; + return { + done: this._index >= this._keys.length, + value: this._map[this._currentKey] + } + } else { + return { + done: true, + value: null + } + } + } + + public get index() { + return this._index; + } + + public get key(): string { + return this._currentKey; + } +}