2024-03-20 02:53:17 +01:00
|
|
|
|
import { IAdvancedMap } from '@nitrots/api';
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
2022-10-30 07:08:37 +01:00
|
|
|
|
export class AdvancedMap<T, U> implements IAdvancedMap<T, U>
|
2021-03-17 03:02:09 +01:00
|
|
|
|
{
|
|
|
|
|
private _length: number;
|
|
|
|
|
private _dictionary: Map<T, U>;
|
|
|
|
|
private _array: U[];
|
|
|
|
|
private _keys: T[];
|
|
|
|
|
|
2021-07-25 05:26:55 +02:00
|
|
|
|
constructor(map: Map<T, U> = null)
|
2021-03-17 03:02:09 +01:00
|
|
|
|
{
|
2022-03-03 01:21:30 +01:00
|
|
|
|
this._length = 0;
|
|
|
|
|
this._dictionary = new Map();
|
|
|
|
|
this._array = [];
|
|
|
|
|
this._keys = [];
|
2021-07-25 05:26:55 +02:00
|
|
|
|
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if(map) for(const [key, value] of map.entries()) this.add(key, value);
|
2021-03-17 03:02:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get length(): number
|
|
|
|
|
{
|
|
|
|
|
return this._length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get disposed(): boolean
|
|
|
|
|
{
|
|
|
|
|
return (!this._dictionary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public dispose(): void
|
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if(!this._dictionary)
|
2021-03-17 03:02:09 +01:00
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
for(const key of this._dictionary.keys()) this._dictionary.delete(key);
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
this._dictionary = null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 01:21:30 +01:00
|
|
|
|
this._length = 0;
|
|
|
|
|
this._array = null;
|
|
|
|
|
this._keys = null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public reset(): void
|
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
for(const key of this._dictionary.keys()) this._dictionary.delete(key);
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
2022-03-03 01:21:30 +01:00
|
|
|
|
this._length = 0;
|
|
|
|
|
this._array = [];
|
|
|
|
|
this._keys = [];
|
2021-03-17 03:02:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unshift(key: T, value: U): boolean
|
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if(this._dictionary.get(key) !== null) return false;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
this._dictionary.set(key, value);
|
|
|
|
|
|
|
|
|
|
this._array.unshift(value);
|
|
|
|
|
this._keys.unshift(key);
|
|
|
|
|
|
|
|
|
|
this._length++;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public add(key: T, value: U): boolean
|
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if(this._dictionary.get(key) !== undefined) return false;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
this._dictionary.set(key, value);
|
|
|
|
|
|
2022-03-03 01:21:30 +01:00
|
|
|
|
this._array[this._length] = value;
|
|
|
|
|
this._keys[this._length] = key;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
this._length++;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public remove(key: T): U
|
|
|
|
|
{
|
|
|
|
|
const value = this._dictionary.get(key);
|
|
|
|
|
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if(!value) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
const index = this._array.indexOf(value);
|
|
|
|
|
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if(index >= 0)
|
2021-03-17 03:02:09 +01:00
|
|
|
|
{
|
|
|
|
|
this._array.splice(index, 1);
|
|
|
|
|
this._keys.splice(index, 1);
|
|
|
|
|
|
|
|
|
|
this._length--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._dictionary.delete(key);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getWithIndex(index: number): U
|
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if((index < 0) || (index >= this._length)) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
return this._array[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getKey(index: number): T
|
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
if((index < 0) || (index >= this._length)) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
|
|
return this._keys[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getKeys(): T[]
|
|
|
|
|
{
|
|
|
|
|
return this._keys.slice();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public hasKey(key: T): boolean
|
|
|
|
|
{
|
|
|
|
|
return (this._keys.indexOf(key) > -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getValue(key: T): U
|
|
|
|
|
{
|
|
|
|
|
return this._dictionary.get(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getValues(): U[]
|
|
|
|
|
{
|
|
|
|
|
return this._array.slice();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public hasValue(value: U): boolean
|
|
|
|
|
{
|
|
|
|
|
return (this._array.indexOf(value) > -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public indexOf(value: U): number
|
|
|
|
|
{
|
|
|
|
|
return this._array.indexOf(value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 06:39:10 +01:00
|
|
|
|
public concatenate(newValues: IAdvancedMap<T, U>): void
|
2021-03-17 03:02:09 +01:00
|
|
|
|
{
|
2022-11-08 22:50:16 +01:00
|
|
|
|
for(const k of (newValues as AdvancedMap<T, U>)._keys) this.add(k, newValues.getValue(k));
|
2021-03-17 03:02:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-30 07:08:37 +01:00
|
|
|
|
public clone(): IAdvancedMap<T, U>
|
2021-03-17 03:02:09 +01:00
|
|
|
|
{
|
|
|
|
|
const map = new AdvancedMap<T, U>();
|
|
|
|
|
|
|
|
|
|
map.concatenate(this);
|
|
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
}
|
2021-07-25 05:26:55 +02:00
|
|
|
|
}
|