Debian Codegraph @

node/@xterm/xterm/src/common/SortedList.ts

raw
1 /**
2 * Copyright (c) 2022 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { IdleTaskQueue } from 'common/TaskQueue';
7
8 // Work variables to avoid garbage collection.
9 let i = 0;
10
11 /**
12 * A generic list that is maintained in sorted order and allows values with duplicate keys. Deferred
13 * batch insertion and deletion is used to significantly reduce the time it takes to insert and
14 * delete a large amount of items in succession. This list is based on binary search and as such
15 * locating a key will take O(log n) amortized, this includes the by key iterator.
16 */
17 export class SortedList<T> {
18 private _array: T[] = [];
19
20 private readonly _insertedValues: T[] = [];
21 private readonly _flushInsertedTask = new IdleTaskQueue();
22 private _isFlushingInserted = false;
23
24 private readonly _deletedIndices: number[] = [];
25 private readonly _flushDeletedTask = new IdleTaskQueue();
26 private _isFlushingDeleted = false;
27
28 constructor(
29 private readonly _getKey: (value: T) => number
30 ) {
31 }
32
33 public clear(): void {
34 this._array.length = 0;
35 this._insertedValues.length = 0;
36 this._flushInsertedTask.clear();
37 this._isFlushingInserted = false;
38 this._deletedIndices.length = 0;
39 this._flushDeletedTask.clear();
40 this._isFlushingDeleted = false;
41 }
42
43 public insert(value: T): void {
44 this._flushCleanupDeleted();
45 if (this._insertedValues.length === 0) {
46 this._flushInsertedTask.enqueue(() => this._flushInserted());
47 }
48 this._insertedValues.push(value);
49 }
50
51 private _flushInserted(): void {
52 const sortedAddedValues = this._insertedValues.sort((a, b) => this._getKey(a) - this._getKey(b));
53 let sortedAddedValuesIndex = 0;
54 let arrayIndex = 0;
55
56 const newArray = new Array(this._array.length + this._insertedValues.length);
57
58 for (let newArrayIndex = 0; newArrayIndex < newArray.length; newArrayIndex++) {
59 if (arrayIndex >= this._array.length || this._getKey(sortedAddedValues[sortedAddedValuesIndex]) <= this._getKey(this._array[arrayIndex])) {
60 newArray[newArrayIndex] = sortedAddedValues[sortedAddedValuesIndex];
61 sortedAddedValuesIndex++;
62 } else {
63 newArray[newArrayIndex] = this._array[arrayIndex++];
64 }
65 }
66
67 this._array = newArray;
68 this._insertedValues.length = 0;
69 }
70
71 private _flushCleanupInserted(): void {
72 if (!this._isFlushingInserted && this._insertedValues.length > 0) {
73 this._flushInsertedTask.flush();
74 }
75 }
76
77 public delete(value: T): boolean {
78 this._flushCleanupInserted();
79 if (this._array.length === 0) {
80 return false;
81 }
82 const key = this._getKey(value);
83 if (key === undefined) {
84 return false;
85 }
86 i = this._search(key);
87 if (i === -1) {
88 return false;
89 }
90 if (this._getKey(this._array[i]) !== key) {
91 return false;
92 }
93 do {
94 if (this._array[i] === value) {
95 if (this._deletedIndices.length === 0) {
96 this._flushDeletedTask.enqueue(() => this._flushDeleted());
97 }
98 this._deletedIndices.push(i);
99 return true;
100 }
101 } while (++i < this._array.length && this._getKey(this._array[i]) === key);
102 return false;
103 }
104
105 private _flushDeleted(): void {
106 this._isFlushingDeleted = true;
107 const sortedDeletedIndices = this._deletedIndices.sort((a, b) => a - b);
108 let sortedDeletedIndicesIndex = 0;
109 const newArray = new Array(this._array.length - sortedDeletedIndices.length);
110 let newArrayIndex = 0;
111 for (let i = 0; i < this._array.length; i++) {
112 if (sortedDeletedIndices[sortedDeletedIndicesIndex] === i) {
113 sortedDeletedIndicesIndex++;
114 } else {
115 newArray[newArrayIndex++] = this._array[i];
116 }
117 }
118 this._array = newArray;
119 this._deletedIndices.length = 0;
120 this._isFlushingDeleted = false;
121 }
122
123 private _flushCleanupDeleted(): void {
124 if (!this._isFlushingDeleted && this._deletedIndices.length > 0) {
125 this._flushDeletedTask.flush();
126 }
127 }
128
129 public *getKeyIterator(key: number): IterableIterator<T> {
130 this._flushCleanupInserted();
131 this._flushCleanupDeleted();
132 if (this._array.length === 0) {
133 return;
134 }
135 i = this._search(key);
136 if (i < 0 || i >= this._array.length) {
137 return;
138 }
139 if (this._getKey(this._array[i]) !== key) {
140 return;
141 }
142 do {
143 yield this._array[i];
144 } while (++i < this._array.length && this._getKey(this._array[i]) === key);
145 }
146
147 public forEachByKey(key: number, callback: (value: T) => void): void {
148 this._flushCleanupInserted();
149 this._flushCleanupDeleted();
150 if (this._array.length === 0) {
151 return;
152 }
153 i = this._search(key);
154 if (i < 0 || i >= this._array.length) {
155 return;
156 }
157 if (this._getKey(this._array[i]) !== key) {
158 return;
159 }
160 do {
161 callback(this._array[i]);
162 } while (++i < this._array.length && this._getKey(this._array[i]) === key);
163 }
164
165 public values(): IterableIterator<T> {
166 this._flushCleanupInserted();
167 this._flushCleanupDeleted();
168 // Duplicate the array to avoid issues when _array changes while iterating
169 return [...this._array].values();
170 }
171
172 private _search(key: number): number {
173 let min = 0;
174 let max = this._array.length - 1;
175 while (max >= min) {
176 let mid = (min + max) >> 1;
177 const midKey = this._getKey(this._array[mid]);
178 if (midKey > key) {
179 max = mid - 1;
180 } else if (midKey < key) {
181 min = mid + 1;
182 } else {
183 // key in list, walk to lowest duplicate
184 while (mid > 0 && this._getKey(this._array[mid - 1]) === key) {
185 mid--;
186 }
187 return mid;
188 }
189 }
190 // key not in list
191 // still return closest min (also used as insert position)
192 return min;
193 }
194 }