Commit a8566117 authored by Grand Francois's avatar Grand Francois
Browse files

#36 : ParamsEquation : ajout d'un itérateur sur le map [string]: ParamDefinition

Showing with 159 additions and 5 deletions
+159 -5
/// <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);
......
/// <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();
});
});
/// <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);
});
});
......@@ -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);
}
}
/**
......
/**
* 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;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment