articulated 0.2.0 → 0.4.0
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/README.md +18 -1
- package/build/commonjs/id_list.d.ts +56 -20
- package/build/commonjs/id_list.js +207 -107
- package/build/commonjs/id_list.js.map +1 -1
- package/build/commonjs/internal/leaf_map.d.ts +25 -0
- package/build/commonjs/internal/leaf_map.js +54 -0
- package/build/commonjs/internal/leaf_map.js.map +1 -0
- package/build/commonjs/internal/seq_map.d.ts +20 -0
- package/build/commonjs/internal/seq_map.js +40 -0
- package/build/commonjs/internal/seq_map.js.map +1 -0
- package/build/commonjs/vendor/functional-red-black-tree.d.ts +8 -0
- package/build/commonjs/vendor/functional-red-black-tree.js +911 -0
- package/build/commonjs/vendor/functional-red-black-tree.js.map +1 -0
- package/build/esm/id_list.d.ts +56 -20
- package/build/esm/id_list.js +206 -105
- package/build/esm/id_list.js.map +1 -1
- package/build/esm/internal/leaf_map.d.ts +25 -0
- package/build/esm/internal/leaf_map.js +47 -0
- package/build/esm/internal/leaf_map.js.map +1 -0
- package/build/esm/internal/seq_map.d.ts +20 -0
- package/build/esm/internal/seq_map.js +32 -0
- package/build/esm/internal/seq_map.js.map +1 -0
- package/build/esm/vendor/functional-red-black-tree.d.ts +8 -0
- package/build/esm/vendor/functional-red-black-tree.js +911 -0
- package/build/esm/vendor/functional-red-black-tree.js.map +1 -0
- package/package.json +10 -5
- package/src/id_list.ts +306 -109
- package/src/internal/leaf_map.ts +57 -0
- package/src/internal/seq_map.ts +48 -0
- package/src/vendor/functional-red-black-tree.d.ts +177 -0
- package/src/vendor/functional-red-black-tree.js +938 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import createRBTree, { Tree } from "../vendor/functional-red-black-tree";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A persistent map from an InnerNode's seq to its parent's seq
|
|
5
|
+
* (or 0 for the root).
|
|
6
|
+
*
|
|
7
|
+
* Sequence numbers start at 1 and increment each time you call set(nextSeq, ...).
|
|
8
|
+
*/
|
|
9
|
+
export class SeqMap {
|
|
10
|
+
constructor(
|
|
11
|
+
private readonly tree: Tree<number, number>,
|
|
12
|
+
private readonly nextSeq: number
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
static new(): SeqMap {
|
|
16
|
+
return new this(
|
|
17
|
+
createRBTree((a, b) => a - b),
|
|
18
|
+
1
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
bumpNextSeq(): SeqMap {
|
|
23
|
+
return new SeqMap(this.tree, this.nextSeq + 1);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get(seq: number): number {
|
|
27
|
+
return this.tree.get(seq)!;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
set(seq: number, value: number): SeqMap {
|
|
31
|
+
return new SeqMap(this.tree.set(seq, value), this.nextSeq);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// delete(seq: number): SeqMap {
|
|
35
|
+
// return new SeqMap(this.tree.remove(seq), this.nextSeq);
|
|
36
|
+
// }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface MutableSeqMap {
|
|
40
|
+
value: SeqMap;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getAndBumpNextSeq(seqsMut: MutableSeqMap): number {
|
|
44
|
+
// @ts-expect-error Ignore private
|
|
45
|
+
const nextSeq = seqsMut.value.nextSeq;
|
|
46
|
+
seqsMut.value = seqsMut.value.bumpNextSeq();
|
|
47
|
+
return nextSeq;
|
|
48
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Modified from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/functional-red-black-tree/functional-red-black-tree-tests.ts
|
|
2
|
+
// which is MIT Licensed.
|
|
3
|
+
|
|
4
|
+
declare namespace createRBTree {
|
|
5
|
+
/** Represents a functional red-black tree. */
|
|
6
|
+
interface Tree<K, V> {
|
|
7
|
+
/** Returns the root node of the tree. */
|
|
8
|
+
root: Node<K, V>;
|
|
9
|
+
|
|
10
|
+
// /** A sorted array of all keys in the tree. */
|
|
11
|
+
// readonly keys: K[];
|
|
12
|
+
|
|
13
|
+
// /** An array of all values in the tree, sorted by key. */
|
|
14
|
+
// readonly values: V[];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new tree with `key` set to `value`, overwriting any
|
|
18
|
+
* existing value.
|
|
19
|
+
*
|
|
20
|
+
* @param key The key of the item to insert.
|
|
21
|
+
* @param value The value of the item to insert.
|
|
22
|
+
* @returns A new tree with `key` set to `value`.
|
|
23
|
+
*/
|
|
24
|
+
set: (key: K, value: V) => Tree<K, V>;
|
|
25
|
+
|
|
26
|
+
// /**
|
|
27
|
+
// * Walks a visitor function over the nodes of the tree in order.
|
|
28
|
+
// *
|
|
29
|
+
// * @param visitor The callback to be executed on each node. If a truthy
|
|
30
|
+
// * value is returned from the visitor, then iteration is stopped.
|
|
31
|
+
// * @param lo An optional start of the range to visit (inclusive).
|
|
32
|
+
// * @param hi An optional end of the range to visit (non-inclusive).
|
|
33
|
+
// * @returns The last value returned by the callback.
|
|
34
|
+
// */
|
|
35
|
+
// forEach: {
|
|
36
|
+
// <T>(visitor: (key: K, value: V) => T): T;
|
|
37
|
+
// // tslint:disable-next-line:unified-signatures
|
|
38
|
+
// <T>(visitor: (key: K, value: V) => T, lo: K, hi?: K): T;
|
|
39
|
+
// };
|
|
40
|
+
|
|
41
|
+
// /** An iterator pointing to the first element in the tree. */
|
|
42
|
+
// readonly begin: Iterator<K, V>;
|
|
43
|
+
|
|
44
|
+
// /** An iterator pointing to the last element in the tree. */
|
|
45
|
+
// readonly end: Iterator<K, V>;
|
|
46
|
+
|
|
47
|
+
// /**
|
|
48
|
+
// * Finds the first item in the tree whose key is >= `key`.
|
|
49
|
+
// *
|
|
50
|
+
// * @param key The key to search for.
|
|
51
|
+
// * @returns An iterator at the given element.
|
|
52
|
+
// */
|
|
53
|
+
// ge: (key: K) => Iterator<K, V>;
|
|
54
|
+
|
|
55
|
+
// /**
|
|
56
|
+
// * Finds the first item in the tree whose key is > `key`.
|
|
57
|
+
// *
|
|
58
|
+
// * @param key The key to search for.
|
|
59
|
+
// * @returns An iterator at the given element.
|
|
60
|
+
// */
|
|
61
|
+
// gt: (key: K) => Iterator<K, V>;
|
|
62
|
+
|
|
63
|
+
// /**
|
|
64
|
+
// * Finds the last item in the tree whose key is < `key`.
|
|
65
|
+
// *
|
|
66
|
+
// * @param key The key to search for.
|
|
67
|
+
// * @returns An iterator at the given element.
|
|
68
|
+
// */
|
|
69
|
+
// lt: (key: K) => Iterator<K, V>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Finds the last item in the tree whose key is <= `key`.
|
|
73
|
+
*
|
|
74
|
+
* @param key The key to search for.
|
|
75
|
+
* @returns An iterator at the given element.
|
|
76
|
+
*/
|
|
77
|
+
le: (key: K) => Iterator<K, V>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @returns An iterator pointing to the first item in the tree with `key`, otherwise null.
|
|
81
|
+
*/
|
|
82
|
+
find: (key: K) => Iterator<K, V>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Removes the first item with `key` in the tree.
|
|
86
|
+
*
|
|
87
|
+
* @param key The key of the item to remove.
|
|
88
|
+
* @returns A new tree with the given item removed, if it exists.
|
|
89
|
+
*/
|
|
90
|
+
remove: (key: K) => Tree<K, V>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves the value associated with `key`.
|
|
94
|
+
*
|
|
95
|
+
* @param key The key of the item to look up.
|
|
96
|
+
* @returns The value of the first node associated with `key`.
|
|
97
|
+
*/
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
99
|
+
get: (key: K) => V | void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Iterates through the nodes in a red-black tree. */
|
|
103
|
+
interface Iterator<K, V> {
|
|
104
|
+
/** The tree associated with the iterator. */
|
|
105
|
+
tree: Tree<K, V>;
|
|
106
|
+
|
|
107
|
+
// /** Checks if the iterator is valid. */
|
|
108
|
+
// readonly valid: boolean;
|
|
109
|
+
|
|
110
|
+
// /**
|
|
111
|
+
// * The value of the node at the iterator's current position, or null if the
|
|
112
|
+
// * iterator is invalid.
|
|
113
|
+
// */
|
|
114
|
+
// readonly node: Node<K, V> | null;
|
|
115
|
+
|
|
116
|
+
// /** Makes a copy of the iterator. */
|
|
117
|
+
// clone: () => Iterator<K, V>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Removes the iterator's current item form the tree.
|
|
121
|
+
*
|
|
122
|
+
* @returns A new binary search tree with the item removed.
|
|
123
|
+
*/
|
|
124
|
+
remove: () => Tree<K, V>;
|
|
125
|
+
|
|
126
|
+
/** The key of the iterator's current item. */
|
|
127
|
+
readonly key?: K | undefined;
|
|
128
|
+
|
|
129
|
+
/** The value of the iterator's current item. */
|
|
130
|
+
readonly value?: V | undefined;
|
|
131
|
+
|
|
132
|
+
// /** Advances the iterator to the next position. */
|
|
133
|
+
// next: () => void;
|
|
134
|
+
|
|
135
|
+
// /** If true, then the iterator is not at the end of the sequence. */
|
|
136
|
+
// readonly hasNext: boolean;
|
|
137
|
+
|
|
138
|
+
// /**
|
|
139
|
+
// * Updates the value of the iterator's current item.
|
|
140
|
+
// *
|
|
141
|
+
// * @returns A new binary search tree with the corresponding node updated.
|
|
142
|
+
// */
|
|
143
|
+
// update: (value: V) => Tree<K, V>;
|
|
144
|
+
|
|
145
|
+
// /** Moves the iterator backward one element. */
|
|
146
|
+
// prev: () => void;
|
|
147
|
+
|
|
148
|
+
// /** If true, then the iterator is not at the beginning of the sequence. */
|
|
149
|
+
// readonly hasPrev: boolean;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Represents a node in a red-black tree. */
|
|
153
|
+
interface Node<K, V> {
|
|
154
|
+
/** The key associated with the node. */
|
|
155
|
+
key: K;
|
|
156
|
+
|
|
157
|
+
/** The value associated with the node. */
|
|
158
|
+
value: V;
|
|
159
|
+
|
|
160
|
+
/** The left subtree of the node. */
|
|
161
|
+
left: Tree<K, V>;
|
|
162
|
+
|
|
163
|
+
/** The right subtree of the node. */
|
|
164
|
+
right: Tree<K, V>;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Creates an empty red-black tree.
|
|
170
|
+
*
|
|
171
|
+
* @param compare Comparison function, same semantics as array.sort().
|
|
172
|
+
* @returns An empty tree ordered by `compare`.
|
|
173
|
+
*/
|
|
174
|
+
declare function createRBTree<K, V>(
|
|
175
|
+
compare: (key1: K, key2: K) => number
|
|
176
|
+
): createRBTree.Tree<K, V>;
|
|
177
|
+
export = createRBTree;
|