@bodil/core 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/collections/lfu-cache.ts +1 -1
- package/src/collections/linked-list.ts +1 -1
- package/src/collections/lru-cache.ts +1 -1
- package/src/disposable.ts +26 -23
- package/dist/assert.d.ts +0 -93
- package/dist/assert.js +0 -217
- package/dist/assert.js.map +0 -1
- package/dist/assert.test.d.ts +0 -1
- package/dist/assert.test.js +0 -51
- package/dist/assert.test.js.map +0 -1
- package/dist/async/abortable-promise.d.ts +0 -71
- package/dist/async/abortable-promise.js +0 -203
- package/dist/async/abortable-promise.js.map +0 -1
- package/dist/async/abortable-promise.test.d.ts +0 -1
- package/dist/async/abortable-promise.test.js +0 -134
- package/dist/async/abortable-promise.test.js.map +0 -1
- package/dist/async/emitter.d.ts +0 -10
- package/dist/async/emitter.js +0 -22
- package/dist/async/emitter.js.map +0 -1
- package/dist/async/emitter.test.d.ts +0 -1
- package/dist/async/emitter.test.js +0 -49
- package/dist/async/emitter.test.js.map +0 -1
- package/dist/async.d.ts +0 -28
- package/dist/async.js +0 -52
- package/dist/async.js.map +0 -1
- package/dist/collections/lfu-cache.d.ts +0 -45
- package/dist/collections/lfu-cache.js +0 -107
- package/dist/collections/lfu-cache.js.map +0 -1
- package/dist/collections/lfu-cache.test.d.ts +0 -1
- package/dist/collections/lfu-cache.test.js +0 -29
- package/dist/collections/lfu-cache.test.js.map +0 -1
- package/dist/collections/linked-list.d.ts +0 -47
- package/dist/collections/linked-list.js +0 -129
- package/dist/collections/linked-list.js.map +0 -1
- package/dist/collections/lru-cache.d.ts +0 -45
- package/dist/collections/lru-cache.js +0 -84
- package/dist/collections/lru-cache.js.map +0 -1
- package/dist/collections/lru-cache.test.d.ts +0 -1
- package/dist/collections/lru-cache.test.js +0 -27
- package/dist/collections/lru-cache.test.js.map +0 -1
- package/dist/collections.d.ts +0 -7
- package/dist/collections.js +0 -8
- package/dist/collections.js.map +0 -1
- package/dist/decorators.d.ts +0 -12
- package/dist/decorators.js +0 -28
- package/dist/decorators.js.map +0 -1
- package/dist/decorators.test.d.ts +0 -1
- package/dist/decorators.test.js +0 -71
- package/dist/decorators.test.js.map +0 -1
- package/dist/disposable.d.ts +0 -138
- package/dist/disposable.js +0 -250
- package/dist/disposable.js.map +0 -1
- package/dist/disposable.test.d.ts +0 -1
- package/dist/disposable.test.js +0 -54
- package/dist/disposable.test.js.map +0 -1
- package/dist/fun.d.ts +0 -40
- package/dist/fun.js +0 -40
- package/dist/fun.js.map +0 -1
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -15
- package/dist/index.js.map +0 -1
- package/dist/order.d.ts +0 -55
- package/dist/order.js +0 -98
- package/dist/order.js.map +0 -1
- package/dist/order.test.d.ts +0 -1
- package/dist/order.test.js +0 -63
- package/dist/order.test.js.map +0 -1
- package/dist/time.d.ts +0 -80
- package/dist/time.js +0 -130
- package/dist/time.js.map +0 -1
- package/dist/time.test.d.ts +0 -1
- package/dist/time.test.js +0 -38
- package/dist/time.test.js.map +0 -1
- package/dist/types.d.ts +0 -17
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { None, Some } from "@bodil/opt";
|
|
2
|
-
import { LinkedList } from "./linked-list";
|
|
3
|
-
import { assert } from "@bodil/core/assert";
|
|
4
|
-
import { isDisposable } from "../disposable";
|
|
5
|
-
function takeOne(it) {
|
|
6
|
-
if (it === undefined) {
|
|
7
|
-
return None;
|
|
8
|
-
}
|
|
9
|
-
const result = it.next();
|
|
10
|
-
return result.done === true ? None : Some(result.value);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* A Least Frequently Used cache.
|
|
14
|
-
*
|
|
15
|
-
* When the cache needs to drop a value to stay within its bounds, it chooses
|
|
16
|
-
* the value that has the lowest access count.
|
|
17
|
-
*/
|
|
18
|
-
export class LFUCache {
|
|
19
|
-
#index = new Map();
|
|
20
|
-
#list = new LinkedList();
|
|
21
|
-
#options;
|
|
22
|
-
constructor(options = {}) {
|
|
23
|
-
this.#options = options;
|
|
24
|
-
}
|
|
25
|
-
[Symbol.dispose]() {
|
|
26
|
-
for (const record of this.#index.values()) {
|
|
27
|
-
this.#disposeValue(record.value);
|
|
28
|
-
}
|
|
29
|
-
this.#index.clear();
|
|
30
|
-
this.#list = new LinkedList();
|
|
31
|
-
}
|
|
32
|
-
#disposeValue(value) {
|
|
33
|
-
if (this.#options.disposeFunction !== undefined) {
|
|
34
|
-
this.#options.disposeFunction(value);
|
|
35
|
-
}
|
|
36
|
-
else if (isDisposable(value)) {
|
|
37
|
-
value[Symbol.dispose]();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Get the current size of the cache.
|
|
42
|
-
*/
|
|
43
|
-
get size() {
|
|
44
|
-
return this.#index.size;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Test if the cache contains a given key.
|
|
48
|
-
*
|
|
49
|
-
* This does not count as an access towards cache invalidation.
|
|
50
|
-
*/
|
|
51
|
-
has(key) {
|
|
52
|
-
return this.#index.has(key);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Get the value associated with the given key, if it exists in the cache.
|
|
56
|
-
*/
|
|
57
|
-
get(key) {
|
|
58
|
-
const record = this.#index.get(key);
|
|
59
|
-
if (record === undefined) {
|
|
60
|
-
return None;
|
|
61
|
-
}
|
|
62
|
-
const node = record.parent;
|
|
63
|
-
let nextNode = node.next;
|
|
64
|
-
if (nextNode === undefined || nextNode.freq !== node.freq + 1) {
|
|
65
|
-
nextNode = this.#list.insert({ freq: node.freq + 1, items: new Set() }, node, nextNode);
|
|
66
|
-
}
|
|
67
|
-
nextNode.items.add(key);
|
|
68
|
-
record.parent = nextNode;
|
|
69
|
-
node.items.delete(key);
|
|
70
|
-
if (node.items.size === 0) {
|
|
71
|
-
this.#list.remove(node);
|
|
72
|
-
}
|
|
73
|
-
return Some(record.value);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Add a key/value pair to the cache.
|
|
77
|
-
*/
|
|
78
|
-
set(key, value) {
|
|
79
|
-
let record = this.#index.get(key);
|
|
80
|
-
if (record !== undefined) {
|
|
81
|
-
if (record.value !== value) {
|
|
82
|
-
this.#disposeValue(record.value);
|
|
83
|
-
record.value = value;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
if (this.#index.size >= (this.#options.maxLength ?? 256)) {
|
|
88
|
-
const head = this.#list.head;
|
|
89
|
-
takeOne(head?.items.keys()).ifSome((key) => {
|
|
90
|
-
const record = this.#index.get(key);
|
|
91
|
-
assert(record !== undefined);
|
|
92
|
-
this.#disposeValue(record.value);
|
|
93
|
-
head.items.delete(key);
|
|
94
|
-
this.#index.delete(key);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
let node = this.#list.head;
|
|
98
|
-
if (node === undefined || node.freq !== 1) {
|
|
99
|
-
node = this.#list.insert({ freq: 1, items: new Set() }, undefined, node);
|
|
100
|
-
}
|
|
101
|
-
record = { value, parent: node };
|
|
102
|
-
node.items.add(key);
|
|
103
|
-
this.#index.set(key, record);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
//# sourceMappingURL=lfu-cache.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lfu-cache.js","sourceRoot":"","sources":["../../src/collections/lfu-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAe,MAAM,YAAY,CAAC;AAErD,OAAO,EAAE,UAAU,EAAiB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AA4B7C,SAAS,OAAO,CAAI,EAAgB;IAChC,IAAI,EAAE,K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AAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACjC,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "@bodil/opt-vitest";
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest";
|
|
2
|
-
import { LFUCache } from "./lfu-cache";
|
|
3
|
-
import "@bodil/opt-vitest";
|
|
4
|
-
test("LFUCache", () => {
|
|
5
|
-
const evicted = [];
|
|
6
|
-
const cache = new LFUCache({
|
|
7
|
-
maxLength: 3,
|
|
8
|
-
disposeFunction: (value) => evicted.push(value),
|
|
9
|
-
});
|
|
10
|
-
expect(cache.size).equal(0);
|
|
11
|
-
cache.set("foo", "FOO");
|
|
12
|
-
cache.set("bar", "BAR");
|
|
13
|
-
cache.set("baz", "BAZ");
|
|
14
|
-
expect(cache.size).equal(3);
|
|
15
|
-
expect(cache.get("foo")).isSome("FOO");
|
|
16
|
-
expect(cache.get("bar")).isSome("BAR");
|
|
17
|
-
expect(cache.get("baz")).isSome("BAZ");
|
|
18
|
-
expect(cache.get("foo")).isSome("FOO");
|
|
19
|
-
expect(cache.get("bar")).isSome("BAR");
|
|
20
|
-
expect(cache.get("wibble")).isNone();
|
|
21
|
-
cache.set("wibble", "WIBBLE");
|
|
22
|
-
expect(cache.size).equal(3);
|
|
23
|
-
expect(cache.get("wibble")).isSome("WIBBLE");
|
|
24
|
-
expect(cache.get("foo")).isSome("FOO");
|
|
25
|
-
expect(cache.get("bar")).isSome("BAR");
|
|
26
|
-
expect(cache.get("baz")).isNone();
|
|
27
|
-
expect(evicted).deep.equal(["BAZ"]);
|
|
28
|
-
});
|
|
29
|
-
//# sourceMappingURL=lfu-cache.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lfu-cache.test.js","sourceRoot":"","sources":["../../src/collections/lfu-cache.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QA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,EAAE,CAAC;IACrC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC9B,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC7C,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAClC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interface for nodes in a {@link LinkedList}.
|
|
3
|
-
*/
|
|
4
|
-
export interface ListNode {
|
|
5
|
-
prev?: this;
|
|
6
|
-
next?: this;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* A doubly linked list.
|
|
10
|
-
*
|
|
11
|
-
* You never know when you're going to need one.
|
|
12
|
-
*/
|
|
13
|
-
export declare class LinkedList<Node extends ListNode> implements Iterable<Node> {
|
|
14
|
-
head?: Node;
|
|
15
|
-
tail?: Node;
|
|
16
|
-
/**
|
|
17
|
-
* Remove a node from the list.
|
|
18
|
-
*/
|
|
19
|
-
remove(node: Node): void;
|
|
20
|
-
/**
|
|
21
|
-
* Insert a node into the list between the given nodes.
|
|
22
|
-
*
|
|
23
|
-
* It goes without saying that the `prev`/`next` nodes must be adjacent to
|
|
24
|
-
* each other in the list, or exceptions will be thrown.
|
|
25
|
-
*/
|
|
26
|
-
insert(node: Node, prev?: Node, next?: Node): Node;
|
|
27
|
-
/**
|
|
28
|
-
* Insert a node at the head of the list.
|
|
29
|
-
*/
|
|
30
|
-
pushHead(node: Node): void;
|
|
31
|
-
/**
|
|
32
|
-
* Insert a node at the tail of the list.
|
|
33
|
-
*/
|
|
34
|
-
pushTail(node: Node): void;
|
|
35
|
-
/**
|
|
36
|
-
* Remove the node at the head of the list and return it.
|
|
37
|
-
*/
|
|
38
|
-
popHead(): Node | undefined;
|
|
39
|
-
/**
|
|
40
|
-
* Remove the node at the tail of the list and return it.
|
|
41
|
-
*/
|
|
42
|
-
popTail(): Node | undefined;
|
|
43
|
-
/**
|
|
44
|
-
* Iterate over the nodes in the list from head to tail.
|
|
45
|
-
*/
|
|
46
|
-
[Symbol.iterator]: () => IterableIterator<Node>;
|
|
47
|
-
}
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
var _a;
|
|
2
|
-
import { assert } from "@bodil/core/assert";
|
|
3
|
-
/**
|
|
4
|
-
* A doubly linked list.
|
|
5
|
-
*
|
|
6
|
-
* You never know when you're going to need one.
|
|
7
|
-
*/
|
|
8
|
-
export class LinkedList {
|
|
9
|
-
constructor() {
|
|
10
|
-
/**
|
|
11
|
-
* Iterate over the nodes in the list from head to tail.
|
|
12
|
-
*/
|
|
13
|
-
this[_a] = function* () {
|
|
14
|
-
let node = this.head;
|
|
15
|
-
while (node !== undefined) {
|
|
16
|
-
yield node;
|
|
17
|
-
node = node.next;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
static { _a = Symbol.iterator; }
|
|
22
|
-
/**
|
|
23
|
-
* Remove a node from the list.
|
|
24
|
-
*/
|
|
25
|
-
remove(node) {
|
|
26
|
-
if (node.prev !== undefined) {
|
|
27
|
-
node.prev.next = node.next;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
assert(this.head === node);
|
|
31
|
-
this.head = node.next;
|
|
32
|
-
}
|
|
33
|
-
if (node.next !== undefined) {
|
|
34
|
-
node.next.prev = node.prev;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
assert(this.tail === node);
|
|
38
|
-
this.tail = node.prev;
|
|
39
|
-
}
|
|
40
|
-
node.prev = undefined;
|
|
41
|
-
node.next = undefined;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Insert a node into the list between the given nodes.
|
|
45
|
-
*
|
|
46
|
-
* It goes without saying that the `prev`/`next` nodes must be adjacent to
|
|
47
|
-
* each other in the list, or exceptions will be thrown.
|
|
48
|
-
*/
|
|
49
|
-
insert(node, prev, next) {
|
|
50
|
-
assert(node.prev === undefined && node.next === undefined);
|
|
51
|
-
node.prev = prev;
|
|
52
|
-
node.next = next;
|
|
53
|
-
if (next !== undefined) {
|
|
54
|
-
assert(next.prev === prev);
|
|
55
|
-
next.prev = node;
|
|
56
|
-
}
|
|
57
|
-
if (prev !== undefined) {
|
|
58
|
-
assert(prev.next === next);
|
|
59
|
-
prev.next = node;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
this.head = node;
|
|
63
|
-
}
|
|
64
|
-
return node;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Insert a node at the head of the list.
|
|
68
|
-
*/
|
|
69
|
-
pushHead(node) {
|
|
70
|
-
assert(node.prev === undefined && node.next === undefined);
|
|
71
|
-
if (this.head === undefined || this.tail === undefined) {
|
|
72
|
-
this.head = node;
|
|
73
|
-
this.tail = node;
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
node.next = this.head;
|
|
77
|
-
this.head.prev = node;
|
|
78
|
-
this.head = node;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Insert a node at the tail of the list.
|
|
83
|
-
*/
|
|
84
|
-
pushTail(node) {
|
|
85
|
-
assert(node.prev === undefined && node.next === undefined);
|
|
86
|
-
if (this.head === undefined || this.tail === undefined) {
|
|
87
|
-
this.head = node;
|
|
88
|
-
this.tail = node;
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
node.prev = this.tail;
|
|
92
|
-
this.tail.next = node;
|
|
93
|
-
this.tail = node;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Remove the node at the head of the list and return it.
|
|
98
|
-
*/
|
|
99
|
-
popHead() {
|
|
100
|
-
const node = this.head;
|
|
101
|
-
if (node === undefined) {
|
|
102
|
-
return undefined;
|
|
103
|
-
}
|
|
104
|
-
this.head = node.next;
|
|
105
|
-
if (this.head === undefined) {
|
|
106
|
-
this.tail = undefined;
|
|
107
|
-
}
|
|
108
|
-
node.prev = undefined;
|
|
109
|
-
node.next = undefined;
|
|
110
|
-
return node;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Remove the node at the tail of the list and return it.
|
|
114
|
-
*/
|
|
115
|
-
popTail() {
|
|
116
|
-
const node = this.tail;
|
|
117
|
-
if (node === undefined) {
|
|
118
|
-
return undefined;
|
|
119
|
-
}
|
|
120
|
-
this.tail = node.prev;
|
|
121
|
-
if (this.tail === undefined) {
|
|
122
|
-
this.head = undefined;
|
|
123
|
-
}
|
|
124
|
-
node.prev = undefined;
|
|
125
|
-
node.next = undefined;
|
|
126
|
-
return node;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
//# sourceMappingURL=linked-list.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"linked-list.js","sourceRoot":"","sources":["../../src/collections/linked-list.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAU5C;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAAvB;QA+GI;;WAEG;QACH,QAAiB,GAAiC,QAAQ,CAAC;YAGvD,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YACrB,OAAO,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,MAA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L,CAAC;IAED;;OAEG;IACH,OAAO;QACH,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QAC1B,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;OAEG;IACH,OAAO;QACH,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QAC1B,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,OAAO,IAAI,CAAC;IAChB,CAAC;CAcJ"}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { type Option } from "@bodil/opt";
|
|
2
|
-
/**
|
|
3
|
-
* Configuration for the {@link LRUCache}.
|
|
4
|
-
*/
|
|
5
|
-
export type LRUCacheOptions<V> = {
|
|
6
|
-
/**
|
|
7
|
-
* The maximum size of the cache.
|
|
8
|
-
*/
|
|
9
|
-
maxLength?: number;
|
|
10
|
-
/**
|
|
11
|
-
* An optional dispose function to be called when a value is removed from
|
|
12
|
-
* the cache. Defaults to disposing of values which implement
|
|
13
|
-
* {@link Disposable}, and doing nothing otherwise.
|
|
14
|
-
*/
|
|
15
|
-
disposeFunction?: (value: V) => void;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* A Least Recently Used cache.
|
|
19
|
-
*
|
|
20
|
-
* When the cache needs to drop a value to stay within its bounds, it chooses
|
|
21
|
-
* the value that was accessed the least recently.
|
|
22
|
-
*/
|
|
23
|
-
export declare class LRUCache<K, V> implements Disposable {
|
|
24
|
-
#private;
|
|
25
|
-
constructor(options?: LRUCacheOptions<V>);
|
|
26
|
-
[Symbol.dispose](): void;
|
|
27
|
-
/**
|
|
28
|
-
* Get the current size of the cache.
|
|
29
|
-
*/
|
|
30
|
-
get size(): number;
|
|
31
|
-
/**
|
|
32
|
-
* Test if the cache contains a given key.
|
|
33
|
-
*
|
|
34
|
-
* This does not count as an access towards cache invalidation.
|
|
35
|
-
*/
|
|
36
|
-
has(key: K): boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Get the value associated with the given key, if it exists in the cache.
|
|
39
|
-
*/
|
|
40
|
-
get(key: K): Option<V>;
|
|
41
|
-
/**
|
|
42
|
-
* Add a key/value pair to the cache.
|
|
43
|
-
*/
|
|
44
|
-
set(key: K, value: V): void;
|
|
45
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { None, Some } from "@bodil/opt";
|
|
2
|
-
import { assert } from "@bodil/core/assert";
|
|
3
|
-
import { LinkedList } from "./linked-list";
|
|
4
|
-
import { isDisposable } from "../disposable";
|
|
5
|
-
/**
|
|
6
|
-
* A Least Recently Used cache.
|
|
7
|
-
*
|
|
8
|
-
* When the cache needs to drop a value to stay within its bounds, it chooses
|
|
9
|
-
* the value that was accessed the least recently.
|
|
10
|
-
*/
|
|
11
|
-
export class LRUCache {
|
|
12
|
-
#index = new Map();
|
|
13
|
-
#list = new LinkedList();
|
|
14
|
-
#options;
|
|
15
|
-
constructor(options = {}) {
|
|
16
|
-
this.#options = options;
|
|
17
|
-
}
|
|
18
|
-
[Symbol.dispose]() {
|
|
19
|
-
for (const node of this.#index.values()) {
|
|
20
|
-
this.#disposeValue(node.value);
|
|
21
|
-
}
|
|
22
|
-
this.#index.clear();
|
|
23
|
-
this.#list = new LinkedList();
|
|
24
|
-
}
|
|
25
|
-
#disposeValue(value) {
|
|
26
|
-
if (this.#options.disposeFunction !== undefined) {
|
|
27
|
-
this.#options.disposeFunction(value);
|
|
28
|
-
}
|
|
29
|
-
else if (isDisposable(value)) {
|
|
30
|
-
value[Symbol.dispose]();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Get the current size of the cache.
|
|
35
|
-
*/
|
|
36
|
-
get size() {
|
|
37
|
-
return this.#index.size;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Test if the cache contains a given key.
|
|
41
|
-
*
|
|
42
|
-
* This does not count as an access towards cache invalidation.
|
|
43
|
-
*/
|
|
44
|
-
has(key) {
|
|
45
|
-
return this.#index.has(key);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Get the value associated with the given key, if it exists in the cache.
|
|
49
|
-
*/
|
|
50
|
-
get(key) {
|
|
51
|
-
const node = this.#index.get(key);
|
|
52
|
-
if (node === undefined) {
|
|
53
|
-
return None;
|
|
54
|
-
}
|
|
55
|
-
this.#list.remove(node);
|
|
56
|
-
this.#list.pushHead(node);
|
|
57
|
-
return Some(node.value);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Add a key/value pair to the cache.
|
|
61
|
-
*/
|
|
62
|
-
set(key, value) {
|
|
63
|
-
let node = this.#index.get(key);
|
|
64
|
-
if (node !== undefined) {
|
|
65
|
-
this.#list.remove(node);
|
|
66
|
-
if (node.value !== value) {
|
|
67
|
-
this.#disposeValue(node.value);
|
|
68
|
-
node.value = value;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
node = { key, value };
|
|
73
|
-
this.#index.set(key, node);
|
|
74
|
-
}
|
|
75
|
-
this.#list.pushHead(node);
|
|
76
|
-
while (this.#index.size > (this.#options.maxLength ?? 256)) {
|
|
77
|
-
const expiredNode = this.#list.popTail();
|
|
78
|
-
assert(expiredNode !== undefined);
|
|
79
|
-
this.#index.delete(expiredNode.key);
|
|
80
|
-
this.#disposeValue(expiredNode.value);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
//# sourceMappingURL=lru-cache.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lru-cache.js","sourceRoot":"","sources":["../../src/collections/lru-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAe,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAiB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAuB7C;;;;;GAKG;AACH,MAAM,OAAO,QAAQ;IACR,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC9C,KAAK,GAAG,IAAI,UAAU,EAAiB,CAAC;IAC/B,QAAQ,CAAqB;IAEtC,YAAY,UAA8B,EAAE;QACxC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED,CAAC,MAAM,CAAC,OAAO,CAAC;QACZ,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC;YACtC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG,IAAI,UAAU,EAAE,CAAC;IAClC,CAAC;IAED,aAAa,CAAC,KAAQ;QAClB,IAAI,IAAI,CAAC,QAAQ,CAAC,eAAe,KAAK,SAAS,EAAE,CAAC;YAC9C,IAAI,CAAC,QAAQ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CAAC;YACtB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC/B,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAE1B,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,IAAI,GAAG,CAAC,EAAE,CAAC;YACzD,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACzC,MAAM,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC;YAClC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;YACpC,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "@bodil/opt-vitest";
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest";
|
|
2
|
-
import { LRUCache } from "./lru-cache";
|
|
3
|
-
import "@bodil/opt-vitest";
|
|
4
|
-
test("LRUCache", () => {
|
|
5
|
-
const evicted = [];
|
|
6
|
-
const cache = new LRUCache({
|
|
7
|
-
maxLength: 3,
|
|
8
|
-
disposeFunction: (value) => evicted.push(value),
|
|
9
|
-
});
|
|
10
|
-
expect(cache.size).equal(0);
|
|
11
|
-
cache.set("foo", "FOO");
|
|
12
|
-
cache.set("bar", "BAR");
|
|
13
|
-
cache.set("baz", "BAZ");
|
|
14
|
-
expect(cache.size).equal(3);
|
|
15
|
-
expect(cache.get("baz")).isSome("BAZ");
|
|
16
|
-
expect(cache.get("bar")).isSome("BAR");
|
|
17
|
-
expect(cache.get("foo")).isSome("FOO");
|
|
18
|
-
expect(cache.get("wibble")).isNone();
|
|
19
|
-
cache.set("wibble", "WIBBLE");
|
|
20
|
-
expect(cache.size).equal(3);
|
|
21
|
-
expect(cache.get("wibble")).isSome("WIBBLE");
|
|
22
|
-
expect(cache.get("foo")).isSome("FOO");
|
|
23
|
-
expect(cache.get("bar")).isSome("BAR");
|
|
24
|
-
expect(cache.get("baz")).isNone();
|
|
25
|
-
expect(evicted).deep.equal(["BAZ"]);
|
|
26
|
-
});
|
|
27
|
-
//# sourceMappingURL=lru-cache.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lru-cache.test.js","sourceRoot":"","sources":["../../src/collections/lru-cache.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,mBAAmB,CAAC;AAE3B,IAAI,CAAC,UAAU,EAAE,GAAG,EAAE;IAClB,MAAM,OAAO,GAAkB,EAAE,CAAC;IAClC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAiB;QACvC,SAAS,EAAE,CAAC;QACZ,eAAe,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;KAClD,CAAC,CAAC;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACrC,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC9B,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC7C,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAClC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC"}
|
package/dist/collections.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Some useful data structures.
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
export { LinkedList, type ListNode } from "./collections/linked-list";
|
|
6
|
-
export { LFUCache, type LFUCacheOptions } from "./collections/lfu-cache";
|
|
7
|
-
export { LRUCache, type LRUCacheOptions } from "./collections/lru-cache";
|
package/dist/collections.js
DELETED
package/dist/collections.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAiB,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAwB,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAwB,MAAM,yBAAyB,CAAC"}
|
package/dist/decorators.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Assorted decorators.
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Decorate a getter to cache its result.
|
|
7
|
-
*
|
|
8
|
-
* Useful for deferring initialisation of a property until first access.
|
|
9
|
-
* Instead of `myProperty = new Thing();`,
|
|
10
|
-
* use `@cache get myProperty() { return new Thing(); }`.
|
|
11
|
-
*/
|
|
12
|
-
export declare function cache<C extends object, T>(getter: (this: C) => T, context: ClassGetterDecoratorContext<C, T>): (this: C) => T;
|
package/dist/decorators.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Assorted decorators.
|
|
3
|
-
* @module
|
|
4
|
-
*/
|
|
5
|
-
const cachedValues = new WeakMap();
|
|
6
|
-
/**
|
|
7
|
-
* Decorate a getter to cache its result.
|
|
8
|
-
*
|
|
9
|
-
* Useful for deferring initialisation of a property until first access.
|
|
10
|
-
* Instead of `myProperty = new Thing();`,
|
|
11
|
-
* use `@cache get myProperty() { return new Thing(); }`.
|
|
12
|
-
*/
|
|
13
|
-
export function cache(getter, context) {
|
|
14
|
-
return function () {
|
|
15
|
-
let meta = cachedValues.get(this);
|
|
16
|
-
if (meta === undefined) {
|
|
17
|
-
meta = {};
|
|
18
|
-
cachedValues.set(this, meta);
|
|
19
|
-
}
|
|
20
|
-
if (Object.hasOwn(meta, context.name)) {
|
|
21
|
-
return meta[context.name];
|
|
22
|
-
}
|
|
23
|
-
const value = getter.call(this);
|
|
24
|
-
meta[context.name] = value;
|
|
25
|
-
return value;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
//# sourceMappingURL=decorators.js.map
|
package/dist/decorators.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,YAAY,GAAG,IAAI,OAAO,EAA6C,CAAC;AAE9E;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CACjB,MAAsB,EACtB,OAA0C;IAE1C,OAAO;QACH,IAAI,IAAI,GAAG,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAClC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,IAAI,GAAG,EAAE,CAAC;YACV,YAAY,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACpC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAM,CAAC;QACnC,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QAC3B,OAAO,KAAK,CAAC;IACjB,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/decorators.test.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
2
|
-
var useValue = arguments.length > 2;
|
|
3
|
-
for (var i = 0; i < initializers.length; i++) {
|
|
4
|
-
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
5
|
-
}
|
|
6
|
-
return useValue ? value : void 0;
|
|
7
|
-
};
|
|
8
|
-
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
9
|
-
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
10
|
-
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
11
|
-
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
12
|
-
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
13
|
-
var _, done = false;
|
|
14
|
-
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
15
|
-
var context = {};
|
|
16
|
-
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
17
|
-
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
18
|
-
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
19
|
-
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
20
|
-
if (kind === "accessor") {
|
|
21
|
-
if (result === void 0) continue;
|
|
22
|
-
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
23
|
-
if (_ = accept(result.get)) descriptor.get = _;
|
|
24
|
-
if (_ = accept(result.set)) descriptor.set = _;
|
|
25
|
-
if (_ = accept(result.init)) initializers.unshift(_);
|
|
26
|
-
}
|
|
27
|
-
else if (_ = accept(result)) {
|
|
28
|
-
if (kind === "field") initializers.unshift(_);
|
|
29
|
-
else descriptor[key] = _;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
|
-
done = true;
|
|
34
|
-
};
|
|
35
|
-
import { expect, test } from "vitest";
|
|
36
|
-
import { cache } from "./decorators";
|
|
37
|
-
test("@cache", () => {
|
|
38
|
-
let called = 0;
|
|
39
|
-
let CacheTest = (() => {
|
|
40
|
-
let _instanceExtraInitializers = [];
|
|
41
|
-
let _get_value_decorators;
|
|
42
|
-
return class CacheTest {
|
|
43
|
-
static {
|
|
44
|
-
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
45
|
-
_get_value_decorators = [cache];
|
|
46
|
-
__esDecorate(this, null, _get_value_decorators, { kind: "getter", name: "value", static: false, private: false, access: { has: obj => "value" in obj, get: obj => obj.value }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
47
|
-
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
48
|
-
}
|
|
49
|
-
get value() {
|
|
50
|
-
called++;
|
|
51
|
-
return 5;
|
|
52
|
-
}
|
|
53
|
-
constructor() {
|
|
54
|
-
__runInitializers(this, _instanceExtraInitializers);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
})();
|
|
58
|
-
const t = new CacheTest();
|
|
59
|
-
// should not be called at construction
|
|
60
|
-
expect(called).toBe(0);
|
|
61
|
-
// should be called the first time the getter is accessed
|
|
62
|
-
expect(t.value).toBe(5);
|
|
63
|
-
expect(called).toBe(1);
|
|
64
|
-
// should not be called the second time the getter is accessed
|
|
65
|
-
expect(t.value).toBe(5);
|
|
66
|
-
expect(called).toBe(1);
|
|
67
|
-
// or the third, for good measure
|
|
68
|
-
expect(t.value).toBe(5);
|
|
69
|
-
expect(called).toBe(1);
|
|
70
|
-
});
|
|
71
|
-
//# sourceMappingURL=decorators.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"decorators.test.js","sourceRoot":"","sources":["../src/decorators.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE;IAChB,IAAI,MAAM,GAAG,CAAC,CAAC;QACT,SAAS;;;qBAAT,SAAS;;;yCACV,KAAK;gBAAC,sKAAI,KAAK,6DAGf;;;YAHM,IAAI,KAAK;gBACZ,MAAM,EAAE,CAAC;gBACT,OAAO,CAAC,CAAC;YACb,CAAC;;gBAJC,mDAAS;;;;IAMf,MAAM,CAAC,GAAG,IAAI,SAAS,EAAE,CAAC;IAC1B,uCAAuC;IACvC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvB,yDAAyD;IACzD,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvB,8DAA8D;IAC9D,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvB,iCAAiC;IACjC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC3B,CAAC,CAAC,CAAC"}
|