Commit 6794d34e authored by Grand Francois's avatar Grand Francois
Browse files

#36 : amélioration de MapIterator, ajout d'un getter _iterator_ à ParamsEquation

Showing with 41 additions and 5 deletions
+41 -5
...@@ -5,15 +5,34 @@ import { MapIterator } from "../../src/util/iterator"; ...@@ -5,15 +5,34 @@ import { MapIterator } from "../../src/util/iterator";
describe('param map iterator : ', () => { describe('param map iterator : ', () => {
it("sur undefined", () => { it("sur undefined", () => {
const it: MapIterator<any> = new MapIterator(undefined); const it: MapIterator<any> = new MapIterator(undefined);
const v1 = it.next(); const v1 = it.next();
expect(v1.done).toBeTruthy(); expect(v1.done).toBeTruthy();
expect(v1.value).toBeUndefined();
expect(it.key).toBeUndefined();
expect(it.index).toBeUndefined();
const v2 = it.next();
expect(v2.done).toBeTruthy();
expect(v2.value).toBeUndefined();
expect(it.key).toBeUndefined();
expect(it.index).toBeUndefined();
}); });
it("sur {}", () => { it("sur {}", () => {
const it: MapIterator<any> = new MapIterator({}); const it: MapIterator<any> = new MapIterator({});
const v1 = it.next(); const v1 = it.next();
expect(v1.done).toBeTruthy(); expect(v1.done).toBeTruthy();
expect(it.index).toEqual(0); expect(v1.value).toBeUndefined();
expect(it.key).toBeUndefined();
expect(it.index).toBeUndefined();
const v2 = it.next();
expect(v2.done).toBeTruthy();
expect(v2.value).toBeUndefined();
expect(it.key).toBeUndefined();
expect(it.index).toBeUndefined();
}); });
it("sur {'a':1}", () => { it("sur {'a':1}", () => {
...@@ -24,9 +43,13 @@ describe('param map iterator : ', () => { ...@@ -24,9 +43,13 @@ describe('param map iterator : ', () => {
expect(v1.done).toBeTruthy(); expect(v1.done).toBeTruthy();
expect(v1.value).toEqual(1); expect(v1.value).toEqual(1);
expect(it.key).toEqual("a"); expect(it.key).toEqual("a");
expect(it.index).toEqual(0);
const v2 = it.next(); const v2 = it.next();
expect(v2.done).toBeTruthy(); expect(v2.done).toBeTruthy();
expect(v2.value).toBeUndefined();
expect(it.key).toBeUndefined();
expect(it.index).toBeUndefined();
}); });
it("sur {'a':1, 'b':2}", () => { it("sur {'a':1, 'b':2}", () => {
...@@ -45,5 +68,8 @@ describe('param map iterator : ', () => { ...@@ -45,5 +68,8 @@ describe('param map iterator : ', () => {
const v3 = it.next(); const v3 = it.next();
expect(v3.done).toBeTruthy(); expect(v3.done).toBeTruthy();
expect(v3.value).toBeUndefined();
expect(it.key).toBeUndefined();
expect(it.index).toBeUndefined();
}); });
}); });
/// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> /// <reference path="../../node_modules/@types/jasmine/index.d.ts" />
//import { MapIterator } from "../../src/util/iterator";
import { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri"; import { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri";
import { ParamDefinition } from "../../src/index"; import { ParamDefinition } from "../../src/index";
...@@ -21,7 +20,7 @@ describe("iterator ConduiteDistribParams : ", () => { ...@@ -21,7 +20,7 @@ describe("iterator ConduiteDistribParams : ", () => {
const symbs = ["Q", "D", "J", "Lg", "Nu"]; const symbs = ["Q", "D", "J", "Lg", "Nu"];
let n = 0; let n = 0;
const it = cdp[Symbol.iterator](); const it = cdp.iterator;
do { do {
var itnext = it.next(); var itnext = it.next();
const p: ParamDefinition = itnext.value; const p: ParamDefinition = itnext.value;
......
...@@ -436,6 +436,10 @@ export abstract class ParamsEquation implements Iterable<ParamDefinition> { ...@@ -436,6 +436,10 @@ export abstract class ParamsEquation implements Iterable<ParamDefinition> {
} }
[Symbol.iterator](): Iterator<ParamDefinition> { [Symbol.iterator](): Iterator<ParamDefinition> {
return this.iterator;
}
public get iterator() {
return new MapIterator(this._paramMap); return new MapIterator(this._paramMap);
} }
} }
......
...@@ -56,6 +56,8 @@ export class MapIterator<T> implements Iterator<T> { ...@@ -56,6 +56,8 @@ export class MapIterator<T> implements Iterator<T> {
private _keys: string[]; private _keys: string[];
private _lastIndex: number = -1; // index of last element returned; -1 if no such
private _index: number = 0; private _index: number = 0;
private _currentKey: string; private _currentKey: string;
...@@ -69,22 +71,27 @@ export class MapIterator<T> implements Iterator<T> { ...@@ -69,22 +71,27 @@ export class MapIterator<T> implements Iterator<T> {
} }
public next(): IteratorResult<T> { public next(): IteratorResult<T> {
const i = this._index;
if (this._index < this._keys.length) { if (this._index < this._keys.length) {
this._currentKey = this._keys[this._index++]; this._currentKey = this._keys[this._index++];
this._index = i + 1;
this._lastIndex = i;
return { return {
done: this._index >= this._keys.length, done: this._index >= this._keys.length,
value: this._map[this._currentKey] value: this._map[this._currentKey]
} }
} else { } else {
this._currentKey = undefined;
this._lastIndex = undefined;
return { return {
done: true, done: true,
value: null value: undefined
} }
} }
} }
public get index() { public get index() {
return this._index; return this._lastIndex;
} }
public get key(): string { public get key(): string {
......
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