Skip to content

Commit

Permalink
feat: add ReadonlyMultiKeyMap type
Browse files Browse the repository at this point in the history
  • Loading branch information
giladgd committed Sep 14, 2024
1 parent adbb9e3 commit 7c072ef
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/MultiKeyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const valueSymbol = Symbol();
*/
export class MultiKeyMap<const Key extends readonly any[], const V> {
/** @internal */ public readonly _map: InternalMap<Key> = new Map();
/** @internal */ public readonly _keys = new Map<Key, V>();
/** @internal */ private readonly _keys = new Map<Key, V>();

public constructor(entries?: readonly (readonly [key: Key, value: V])[] | MultiKeyMap<Key, V> | null) {
if (entries != null) {
Expand All @@ -19,7 +19,7 @@ export class MultiKeyMap<const Key extends readonly any[], const V> {
*
* Time complexity: O(1), given that the length of the key is constant.
*/
public set(key: Key, value: V): this {
public set(key: Readonly<Key>, value: V): this {
let map: InternalMap<Key> = this._map;

for (let i = 0; i < key.length; i++) {
Expand Down Expand Up @@ -48,7 +48,7 @@ export class MultiKeyMap<const Key extends readonly any[], const V> {
*
* Time complexity: O(1), given that the length of the key is constant.
*/
public get(key: Key): V | undefined {
public get(key: Readonly<Key>): V | undefined {
let map: InternalMap<Key> | undefined = this._map;

for (let i = 0; i < key.length && map != null; i++)
Expand All @@ -69,7 +69,7 @@ export class MultiKeyMap<const Key extends readonly any[], const V> {
*
* Time complexity: O(1), given that the length of the key is constant.
*/
public has(key: Key): boolean {
public has(key: Readonly<Key>): boolean {
let map: InternalMap<Key> | undefined = this._map;

for (let i = 0; i < key.length && map != null; i++) {
Expand All @@ -84,7 +84,7 @@ export class MultiKeyMap<const Key extends readonly any[], const V> {
*
* Time complexity: O(1), given that the length of the key is constant.
*/
public delete(key: Key): boolean {
public delete(key: Readonly<Key>): boolean {
let map: InternalMap<Key> | undefined = this._map;
const stack: [accessMap: InternalMap<Key>, accessKey: Key[number]][] = [];

Expand Down Expand Up @@ -172,4 +172,6 @@ export class MultiKeyMap<const Key extends readonly any[], const V> {
}
}

export type ReadonlyMultiKeyMap<Key extends readonly any[], V> = Omit<MultiKeyMap<Key, V>, "set" | "delete" | "clear">;

type InternalMap<Key extends readonly any[]> = Map<Key, InternalMap<Key>> & Map<typeof valueSymbol, Key>;
4 changes: 3 additions & 1 deletion test/MultiKeyMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ describe("MultiKeyMap", () => {
map.set(["rootA", "null"], null);
map.set(["rootB", "undefined"], undefined);
map.set(["rootA", "num"], 4);
map.set(["rootA", "num2"], 8);

const key1 = ["rootA", "num2"] as const;
map.set(key1, 8);

const map2 = new MultiKeyMap(map);

Expand Down

0 comments on commit 7c072ef

Please sign in to comment.