155
}
156
157
>
function prepend(list: Node, nodeToAppend: Node): Node {
tokenStore.ts
158
>
let curNode = list;
159
>
const parents: ListNode[] = [];
160
>
while (nodeToAppend.height !== curNode.height) {
161
>
if (isLeaf(curNode)) {
162
throw new Error('unexpected');
163
}
165
>
// assert 2 <= curNode.childrenFast.length <= 3
166
>
curNode = curNode.children[0] as ListNode;
167
>
}
168
>
let nodeToPrependOfCorrectHeight: Node | undefined = nodeToAppend;
169
>
// assert nodeToAppendOfCorrectHeight!.listHeight === curNode.listHeight
170
>
for (let i = parents.length - 1; i >= 0; i--) {
171
>
const parent = parents[i];
172
>
if (nodeToPrependOfCorrectHeight) {
173
>
// Can we take the element?
174
>
if (parent.children.length >= 3) {
175
// we need to split to maintain (2,3)-tree property.
176
// Send the third element + the new element to the parent.
177
nodeToPrependOfCorrectHeight = ListNode.create(nodeToPrependOfCorrectHeight, parent.unprependChild());
179
>
parent.prependChild(nodeToPrependOfCorrectHeight);
180
>
nodeToPrependOfCorrectHeight = undefined;
181
>
}
182
>
}
183
>
}
184
>
if (nodeToPrependOfCorrectHeight) {
185
return ListNode.create(nodeToPrependOfCorrectHeight, list);
187
>
return list;
188
>
}
189
>
}
190
191
function concat(node1: Node, node2: Node): Node {