fix(Objectmap): switch to an object map with unique keys
This commit is contained in:
parent
9d756dbff7
commit
b880036b64
39
package-lock.json
generated
39
package-lock.json
generated
@ -385,6 +385,17 @@
|
||||
"luxon": "^1.16.0"
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartunique": {
|
||||
"version": "3.0.1",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartunique/-/smartunique-3.0.1.tgz",
|
||||
"integrity": "sha512-xBu9ZB4C0BA0S/pbFFZn2ItPfnodPKpzrYIq1yN5XDs6OaookwcDF/iBwfS9+EYMSPENC9wAsOxg2RGMm4Qicw==",
|
||||
"requires": {
|
||||
"@types/shortid": "^0.0.29",
|
||||
"@types/uuid": "^3.0.0",
|
||||
"shortid": "^2.2.8",
|
||||
"uuid": "^3.1.0"
|
||||
}
|
||||
},
|
||||
"@pushrocks/tapbundle": {
|
||||
"version": "3.2.0",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2ftapbundle/-/tapbundle-3.2.0.tgz",
|
||||
@ -540,6 +551,11 @@
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-13.7.0.tgz",
|
||||
"integrity": "sha512-GnZbirvmqZUzMgkFn70c74OQpTTUcCzlhQliTzYjQMqg+hVKcDnxdL19Ne3UdYzdMA/+W3eb646FWn/ZaT1NfQ=="
|
||||
},
|
||||
"@types/shortid": {
|
||||
"version": "0.0.29",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fshortid/-/shortid-0.0.29.tgz",
|
||||
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps="
|
||||
},
|
||||
"@types/through2": {
|
||||
"version": "2.0.34",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fthrough2/-/through2-2.0.34.tgz",
|
||||
@ -549,6 +565,11 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/uuid": {
|
||||
"version": "3.4.7",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fuuid/-/uuid-3.4.7.tgz",
|
||||
"integrity": "sha512-C2j2FWgQkF1ru12SjZJyMaTPxs/f6n90+5G5qNakBxKXjTBc/YTSelHh4Pz1HUDwxFXD9WvpQhOGCDC+/Y4mIQ=="
|
||||
},
|
||||
"@types/vinyl": {
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fvinyl/-/vinyl-2.0.3.tgz",
|
||||
@ -1259,6 +1280,11 @@
|
||||
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
|
||||
"dev": true
|
||||
},
|
||||
"nanoid": {
|
||||
"version": "2.1.11",
|
||||
"resolved": "https://verdaccio.lossless.one/nanoid/-/nanoid-2.1.11.tgz",
|
||||
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
|
||||
},
|
||||
"nice-try": {
|
||||
"version": "1.0.5",
|
||||
"resolved": "https://verdaccio.lossless.one/nice-try/-/nice-try-1.0.5.tgz",
|
||||
@ -1542,6 +1568,14 @@
|
||||
"integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=",
|
||||
"dev": true
|
||||
},
|
||||
"shortid": {
|
||||
"version": "2.2.15",
|
||||
"resolved": "https://verdaccio.lossless.one/shortid/-/shortid-2.2.15.tgz",
|
||||
"integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==",
|
||||
"requires": {
|
||||
"nanoid": "^2.1.0"
|
||||
}
|
||||
},
|
||||
"signal-exit": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://verdaccio.lossless.one/signal-exit/-/signal-exit-3.0.2.tgz",
|
||||
@ -1755,6 +1789,11 @@
|
||||
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=",
|
||||
"dev": true
|
||||
},
|
||||
"uuid": {
|
||||
"version": "3.4.0",
|
||||
"resolved": "https://verdaccio.lossless.one/uuid/-/uuid-3.4.0.tgz",
|
||||
"integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A=="
|
||||
},
|
||||
"vinyl": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://verdaccio.lossless.one/vinyl/-/vinyl-2.2.0.tgz",
|
||||
|
@ -33,6 +33,7 @@
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
"@pushrocks/smartrx": "^2.0.5",
|
||||
"@pushrocks/smarttime": "^3.0.12",
|
||||
"@pushrocks/smartunique": "^3.0.1",
|
||||
"@types/minimatch": "^3.0.3",
|
||||
"minimatch": "^3.0.4",
|
||||
"symbol-tree": "^3.2.4"
|
||||
|
@ -1,11 +1,41 @@
|
||||
import * as plugins from './lik.plugins';
|
||||
|
||||
/**
|
||||
* fast map allows for very quick lookups of objects with a unique key
|
||||
*/
|
||||
export class FastMap<T> {
|
||||
private mapObject: { [key: string]: T } = {};
|
||||
|
||||
public isUniqueKey() {}
|
||||
public isUniqueKey(keyArg: string): boolean {
|
||||
return this.mapObject[keyArg] ? false : true;
|
||||
}
|
||||
|
||||
public addToMap(identifier: string, objectArg: T) {
|
||||
this.mapObject[identifier] = objectArg;
|
||||
public addToMap(keyArg: string, objectArg: T): boolean {
|
||||
if (this.isUniqueKey(keyArg)) {
|
||||
this.mapObject[keyArg] = objectArg;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public getByKey(keyArg: string) {
|
||||
return this.mapObject[keyArg];
|
||||
}
|
||||
|
||||
public removeFromMap(keyArg): T {
|
||||
const removedItem = this.getByKey(keyArg);
|
||||
delete this.mapObject[keyArg];
|
||||
return removedItem;
|
||||
}
|
||||
|
||||
public getKeys() {
|
||||
const keys: string[] = [];
|
||||
for (const keyArg in this.mapObject) {
|
||||
if (this.mapObject[keyArg]) {
|
||||
keys.push(keyArg);
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,12 @@ export class LoopTracker<T> {
|
||||
* checks and tracks an object
|
||||
* @param objectArg
|
||||
*/
|
||||
checkAndTrack(objectArg: T) {
|
||||
return this.referenceObjectMap.add(objectArg);
|
||||
checkAndTrack(objectArg: T): boolean {
|
||||
if (!this.referenceObjectMap.checkForObject(objectArg)) {
|
||||
this.referenceObjectMap.add(objectArg);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ export interface IObjectmapFindFunction<T> {
|
||||
*/
|
||||
export class Objectmap<T> {
|
||||
private fastMap = new FastMap<T>();
|
||||
private objectArray: T[] = [];
|
||||
|
||||
// events
|
||||
public eventSubject = new plugins.smartrx.rxjs.Subject<any>();
|
||||
@ -30,45 +29,51 @@ export class Objectmap<T> {
|
||||
* adds an object mapped to a string
|
||||
* the string must be unique
|
||||
*/
|
||||
addMappedUnique(uniqueKey: string, objectArg: T) {
|
||||
this.add(objectArg);
|
||||
this.fastMap.addToMap(uniqueKey, objectArg);
|
||||
addMappedUnique(uniqueKeyArg: string, objectArg: T) {
|
||||
this.fastMap.addToMap(uniqueKeyArg, objectArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* fastest way to get an object from the map
|
||||
* @param uniqueKey
|
||||
*/
|
||||
public getMappedUnique(uniqueKey: string) {}
|
||||
public getMappedUnique(uniqueKeyArg: string) {
|
||||
return this.fastMap.getByKey(uniqueKeyArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* remove key
|
||||
* @param functionArg
|
||||
*/
|
||||
public removeMappedUnique() {}
|
||||
public removeMappedUnique(uniqueKey: string) {
|
||||
const object = this.getMappedUnique(uniqueKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* add object to Objectmap
|
||||
* returns false if the object is already in the map
|
||||
* returns true if the object was added successfully
|
||||
*/
|
||||
public add(objectArg: T): boolean {
|
||||
if (this.checkForObject(objectArg)) {
|
||||
// the object is already in the objectmap
|
||||
return false;
|
||||
} else {
|
||||
// the object is not yet in the objectmap
|
||||
this.objectArray.push(objectArg);
|
||||
this.eventSubject.next('add');
|
||||
return true;
|
||||
public add(objectArg: T): string {
|
||||
// lets search for an existing unique key
|
||||
for (const keyArg of this.fastMap.getKeys()) {
|
||||
const object = this.fastMap.getByKey(keyArg);
|
||||
if (object === objectArg) {
|
||||
return keyArg;
|
||||
}
|
||||
}
|
||||
|
||||
// otherwise lets create it
|
||||
const uniqueKey = plugins.smartunique.shortId();
|
||||
this.addMappedUnique(uniqueKey, objectArg);
|
||||
return uniqueKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* like .add but adds an whole array of objects
|
||||
*/
|
||||
public addArray(objectArrayArg: T[]) {
|
||||
for (let item of objectArrayArg) {
|
||||
for (const item of objectArrayArg) {
|
||||
this.add(item);
|
||||
}
|
||||
}
|
||||
@ -76,19 +81,34 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* check if object is in Objectmap
|
||||
*/
|
||||
public checkForObject(objectArg: T) {
|
||||
return this.objectArray.indexOf(objectArg) !== -1;
|
||||
public checkForObject(objectArg: T): boolean {
|
||||
return !!this.getKeyForObject(objectArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* get key for object
|
||||
* @param findFunction
|
||||
*/
|
||||
public getKeyForObject(objectArg: T) {
|
||||
let foundKey: string = null;
|
||||
for (const keyArg of this.fastMap.getKeys()) {
|
||||
if (!foundKey && this.fastMap.getByKey(keyArg) === objectArg) {
|
||||
foundKey = keyArg;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return foundKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* find object
|
||||
*/
|
||||
public find(findFunction: IObjectmapFindFunction<T>) {
|
||||
const resultArray = this.objectArray.filter(findFunction);
|
||||
if (resultArray.length > 0) {
|
||||
return resultArray[0];
|
||||
} else {
|
||||
return null;
|
||||
public find(findFunction: IObjectmapFindFunction<T>): T {
|
||||
for (const keyArg of this.fastMap.getKeys()) {
|
||||
if (findFunction(this.fastMap.getByKey(keyArg))) {
|
||||
return this.getMappedUnique(keyArg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,8 +127,8 @@ export class Objectmap<T> {
|
||||
* run function for each item in Objectmap
|
||||
*/
|
||||
public async forEach(functionArg: IObjectmapForEachFunction<T>) {
|
||||
for (let object of this.objectArray) {
|
||||
await functionArg(object);
|
||||
for (const keyArg of this.fastMap.getKeys()) {
|
||||
await functionArg(this.fastMap.getByKey(keyArg));
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,7 +136,9 @@ export class Objectmap<T> {
|
||||
* gets an object in the Observablemap and removes it, so it can't be retrieved again
|
||||
*/
|
||||
public getOneAndRemove(): T {
|
||||
const removedItem = this.objectArray.shift();
|
||||
const keys = this.fastMap.getKeys();
|
||||
const keyToUse = keys[keys.length - 1];
|
||||
const removedItem = this.fastMap.removeFromMap(keyToUse);
|
||||
this.eventSubject.next('remove');
|
||||
return removedItem;
|
||||
}
|
||||
@ -126,8 +148,8 @@ export class Objectmap<T> {
|
||||
*/
|
||||
public getArray(): T[] {
|
||||
const returnArray: any[] = [];
|
||||
for (const objectItem of this.objectArray) {
|
||||
returnArray.push(objectItem);
|
||||
for (const keyArg of this.fastMap.getKeys()) {
|
||||
returnArray.push(this.fastMap.getByKey(keyArg));
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
@ -136,32 +158,29 @@ export class Objectmap<T> {
|
||||
* check if Objectmap ist empty
|
||||
*/
|
||||
public isEmpty(): boolean {
|
||||
if (this.objectArray.length === 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return this.fastMap.getKeys().length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* remove object from Objectmap
|
||||
*/
|
||||
public remove(objectArg: T) {
|
||||
let replacementArray = [];
|
||||
for (let item of this.objectArray) {
|
||||
if (item !== objectArg) {
|
||||
replacementArray.push(item);
|
||||
}
|
||||
public remove(objectArg: T): T {
|
||||
if (this.checkForObject(objectArg)) {
|
||||
const keyArg = this.getKeyForObject(objectArg);
|
||||
const removedObject = this.fastMap.removeFromMap(keyArg);
|
||||
this.eventSubject.next('remove');
|
||||
return removedObject;
|
||||
}
|
||||
this.objectArray = replacementArray;
|
||||
this.eventSubject.next('remove');
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* wipe Objectmap
|
||||
*/
|
||||
public wipe() {
|
||||
this.objectArray = [];
|
||||
this.eventSubject.next('wiped');
|
||||
for (const keyArg of this.fastMap.getKeys()) {
|
||||
this.fastMap.removeFromMap(keyArg);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,9 @@ import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
import * as smartunique from '@pushrocks/smartunique';
|
||||
|
||||
export { smartdelay, smartpromise, smartrx, smarttime };
|
||||
export { smartdelay, smartpromise, smartrx, smarttime, smartunique };
|
||||
|
||||
// ==============
|
||||
// third party
|
||||
|
Loading…
Reference in New Issue
Block a user