Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented missing methods in BigMap #142

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions packages/transformer/src/BigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ import { Id64String } from "@itwin/core-bentley";
* replace this stopgap with a more robust solution, utilizing a temporary SQLite database (https://github.com/iTwin/imodel-transformer/issues/83).
* @internal
*/
export class BigMap<V> extends Map<Id64String, V> {
export class BigMap<V> implements Map<Id64String, V> {
private _maps: Record<string, Map<string, V>>;
private _size: number;

public override get size(): number {
public get size(): number {
return this._size;
}

public constructor() {
super();
this._maps = {
0: new Map(),
1: new Map(),
Expand All @@ -45,12 +44,12 @@ export class BigMap<V> extends Map<Id64String, V> {
this._size = 0;
}

public override clear(): void {
public clear(): void {
Object.values(this._maps).forEach((m) => m.clear());
this._size = 0;
}

public override delete(key: Id64String): boolean {
public delete(key: Id64String): boolean {
const wasDeleted = this._maps[key[key.length - 1]].delete(key);
if (wasDeleted) {
this._size--;
Expand All @@ -59,21 +58,21 @@ export class BigMap<V> extends Map<Id64String, V> {
return wasDeleted;
}

public override forEach(callbackfn: (value: V, key: Id64String, map: Map<Id64String, V>) => void, thisArg?: any): void {
public forEach(callbackfn: (value: V, key: Id64String, map: Map<Id64String, V>) => void, thisArg?: any): void {
Object.values(this._maps).forEach((m) => {
m.forEach(callbackfn, thisArg);
});
}

public override get(key: Id64String): V | undefined {
public get(key: Id64String): V | undefined {
return this._maps[key[key.length - 1]].get(key);
}

public override has(key: Id64String): boolean {
public has(key: Id64String): boolean {
return this._maps[key[key.length - 1]].has(key);
}

public override set(key: Id64String, value: V): this {
public set(key: Id64String, value: V): this {
const mapForKey = this._maps[key[key.length - 1]];
if (mapForKey === undefined)
throw Error(`Tried to set ${key}, but that key has no submap`);
Expand All @@ -83,4 +82,36 @@ export class BigMap<V> extends Map<Id64String, V> {
this._size += (afterSize - beforeSize);
return this;
}

[Symbol.iterator](): IterableIterator<[Id64String, V]>{
return this.entries();
}

get [Symbol.toStringTag]() {
return 'BigMap';
}

*entries(): IterableIterator<[Id64String, V]> {
var maps = Object.values(this._maps);
for (var map of maps) {
for (let [key, value] of map.entries())
MichaelBelousov marked this conversation as resolved.
Show resolved Hide resolved
yield [key, value];
}
}

*keys(): IterableIterator<Id64String> {
var maps = Object.values(this._maps);
for (var map of maps) {
for (let key of map.keys())
MichaelBelousov marked this conversation as resolved.
Show resolved Hide resolved
yield key;
}
}

*values(): IterableIterator<V> {
var maps = Object.values(this._maps);
for (var map of maps) {
for (let value of map.values())
yield value;
MichaelBelousov marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
85 changes: 85 additions & 0 deletions packages/transformer/src/test/standalone/BigMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { BigMap } from "../../BigMap";
import { assert } from "chai";

describe ("BigMap", function () {

// Test map keys will be assigned into 2 different submaps when BigMap is created
const testMap = new Map ([["0x123f", "testVal1"], ["0x1231", "testVal2"]]);

const createBigMap = (map: Map<string, string>) : BigMap<string> => {
const bigMap = new BigMap <string> ();
for (let entry of map.entries ()) {
MichaelBelousov marked this conversation as resolved.
Show resolved Hide resolved
bigMap.set(entry[0], entry[1]);
}
return bigMap;
}

describe ("keys", function () {
it("should iterate all keys", async function () {
const bigMap = createBigMap (testMap);
assert.sameMembers([...bigMap.keys()], [...testMap.keys()]);
});
});

describe ("values", function () {
it("should get all values", async function () {
const bigMap = createBigMap (testMap);
assert.sameMembers([...bigMap.values()], [...testMap.values()]);
});
});

describe ("entries", function () {
it("should get all values", async function () {
const bigMap = createBigMap (testMap);
const actualMap = new Map([...bigMap.entries()]);
assert.deepEqual(actualMap, testMap);
});
});

describe ("iterator", function () {
it("should get all values", async function () {
const bigMap = createBigMap (testMap);

const actualMap = new Map();
for (let entry of bigMap) {
MichaelBelousov marked this conversation as resolved.
Show resolved Hide resolved
actualMap.set(entry[0], entry[1]);
}
assert.deepEqual(actualMap, testMap);
});
});

describe ("toStringTag", function () {
it("should return type name", async function () {
const typeName = Object.prototype.toString.call(new BigMap <string> ());
assert.equal(typeName, "[object BigMap]");
});
});

describe ("has", function () {
it("should return true when value was set", async function () {
const bigMap = new BigMap <string> ();
const key = "0x123f";
bigMap.set(key, "12134");
assert.isTrue (bigMap.has(key));
});
});

describe ("set", function () {
it("should set when key has submap", async function () {
const bigMap = new BigMap <string> ();
assert.doesNotThrow(() => bigMap.set("0x13", "12134"));
assert.equal(bigMap.size, 1);
});

it("should throw when key has no submap", async function () {
const bigMap = new BigMap <string> ();
assert.throw(() => bigMap.set("g", "12134"), "Tried to set g, but that key has no submap");
});
});

});
Loading