2022-05-27 15:53:02 +00:00
|
|
|
/* ============
|
|
|
|
The FastMap has the goal of creating the fastes to use map possible in JS
|
|
|
|
|
|
|
|
============ */
|
|
|
|
|
|
|
|
import * as plugins from './lik.plugins.js';
|
2020-05-25 22:18:41 +00:00
|
|
|
|
2020-02-06 15:31:58 +00:00
|
|
|
/**
|
|
|
|
* fast map allows for very quick lookups of objects with a unique key
|
|
|
|
*/
|
2020-02-06 11:11:16 +00:00
|
|
|
export class FastMap<T> {
|
|
|
|
private mapObject: { [key: string]: T } = {};
|
|
|
|
|
2020-02-06 15:31:58 +00:00
|
|
|
public isUniqueKey(keyArg: string): boolean {
|
|
|
|
return this.mapObject[keyArg] ? false : true;
|
|
|
|
}
|
|
|
|
|
2020-05-04 00:09:20 +00:00
|
|
|
public addToMap(
|
|
|
|
keyArg: string,
|
|
|
|
objectArg: T,
|
|
|
|
optionsArg?: {
|
|
|
|
force: boolean;
|
|
|
|
}
|
|
|
|
): boolean {
|
|
|
|
if (this.isUniqueKey(keyArg) || (optionsArg && optionsArg.force)) {
|
2020-02-06 15:31:58 +00:00
|
|
|
this.mapObject[keyArg] = objectArg;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getByKey(keyArg: string) {
|
|
|
|
return this.mapObject[keyArg];
|
|
|
|
}
|
|
|
|
|
2021-09-12 13:39:47 +00:00
|
|
|
public removeFromMap(keyArg: string): T {
|
2020-02-06 15:31:58 +00:00
|
|
|
const removedItem = this.getByKey(keyArg);
|
|
|
|
delete this.mapObject[keyArg];
|
|
|
|
return removedItem;
|
|
|
|
}
|
2020-02-06 11:11:16 +00:00
|
|
|
|
2020-02-06 15:31:58 +00:00
|
|
|
public getKeys() {
|
|
|
|
const keys: string[] = [];
|
|
|
|
for (const keyArg in this.mapObject) {
|
2020-05-04 00:09:20 +00:00
|
|
|
if (this.mapObject[keyArg]) {
|
|
|
|
keys.push(keyArg);
|
|
|
|
}
|
2020-02-06 15:31:58 +00:00
|
|
|
}
|
|
|
|
return keys;
|
2020-02-06 11:11:16 +00:00
|
|
|
}
|
2020-02-16 23:15:50 +00:00
|
|
|
|
|
|
|
public clean() {
|
|
|
|
this.mapObject = {};
|
|
|
|
}
|
2020-05-04 00:09:20 +00:00
|
|
|
|
|
|
|
/**
|
2021-09-12 13:39:47 +00:00
|
|
|
* returns a new Fastmap that includes all values from this and all from the fastmap in the argument
|
2020-05-04 00:09:20 +00:00
|
|
|
*/
|
|
|
|
public concat(fastMapArg: FastMap<T>) {
|
|
|
|
const concatedFastmap = new FastMap<T>();
|
|
|
|
for (const key of this.getKeys()) {
|
|
|
|
concatedFastmap.addToMap(key, this.getByKey(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of fastMapArg.getKeys()) {
|
|
|
|
concatedFastmap.addToMap(key, fastMapArg.getByKey(key), {
|
2020-07-12 00:44:50 +00:00
|
|
|
force: true,
|
2020-05-04 00:09:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return concatedFastmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-12 13:39:47 +00:00
|
|
|
* tries to merge another Fastmap
|
2020-05-04 00:09:20 +00:00
|
|
|
* Note: uniqueKeyCollisions will cause overwrite
|
2021-09-12 13:39:47 +00:00
|
|
|
* @param fastMapArg
|
2020-05-04 00:09:20 +00:00
|
|
|
*/
|
|
|
|
public addAllFromOther(fastMapArg: FastMap<T>) {
|
|
|
|
for (const key of fastMapArg.getKeys()) {
|
|
|
|
this.addToMap(key, fastMapArg.getByKey(key), {
|
2020-07-12 00:44:50 +00:00
|
|
|
force: true,
|
2020-05-04 00:09:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 13:39:47 +00:00
|
|
|
|
2021-09-12 14:08:20 +00:00
|
|
|
public async find(findFunctionArg: (mapItemArg: T) => Promise<boolean>) {
|
2021-09-12 13:39:47 +00:00
|
|
|
for (const key of this.getKeys()) {
|
2021-09-12 14:08:20 +00:00
|
|
|
const item = this.getByKey(key);
|
|
|
|
const findFunctionResult = await findFunctionArg(item);
|
|
|
|
if (findFunctionResult) {
|
|
|
|
return item;
|
|
|
|
}
|
2021-09-12 13:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 11:11:16 +00:00
|
|
|
}
|