Skip to content

Commit f6dfaa3

Browse files
committed
replace library
1 parent ba4d07a commit f6dfaa3

File tree

5 files changed

+187
-7
lines changed

5 files changed

+187
-7
lines changed

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"@rollup/plugin-commonjs": "^18.0.0",
1717
"@shoelace-style/localize": "^3.2.1",
1818
"@shoelace-style/shoelace": "~2.18.0",
19-
"@solancer/two-way-map": "^1.0.4",
2019
"@tailwindcss/container-queries": "^0.1.1",
2120
"@tanstack/lit-virtual": "^3.13.12",
2221
"@tanstack/virtual-core": "^3.13.12",

frontend/src/features/collections/collections-add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { localized, msg } from "@lit/localize";
22
import { Task, TaskStatus } from "@lit/task";
33
import type { SlInput, SlMenuItem } from "@shoelace-style/shoelace";
4-
import { TwoWayMap } from "@solancer/two-way-map";
54
import Fuse from "fuse.js";
65
import { html, nothing } from "lit";
76
import { customElement, property, query, state } from "lit/decorators.js";
@@ -23,6 +22,7 @@ import type {
2322
} from "@/types/api";
2423
import type { Collection, CollectionSearchValues } from "@/types/collection";
2524
import type { UnderlyingFunction } from "@/types/utils";
25+
import { TwoWayMap } from "@/utils/TwoWayMap";
2626

2727
const INITIAL_PAGE_SIZE = 10;
2828
const MIN_SEARCH_LENGTH = 1;

frontend/src/utils/LinkedList.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Based on https://github.com/solancer/two-way-map/tree/v1.0.3/src
3+
*/
4+
5+
export interface ILinkedListNode<T> {
6+
value: T;
7+
next: ILinkedListNode<T> | null;
8+
prev: ILinkedListNode<T> | null;
9+
}
10+
11+
export class LinkedList<T> {
12+
private head: ILinkedListNode<T> | null = null;
13+
private tail: ILinkedListNode<T> | null = null;
14+
15+
add(value: T): void {
16+
const newNode: ILinkedListNode<T> = { value, next: null, prev: this.tail };
17+
18+
if (!this.head) {
19+
this.head = newNode;
20+
} else if (this.tail) {
21+
this.tail.next = newNode;
22+
}
23+
24+
this.tail = newNode;
25+
}
26+
27+
remove(value: T): void {
28+
let current = this.head;
29+
30+
while (current) {
31+
if (current.value === value) {
32+
if (current.prev) {
33+
current.prev.next = current.next;
34+
} else {
35+
this.head = current.next;
36+
}
37+
38+
if (current.next) {
39+
current.next.prev = current.prev;
40+
} else {
41+
this.tail = current.prev;
42+
}
43+
44+
return;
45+
}
46+
47+
current = current.next;
48+
}
49+
}
50+
51+
toArray(): T[] {
52+
const array: T[] = [];
53+
let current = this.head;
54+
55+
while (current) {
56+
array.push(current.value);
57+
current = current.next;
58+
}
59+
60+
return array;
61+
}
62+
63+
getFirst(): T | undefined {
64+
return this.head?.value;
65+
}
66+
67+
getLast(): T | undefined {
68+
return this.tail?.value;
69+
}
70+
71+
clear(): void {
72+
this.head = null;
73+
this.tail = null;
74+
}
75+
}

frontend/src/utils/TwoWayMap.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Based on https://github.com/solancer/two-way-map/tree/v1.0.3/src
3+
*/
4+
5+
import { LinkedList } from "./LinkedList";
6+
7+
export interface ITwoWayMap<K, V> {
8+
set: (key: K, value: V) => void;
9+
get: (key: K) => V | undefined;
10+
getByValue: (value: V) => K | undefined;
11+
delete: (key: K) => boolean;
12+
keys: () => K[];
13+
values: () => V[];
14+
entries: () => [K, V][];
15+
pop: (key: K, defaultValue?: V) => V | undefined;
16+
popitem: (last?: boolean) => [K, V] | undefined;
17+
copy: () => ITwoWayMap<K, V>;
18+
clear: () => void;
19+
}
20+
21+
export class TwoWayMap<K, V> implements ITwoWayMap<K, V> {
22+
private readonly keyToValueMap = new Map<K, V>();
23+
private readonly valueToKeyMap = new Map<V, K>();
24+
private readonly orderList = new LinkedList<K>();
25+
26+
set(key: K, value: V): void {
27+
// Check if the key is already in the map and delete the reverse mapping
28+
if (this.keyToValueMap.has(key)) {
29+
this.valueToKeyMap.delete(this.keyToValueMap.get(key)!);
30+
}
31+
32+
// Check if the value is already in the map and delete the forward mapping
33+
if (this.valueToKeyMap.has(value)) {
34+
this.keyToValueMap.delete(this.valueToKeyMap.get(value)!);
35+
}
36+
37+
// Set new key-value and value-key mappings
38+
this.keyToValueMap.set(key, value);
39+
this.valueToKeyMap.set(value, key);
40+
41+
// Add the key to the order list
42+
this.orderList.add(key);
43+
}
44+
45+
get(key: K): V | undefined {
46+
return this.keyToValueMap.get(key);
47+
}
48+
49+
getByValue(value: V): K | undefined {
50+
return this.valueToKeyMap.get(value);
51+
}
52+
53+
delete(key: K): boolean {
54+
if (!this.keyToValueMap.has(key)) {
55+
return false;
56+
}
57+
58+
const value = this.keyToValueMap.get(key)!;
59+
this.keyToValueMap.delete(key);
60+
this.valueToKeyMap.delete(value);
61+
this.orderList.remove(key);
62+
return true;
63+
}
64+
65+
keys(): K[] {
66+
return this.orderList.toArray();
67+
}
68+
69+
values(): V[] {
70+
return this.orderList.toArray().map((key) => this.keyToValueMap.get(key)!);
71+
}
72+
73+
entries(): [K, V][] {
74+
return this.orderList
75+
.toArray()
76+
.map((key) => [key, this.keyToValueMap.get(key)!] as [K, V]);
77+
}
78+
79+
pop(key: K, defaultValue?: V): V | undefined {
80+
if (this.keyToValueMap.has(key)) {
81+
const value = this.keyToValueMap.get(key);
82+
this.delete(key);
83+
return value;
84+
} else {
85+
return defaultValue;
86+
}
87+
}
88+
89+
popitem(last = true): [K, V] | undefined {
90+
const key = last ? this.orderList.getLast() : this.orderList.getFirst();
91+
if (key !== undefined) {
92+
const value = this.keyToValueMap.get(key);
93+
this.delete(key);
94+
return [key, value as V];
95+
}
96+
}
97+
98+
copy(): TwoWayMap<K, V> {
99+
const newMap = new TwoWayMap<K, V>();
100+
this.keyToValueMap.forEach((value, key) => {
101+
newMap.set(key, value);
102+
});
103+
return newMap;
104+
}
105+
106+
clear(): void {
107+
this.keyToValueMap.clear();
108+
this.valueToKeyMap.clear();
109+
this.orderList.clear();
110+
}
111+
}

frontend/yarn.lock

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)