@itwin/core-bentley 4.0.0-dev.7 → 4.0.0-dev.70

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.
Files changed (211) hide show
  1. package/CHANGELOG.md +29 -12
  2. package/lib/cjs/AccessToken.d.ts +10 -10
  3. package/lib/cjs/AccessToken.d.ts.map +1 -1
  4. package/lib/cjs/AccessToken.js +9 -9
  5. package/lib/cjs/Assert.d.ts +25 -25
  6. package/lib/cjs/Assert.js +45 -45
  7. package/lib/cjs/Assert.js.map +1 -1
  8. package/lib/cjs/BeEvent.d.ts +81 -81
  9. package/lib/cjs/BeEvent.d.ts.map +1 -1
  10. package/lib/cjs/BeEvent.js +156 -156
  11. package/lib/cjs/BeSQLite.d.ts +172 -170
  12. package/lib/cjs/BeSQLite.d.ts.map +1 -1
  13. package/lib/cjs/BeSQLite.js +185 -183
  14. package/lib/cjs/BeSQLite.js.map +1 -1
  15. package/lib/cjs/BentleyError.d.ts +378 -378
  16. package/lib/cjs/BentleyError.d.ts.map +1 -1
  17. package/lib/cjs/BentleyError.js +703 -702
  18. package/lib/cjs/BentleyError.js.map +1 -1
  19. package/lib/cjs/BentleyLoggerCategory.d.ts +11 -11
  20. package/lib/cjs/BentleyLoggerCategory.js +19 -19
  21. package/lib/cjs/ByteStream.d.ts +110 -110
  22. package/lib/cjs/ByteStream.js +159 -159
  23. package/lib/cjs/ClassUtils.d.ts +14 -14
  24. package/lib/cjs/ClassUtils.js +27 -27
  25. package/lib/cjs/Compare.d.ts +47 -47
  26. package/lib/cjs/Compare.d.ts.map +1 -1
  27. package/lib/cjs/Compare.js +75 -75
  28. package/lib/cjs/CompressedId64Set.d.ts +134 -134
  29. package/lib/cjs/CompressedId64Set.d.ts.map +1 -1
  30. package/lib/cjs/CompressedId64Set.js +428 -428
  31. package/lib/cjs/CompressedId64Set.js.map +1 -1
  32. package/lib/cjs/Dictionary.d.ts +125 -125
  33. package/lib/cjs/Dictionary.js +203 -203
  34. package/lib/cjs/Disposable.d.ts +80 -80
  35. package/lib/cjs/Disposable.d.ts.map +1 -1
  36. package/lib/cjs/Disposable.js +120 -120
  37. package/lib/cjs/Id.d.ts +285 -285
  38. package/lib/cjs/Id.d.ts.map +1 -1
  39. package/lib/cjs/Id.js +643 -643
  40. package/lib/cjs/IndexMap.d.ts +65 -65
  41. package/lib/cjs/IndexMap.js +91 -91
  42. package/lib/cjs/JsonSchema.d.ts +77 -77
  43. package/lib/cjs/JsonSchema.d.ts.map +1 -1
  44. package/lib/cjs/JsonSchema.js +9 -9
  45. package/lib/cjs/JsonUtils.d.ts +78 -78
  46. package/lib/cjs/JsonUtils.js +151 -151
  47. package/lib/cjs/LRUMap.d.ts +129 -129
  48. package/lib/cjs/LRUMap.js +333 -333
  49. package/lib/cjs/LRUMap.js.map +1 -1
  50. package/lib/cjs/Logger.d.ts +143 -143
  51. package/lib/cjs/Logger.d.ts.map +1 -1
  52. package/lib/cjs/Logger.js +256 -258
  53. package/lib/cjs/Logger.js.map +1 -1
  54. package/lib/cjs/ObservableSet.d.ts +23 -23
  55. package/lib/cjs/ObservableSet.js +51 -51
  56. package/lib/cjs/OneAtATimeAction.d.ts +31 -31
  57. package/lib/cjs/OneAtATimeAction.js +94 -94
  58. package/lib/cjs/OneAtATimeAction.js.map +1 -1
  59. package/lib/cjs/OrderedId64Iterable.d.ts +74 -74
  60. package/lib/cjs/OrderedId64Iterable.d.ts.map +1 -1
  61. package/lib/cjs/OrderedId64Iterable.js +235 -235
  62. package/lib/cjs/OrderedSet.d.ts +40 -40
  63. package/lib/cjs/OrderedSet.js +64 -64
  64. package/lib/cjs/PriorityQueue.d.ts +70 -70
  65. package/lib/cjs/PriorityQueue.d.ts.map +1 -1
  66. package/lib/cjs/PriorityQueue.js +140 -140
  67. package/lib/cjs/ProcessDetector.d.ts +59 -59
  68. package/lib/cjs/ProcessDetector.js +71 -71
  69. package/lib/cjs/SortedArray.d.ts +236 -232
  70. package/lib/cjs/SortedArray.d.ts.map +1 -1
  71. package/lib/cjs/SortedArray.js +315 -303
  72. package/lib/cjs/SortedArray.js.map +1 -1
  73. package/lib/cjs/StatusCategory.d.ts +30 -30
  74. package/lib/cjs/StatusCategory.d.ts.map +1 -1
  75. package/lib/cjs/StatusCategory.js +460 -460
  76. package/lib/cjs/StatusCategory.js.map +1 -1
  77. package/lib/cjs/StringUtils.d.ts +22 -22
  78. package/lib/cjs/StringUtils.js +148 -148
  79. package/lib/cjs/Time.d.ts +122 -122
  80. package/lib/cjs/Time.js +152 -152
  81. package/lib/cjs/Time.js.map +1 -1
  82. package/lib/cjs/Tracing.d.ts +43 -40
  83. package/lib/cjs/Tracing.d.ts.map +1 -1
  84. package/lib/cjs/Tracing.js +134 -130
  85. package/lib/cjs/Tracing.js.map +1 -1
  86. package/lib/cjs/TupleKeyedMap.d.ts +36 -36
  87. package/lib/cjs/TupleKeyedMap.js +102 -102
  88. package/lib/cjs/TypedArrayBuilder.d.ts +155 -155
  89. package/lib/cjs/TypedArrayBuilder.d.ts.map +1 -1
  90. package/lib/cjs/TypedArrayBuilder.js +206 -208
  91. package/lib/cjs/TypedArrayBuilder.js.map +1 -1
  92. package/lib/cjs/UnexpectedErrors.d.ts +43 -43
  93. package/lib/cjs/UnexpectedErrors.d.ts.map +1 -1
  94. package/lib/cjs/UnexpectedErrors.js +68 -68
  95. package/lib/cjs/UnexpectedErrors.js.map +1 -1
  96. package/lib/cjs/UtilityTypes.d.ts +112 -96
  97. package/lib/cjs/UtilityTypes.d.ts.map +1 -1
  98. package/lib/cjs/UtilityTypes.js +40 -40
  99. package/lib/cjs/UtilityTypes.js.map +1 -1
  100. package/lib/cjs/YieldManager.d.ts +18 -18
  101. package/lib/cjs/YieldManager.js +34 -34
  102. package/lib/cjs/core-bentley.d.ts +74 -74
  103. package/lib/cjs/core-bentley.js +94 -90
  104. package/lib/cjs/core-bentley.js.map +1 -1
  105. package/lib/cjs/partitionArray.d.ts +21 -21
  106. package/lib/cjs/partitionArray.js +43 -43
  107. package/lib/esm/AccessToken.d.ts +10 -10
  108. package/lib/esm/AccessToken.d.ts.map +1 -1
  109. package/lib/esm/AccessToken.js +8 -8
  110. package/lib/esm/Assert.d.ts +25 -25
  111. package/lib/esm/Assert.js +41 -41
  112. package/lib/esm/Assert.js.map +1 -1
  113. package/lib/esm/BeEvent.d.ts +81 -81
  114. package/lib/esm/BeEvent.d.ts.map +1 -1
  115. package/lib/esm/BeEvent.js +150 -150
  116. package/lib/esm/BeSQLite.d.ts +172 -170
  117. package/lib/esm/BeSQLite.d.ts.map +1 -1
  118. package/lib/esm/BeSQLite.js +182 -180
  119. package/lib/esm/BeSQLite.js.map +1 -1
  120. package/lib/esm/BentleyError.d.ts +378 -378
  121. package/lib/esm/BentleyError.d.ts.map +1 -1
  122. package/lib/esm/BentleyError.js +699 -698
  123. package/lib/esm/BentleyError.js.map +1 -1
  124. package/lib/esm/BentleyLoggerCategory.d.ts +11 -11
  125. package/lib/esm/BentleyLoggerCategory.js +16 -16
  126. package/lib/esm/ByteStream.d.ts +110 -110
  127. package/lib/esm/ByteStream.js +155 -155
  128. package/lib/esm/ClassUtils.d.ts +14 -14
  129. package/lib/esm/ClassUtils.js +22 -22
  130. package/lib/esm/Compare.d.ts +47 -47
  131. package/lib/esm/Compare.d.ts.map +1 -1
  132. package/lib/esm/Compare.js +63 -63
  133. package/lib/esm/CompressedId64Set.d.ts +134 -134
  134. package/lib/esm/CompressedId64Set.d.ts.map +1 -1
  135. package/lib/esm/CompressedId64Set.js +423 -423
  136. package/lib/esm/CompressedId64Set.js.map +1 -1
  137. package/lib/esm/Dictionary.d.ts +125 -125
  138. package/lib/esm/Dictionary.js +199 -199
  139. package/lib/esm/Disposable.d.ts +80 -80
  140. package/lib/esm/Disposable.d.ts.map +1 -1
  141. package/lib/esm/Disposable.js +112 -112
  142. package/lib/esm/Id.d.ts +285 -285
  143. package/lib/esm/Id.d.ts.map +1 -1
  144. package/lib/esm/Id.js +639 -639
  145. package/lib/esm/IndexMap.d.ts +65 -65
  146. package/lib/esm/IndexMap.js +86 -86
  147. package/lib/esm/JsonSchema.d.ts +77 -77
  148. package/lib/esm/JsonSchema.d.ts.map +1 -1
  149. package/lib/esm/JsonSchema.js +8 -8
  150. package/lib/esm/JsonUtils.d.ts +78 -78
  151. package/lib/esm/JsonUtils.js +148 -148
  152. package/lib/esm/LRUMap.d.ts +129 -129
  153. package/lib/esm/LRUMap.js +326 -326
  154. package/lib/esm/LRUMap.js.map +1 -1
  155. package/lib/esm/Logger.d.ts +143 -143
  156. package/lib/esm/Logger.d.ts.map +1 -1
  157. package/lib/esm/Logger.js +253 -253
  158. package/lib/esm/Logger.js.map +1 -1
  159. package/lib/esm/ObservableSet.d.ts +23 -23
  160. package/lib/esm/ObservableSet.js +47 -47
  161. package/lib/esm/OneAtATimeAction.d.ts +31 -31
  162. package/lib/esm/OneAtATimeAction.js +89 -89
  163. package/lib/esm/OneAtATimeAction.js.map +1 -1
  164. package/lib/esm/OrderedId64Iterable.d.ts +74 -74
  165. package/lib/esm/OrderedId64Iterable.d.ts.map +1 -1
  166. package/lib/esm/OrderedId64Iterable.js +232 -232
  167. package/lib/esm/OrderedSet.d.ts +40 -40
  168. package/lib/esm/OrderedSet.js +59 -59
  169. package/lib/esm/PriorityQueue.d.ts +70 -70
  170. package/lib/esm/PriorityQueue.d.ts.map +1 -1
  171. package/lib/esm/PriorityQueue.js +136 -136
  172. package/lib/esm/ProcessDetector.d.ts +59 -59
  173. package/lib/esm/ProcessDetector.js +67 -67
  174. package/lib/esm/SortedArray.d.ts +236 -232
  175. package/lib/esm/SortedArray.d.ts.map +1 -1
  176. package/lib/esm/SortedArray.js +308 -296
  177. package/lib/esm/SortedArray.js.map +1 -1
  178. package/lib/esm/StatusCategory.d.ts +30 -30
  179. package/lib/esm/StatusCategory.d.ts.map +1 -1
  180. package/lib/esm/StatusCategory.js +455 -454
  181. package/lib/esm/StatusCategory.js.map +1 -1
  182. package/lib/esm/StringUtils.d.ts +22 -22
  183. package/lib/esm/StringUtils.js +142 -142
  184. package/lib/esm/Time.d.ts +122 -122
  185. package/lib/esm/Time.js +146 -146
  186. package/lib/esm/Time.js.map +1 -1
  187. package/lib/esm/Tracing.d.ts +43 -40
  188. package/lib/esm/Tracing.d.ts.map +1 -1
  189. package/lib/esm/Tracing.js +130 -126
  190. package/lib/esm/Tracing.js.map +1 -1
  191. package/lib/esm/TupleKeyedMap.d.ts +36 -36
  192. package/lib/esm/TupleKeyedMap.js +98 -98
  193. package/lib/esm/TypedArrayBuilder.d.ts +155 -155
  194. package/lib/esm/TypedArrayBuilder.d.ts.map +1 -1
  195. package/lib/esm/TypedArrayBuilder.js +198 -200
  196. package/lib/esm/TypedArrayBuilder.js.map +1 -1
  197. package/lib/esm/UnexpectedErrors.d.ts +43 -43
  198. package/lib/esm/UnexpectedErrors.d.ts.map +1 -1
  199. package/lib/esm/UnexpectedErrors.js +65 -64
  200. package/lib/esm/UnexpectedErrors.js.map +1 -1
  201. package/lib/esm/UtilityTypes.d.ts +112 -96
  202. package/lib/esm/UtilityTypes.d.ts.map +1 -1
  203. package/lib/esm/UtilityTypes.js +34 -34
  204. package/lib/esm/UtilityTypes.js.map +1 -1
  205. package/lib/esm/YieldManager.d.ts +18 -18
  206. package/lib/esm/YieldManager.js +30 -30
  207. package/lib/esm/core-bentley.d.ts +74 -74
  208. package/lib/esm/core-bentley.js +78 -78
  209. package/lib/esm/partitionArray.d.ts +21 -21
  210. package/lib/esm/partitionArray.js +39 -39
  211. package/package.json +8 -8
package/lib/esm/LRUMap.js CHANGED
@@ -1,327 +1,327 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
- * See LICENSE.md in the project root for license terms and full copyright notice.
4
- *--------------------------------------------------------------------------------------------*/
5
- /** @packageDocumentation
6
- * @module Collections
7
- */
8
- import { Dictionary } from "./Dictionary";
9
- /**
10
- * Derived from:
11
- * Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
12
- * See README.md at https://github.com/rsms/js-lru for details.
13
- */
14
- /** An entry holds the key and value, and pointers to any older and newer entries.
15
- * @public
16
- */
17
- export class Entry {
18
- constructor(key, value) {
19
- this.key = key;
20
- this.value = value;
21
- }
22
- }
23
- class EntryIterator {
24
- constructor(oldestEntry) {
25
- this._entry = oldestEntry;
26
- }
27
- next() {
28
- const ent = this._entry;
29
- if (!ent)
30
- return { done: true, value: undefined };
31
- this._entry = ent.newer;
32
- const val = [ent.key, ent.value];
33
- return { done: false, value: val };
34
- }
35
- }
36
- class KeyIterator {
37
- constructor(oldestEntry) {
38
- this._entry = oldestEntry;
39
- }
40
- next() {
41
- const ent = this._entry;
42
- if (!ent)
43
- return { done: true, value: undefined };
44
- this._entry = ent.newer;
45
- return { done: false, value: ent.key };
46
- }
47
- }
48
- class ValueIterator {
49
- constructor(oldestEntry) {
50
- this._entry = oldestEntry;
51
- }
52
- next() {
53
- const ent = this._entry;
54
- if (!ent)
55
- return { done: true, value: undefined };
56
- this._entry = ent.newer;
57
- return { done: false, value: ent.value };
58
- }
59
- }
60
- /**
61
- * A mapping of a key/value pairs, where the size of the cache can be limited.
62
- *
63
- * When entries are inserted, if the cache is "full", the
64
- * least-recently-used (LRU) value is dropped. When entries are retrieved, they are moved to the front of the LRU list.
65
- *
66
- * Illustration of the design:
67
- *
68
- * ```
69
- *
70
- * entry entry entry entry
71
- * ______ ______ ______ ______
72
- * | head |.newer => | |.newer => | |.newer => | tail |
73
- * | A | | B | | C | | D |
74
- * |______| <= older.|______| <= older.|______| <= older.|______|
75
- *
76
- * removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
77
- * ```
78
- * @public
79
- */
80
- export class LRUCache {
81
- /**
82
- * Construct a new LRUCache to hold up to `limit` entries.
83
- */
84
- constructor(limit, container) {
85
- this.size = 0;
86
- this.limit = limit;
87
- this.oldest = this.newest = undefined;
88
- this._container = container;
89
- }
90
- markEntryAsUsed(entry) {
91
- if (entry === this.newest)
92
- return; // Already the most recently used entry, so no need to update the list
93
- // HEAD--------------TAIL
94
- // <.older .newer>
95
- // <--- add direction --
96
- // A B C <D> E
97
- if (entry.newer) {
98
- if (entry === this.oldest) {
99
- this.oldest = entry.newer;
100
- }
101
- entry.newer.older = entry.older; // C <-- E.
102
- }
103
- if (entry.older) {
104
- entry.older.newer = entry.newer; // C. --> E
105
- }
106
- entry.newer = undefined; // D --x
107
- entry.older = this.newest; // D. --> E
108
- if (this.newest) {
109
- this.newest.newer = entry; // E. <-- D
110
- }
111
- this.newest = entry;
112
- }
113
- /** Replace all values in this cache with key-value pairs (2-element Arrays) from provided iterable. */
114
- assign(entries) {
115
- let entry;
116
- let limit = this.limit || Number.MAX_VALUE;
117
- this._container.clear();
118
- const it = entries[Symbol.iterator]();
119
- for (let itv = it.next(); !itv.done; itv = it.next()) {
120
- const e = new Entry(itv.value[0], itv.value[1]);
121
- this._container.set(e.key, e);
122
- if (!entry) {
123
- this.oldest = e;
124
- }
125
- else {
126
- entry.newer = e;
127
- e.older = entry;
128
- }
129
- entry = e;
130
- if (limit-- === 0) {
131
- throw new Error("overflow");
132
- }
133
- }
134
- this.newest = entry;
135
- this.size = this._container.size;
136
- }
137
- /** Get and register recent use of <key>.
138
- * Returns the value associated with <key> or undefined if not in cache.
139
- */
140
- get(key) {
141
- // First, find our cache entry
142
- const entry = this._container.get(key);
143
- if (!entry)
144
- return; // Not cached. Sorry.
145
- // As <key> was found in the cache, register it as being requested recently
146
- this.markEntryAsUsed(entry);
147
- return entry.value;
148
- }
149
- /** Put <value> into the cache associated with <key>. Replaces any existing entry with the same key.
150
- * @returns `this`.
151
- */
152
- set(key, value) {
153
- let entry = this._container.get(key);
154
- if (entry) {
155
- // update existing
156
- entry.value = value;
157
- this.markEntryAsUsed(entry);
158
- return this;
159
- }
160
- // new entry
161
- this._container.set(key, (entry = new Entry(key, value)));
162
- if (this.newest) {
163
- // link previous tail to the new tail (entry)
164
- this.newest.newer = entry;
165
- entry.older = this.newest;
166
- }
167
- else {
168
- // we're first in
169
- this.oldest = entry;
170
- }
171
- // add new entry to the end of the linked list -- it is now the freshest entry.
172
- this.newest = entry;
173
- ++this.size;
174
- if (this.size > this.limit) {
175
- // we hit the limit -- remove the head
176
- this.shift();
177
- }
178
- return this;
179
- }
180
- /** Purge the least recently used (oldest) entry from the cache.
181
- * @returns The removed entry or undefined if the cache was empty.
182
- */
183
- shift() {
184
- const entry = this.oldest;
185
- if (entry) {
186
- if (entry.newer) {
187
- // advance the list
188
- this.oldest = entry.newer;
189
- this.oldest.older = undefined;
190
- }
191
- else {
192
- // the cache is exhausted
193
- this.oldest = undefined;
194
- this.newest = undefined;
195
- }
196
- // Remove last strong reference to <entry> and remove links from the purged
197
- // entry being returned:
198
- entry.newer = entry.older = undefined;
199
- this._container.delete(entry.key);
200
- --this.size;
201
- return [entry.key, entry.value];
202
- }
203
- return undefined;
204
- }
205
- /** Access value for `key` without registering recent use. Useful if you do not
206
- * want to change the state of the cache, but only "peek" at it.
207
- * @returns The value associated with `key` if found, or undefined if not found.
208
- */
209
- find(key) {
210
- const e = this._container.get(key);
211
- return e ? e.value : undefined;
212
- }
213
- /** Check if there's a value for key in the cache without registering recent use. */
214
- has(key) {
215
- return this._container.has(key);
216
- }
217
- /** Remove entry `key` from cache and return its value.
218
- * @returns The removed value, or undefined if not found.
219
- */
220
- delete(key) {
221
- const entry = this._container.get(key);
222
- if (!entry)
223
- return;
224
- this._container.delete(entry.key);
225
- if (entry.newer && entry.older) {
226
- // re-link the older entry with the newer entry
227
- entry.older.newer = entry.newer;
228
- entry.newer.older = entry.older;
229
- }
230
- else if (entry.newer) {
231
- // remove the link to us
232
- entry.newer.older = undefined;
233
- // link the newer entry to head
234
- this.oldest = entry.newer;
235
- }
236
- else if (entry.older) {
237
- // remove the link to us
238
- entry.older.newer = undefined;
239
- // link the newer entry to head
240
- this.newest = entry.older;
241
- }
242
- else { // if(entry.older === undefined && entry.newer === undefined) {
243
- this.oldest = this.newest = undefined;
244
- }
245
- this.size--;
246
- return entry.value;
247
- }
248
- /** Removes all entries */
249
- clear() {
250
- // Note: clearing links should be safe, as we don't expose live links to user
251
- this.oldest = this.newest = undefined;
252
- this.size = 0;
253
- this._container.clear();
254
- }
255
- /** Returns an iterator over all keys, starting with the oldest. */
256
- keys() {
257
- return this.oldest ? new KeyIterator(this.oldest) : undefined;
258
- }
259
- /** Returns an iterator over all values, starting with the oldest. */
260
- values() {
261
- return this.oldest ? new ValueIterator(this.oldest) : undefined;
262
- }
263
- /** Returns an iterator over all entries, starting with the oldest. */
264
- entries() {
265
- return this.oldest ? new EntryIterator(this.oldest) : undefined;
266
- }
267
- /** Call `fun` for each entry, starting with the oldest entry. */
268
- forEach(fun, thisObj) {
269
- if (typeof thisObj !== "object") {
270
- thisObj = this;
271
- }
272
- let entry = this.oldest;
273
- while (entry) {
274
- fun.call(thisObj, entry.value, entry.key, this);
275
- entry = entry.newer;
276
- }
277
- }
278
- /** Returns a JSON (array) representation */
279
- toJSON() {
280
- const s = new Array(this.size);
281
- let i = 0;
282
- let entry = this.oldest;
283
- while (entry) {
284
- s[i++] = { key: entry.key, value: entry.value };
285
- entry = entry.newer;
286
- }
287
- return s;
288
- }
289
- /** Returns a String representation */
290
- toString() {
291
- let s = "";
292
- let entry = this.oldest;
293
- while (entry) {
294
- s += `${String(entry.key)}:${entry.value}`;
295
- entry = entry.newer;
296
- if (entry) {
297
- s += " < ";
298
- }
299
- }
300
- return s;
301
- }
302
- }
303
- /** A [[LRUCache]] using a standard Map as its internal storage.
304
- * @public
305
- */
306
- export class LRUMap extends LRUCache {
307
- /**
308
- * Construct a new LRUMap to hold up to `limit` entries.
309
- */
310
- constructor(limit) {
311
- super(limit, new Map());
312
- }
313
- }
314
- /** A [[LRUCache]] using a [[Dictionary]] as its internal storage, permitting custom key comparison logic.
315
- * @public
316
- */
317
- export class LRUDictionary extends LRUCache {
318
- /**
319
- * Construct a new LRUDictionary to hold up to `limit` entries.
320
- * @param limit The maximum number of entries permitted in the dictionary.
321
- * @param compareKeys The function used to compare keys within the dictionary.
322
- */
323
- constructor(limit, compareKeys) {
324
- super(limit, new Dictionary(compareKeys));
325
- }
326
- }
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module Collections
7
+ */
8
+ import { Dictionary } from "./Dictionary";
9
+ /**
10
+ * Derived from:
11
+ * Licensed under MIT. Copyright (c) 2010 Rasmus Andersson <http://hunch.se/>
12
+ * See README.md at https://github.com/rsms/js-lru for details.
13
+ */
14
+ /** An entry holds the key and value, and pointers to any older and newer entries.
15
+ * @public
16
+ */
17
+ export class Entry {
18
+ constructor(key, value) {
19
+ this.key = key;
20
+ this.value = value;
21
+ }
22
+ }
23
+ class EntryIterator {
24
+ constructor(oldestEntry) {
25
+ this._entry = oldestEntry;
26
+ }
27
+ next() {
28
+ const ent = this._entry;
29
+ if (!ent)
30
+ return { done: true, value: undefined };
31
+ this._entry = ent.newer;
32
+ const val = [ent.key, ent.value];
33
+ return { done: false, value: val };
34
+ }
35
+ }
36
+ class KeyIterator {
37
+ constructor(oldestEntry) {
38
+ this._entry = oldestEntry;
39
+ }
40
+ next() {
41
+ const ent = this._entry;
42
+ if (!ent)
43
+ return { done: true, value: undefined };
44
+ this._entry = ent.newer;
45
+ return { done: false, value: ent.key };
46
+ }
47
+ }
48
+ class ValueIterator {
49
+ constructor(oldestEntry) {
50
+ this._entry = oldestEntry;
51
+ }
52
+ next() {
53
+ const ent = this._entry;
54
+ if (!ent)
55
+ return { done: true, value: undefined };
56
+ this._entry = ent.newer;
57
+ return { done: false, value: ent.value };
58
+ }
59
+ }
60
+ /**
61
+ * A mapping of a key/value pairs, where the size of the cache can be limited.
62
+ *
63
+ * When entries are inserted, if the cache is "full", the
64
+ * least-recently-used (LRU) value is dropped. When entries are retrieved, they are moved to the front of the LRU list.
65
+ *
66
+ * Illustration of the design:
67
+ *
68
+ * ```
69
+ *
70
+ * entry entry entry entry
71
+ * ______ ______ ______ ______
72
+ * | head |.newer => | |.newer => | |.newer => | tail |
73
+ * | A | | B | | C | | D |
74
+ * |______| <= older.|______| <= older.|______| <= older.|______|
75
+ *
76
+ * removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added
77
+ * ```
78
+ * @public
79
+ */
80
+ export class LRUCache {
81
+ /**
82
+ * Construct a new LRUCache to hold up to `limit` entries.
83
+ */
84
+ constructor(limit, container) {
85
+ this.size = 0;
86
+ this.limit = limit;
87
+ this.oldest = this.newest = undefined;
88
+ this._container = container;
89
+ }
90
+ markEntryAsUsed(entry) {
91
+ if (entry === this.newest)
92
+ return; // Already the most recently used entry, so no need to update the list
93
+ // HEAD--------------TAIL
94
+ // <.older .newer>
95
+ // <--- add direction --
96
+ // A B C <D> E
97
+ if (entry.newer) {
98
+ if (entry === this.oldest) {
99
+ this.oldest = entry.newer;
100
+ }
101
+ entry.newer.older = entry.older; // C <-- E.
102
+ }
103
+ if (entry.older) {
104
+ entry.older.newer = entry.newer; // C. --> E
105
+ }
106
+ entry.newer = undefined; // D --x
107
+ entry.older = this.newest; // D. --> E
108
+ if (this.newest) {
109
+ this.newest.newer = entry; // E. <-- D
110
+ }
111
+ this.newest = entry;
112
+ }
113
+ /** Replace all values in this cache with key-value pairs (2-element Arrays) from provided iterable. */
114
+ assign(entries) {
115
+ let entry;
116
+ let limit = this.limit || Number.MAX_VALUE;
117
+ this._container.clear();
118
+ const it = entries[Symbol.iterator]();
119
+ for (let itv = it.next(); !itv.done; itv = it.next()) {
120
+ const e = new Entry(itv.value[0], itv.value[1]);
121
+ this._container.set(e.key, e);
122
+ if (!entry) {
123
+ this.oldest = e;
124
+ }
125
+ else {
126
+ entry.newer = e;
127
+ e.older = entry;
128
+ }
129
+ entry = e;
130
+ if (limit-- === 0) {
131
+ throw new Error("overflow");
132
+ }
133
+ }
134
+ this.newest = entry;
135
+ this.size = this._container.size;
136
+ }
137
+ /** Get and register recent use of <key>.
138
+ * Returns the value associated with <key> or undefined if not in cache.
139
+ */
140
+ get(key) {
141
+ // First, find our cache entry
142
+ const entry = this._container.get(key);
143
+ if (!entry)
144
+ return; // Not cached. Sorry.
145
+ // As <key> was found in the cache, register it as being requested recently
146
+ this.markEntryAsUsed(entry);
147
+ return entry.value;
148
+ }
149
+ /** Put <value> into the cache associated with <key>. Replaces any existing entry with the same key.
150
+ * @returns `this`.
151
+ */
152
+ set(key, value) {
153
+ let entry = this._container.get(key);
154
+ if (entry) {
155
+ // update existing
156
+ entry.value = value;
157
+ this.markEntryAsUsed(entry);
158
+ return this;
159
+ }
160
+ // new entry
161
+ this._container.set(key, (entry = new Entry(key, value)));
162
+ if (this.newest) {
163
+ // link previous tail to the new tail (entry)
164
+ this.newest.newer = entry;
165
+ entry.older = this.newest;
166
+ }
167
+ else {
168
+ // we're first in
169
+ this.oldest = entry;
170
+ }
171
+ // add new entry to the end of the linked list -- it is now the freshest entry.
172
+ this.newest = entry;
173
+ ++this.size;
174
+ if (this.size > this.limit) {
175
+ // we hit the limit -- remove the head
176
+ this.shift();
177
+ }
178
+ return this;
179
+ }
180
+ /** Purge the least recently used (oldest) entry from the cache.
181
+ * @returns The removed entry or undefined if the cache was empty.
182
+ */
183
+ shift() {
184
+ const entry = this.oldest;
185
+ if (entry) {
186
+ if (entry.newer) {
187
+ // advance the list
188
+ this.oldest = entry.newer;
189
+ this.oldest.older = undefined;
190
+ }
191
+ else {
192
+ // the cache is exhausted
193
+ this.oldest = undefined;
194
+ this.newest = undefined;
195
+ }
196
+ // Remove last strong reference to <entry> and remove links from the purged
197
+ // entry being returned:
198
+ entry.newer = entry.older = undefined;
199
+ this._container.delete(entry.key);
200
+ --this.size;
201
+ return [entry.key, entry.value];
202
+ }
203
+ return undefined;
204
+ }
205
+ /** Access value for `key` without registering recent use. Useful if you do not
206
+ * want to change the state of the cache, but only "peek" at it.
207
+ * @returns The value associated with `key` if found, or undefined if not found.
208
+ */
209
+ find(key) {
210
+ const e = this._container.get(key);
211
+ return e ? e.value : undefined;
212
+ }
213
+ /** Check if there's a value for key in the cache without registering recent use. */
214
+ has(key) {
215
+ return this._container.has(key);
216
+ }
217
+ /** Remove entry `key` from cache and return its value.
218
+ * @returns The removed value, or undefined if not found.
219
+ */
220
+ delete(key) {
221
+ const entry = this._container.get(key);
222
+ if (!entry)
223
+ return;
224
+ this._container.delete(entry.key);
225
+ if (entry.newer && entry.older) {
226
+ // re-link the older entry with the newer entry
227
+ entry.older.newer = entry.newer;
228
+ entry.newer.older = entry.older;
229
+ }
230
+ else if (entry.newer) {
231
+ // remove the link to us
232
+ entry.newer.older = undefined;
233
+ // link the newer entry to head
234
+ this.oldest = entry.newer;
235
+ }
236
+ else if (entry.older) {
237
+ // remove the link to us
238
+ entry.older.newer = undefined;
239
+ // link the newer entry to head
240
+ this.newest = entry.older;
241
+ }
242
+ else { // if(entry.older === undefined && entry.newer === undefined) {
243
+ this.oldest = this.newest = undefined;
244
+ }
245
+ this.size--;
246
+ return entry.value;
247
+ }
248
+ /** Removes all entries */
249
+ clear() {
250
+ // Note: clearing links should be safe, as we don't expose live links to user
251
+ this.oldest = this.newest = undefined;
252
+ this.size = 0;
253
+ this._container.clear();
254
+ }
255
+ /** Returns an iterator over all keys, starting with the oldest. */
256
+ keys() {
257
+ return this.oldest ? new KeyIterator(this.oldest) : undefined;
258
+ }
259
+ /** Returns an iterator over all values, starting with the oldest. */
260
+ values() {
261
+ return this.oldest ? new ValueIterator(this.oldest) : undefined;
262
+ }
263
+ /** Returns an iterator over all entries, starting with the oldest. */
264
+ entries() {
265
+ return this.oldest ? new EntryIterator(this.oldest) : undefined;
266
+ }
267
+ /** Call `fun` for each entry, starting with the oldest entry. */
268
+ forEach(fun, thisObj) {
269
+ if (typeof thisObj !== "object") {
270
+ thisObj = this; // eslint-disable-line @typescript-eslint/no-this-alias
271
+ }
272
+ let entry = this.oldest;
273
+ while (entry) {
274
+ fun.call(thisObj, entry.value, entry.key, this);
275
+ entry = entry.newer;
276
+ }
277
+ }
278
+ /** Returns a JSON (array) representation */
279
+ toJSON() {
280
+ const s = new Array(this.size);
281
+ let i = 0;
282
+ let entry = this.oldest;
283
+ while (entry) {
284
+ s[i++] = { key: entry.key, value: entry.value };
285
+ entry = entry.newer;
286
+ }
287
+ return s;
288
+ }
289
+ /** Returns a String representation */
290
+ toString() {
291
+ let s = "";
292
+ let entry = this.oldest;
293
+ while (entry) {
294
+ s += `${String(entry.key)}:${entry.value}`;
295
+ entry = entry.newer;
296
+ if (entry) {
297
+ s += " < ";
298
+ }
299
+ }
300
+ return s;
301
+ }
302
+ }
303
+ /** A [[LRUCache]] using a standard Map as its internal storage.
304
+ * @public
305
+ */
306
+ export class LRUMap extends LRUCache {
307
+ /**
308
+ * Construct a new LRUMap to hold up to `limit` entries.
309
+ */
310
+ constructor(limit) {
311
+ super(limit, new Map());
312
+ }
313
+ }
314
+ /** A [[LRUCache]] using a [[Dictionary]] as its internal storage, permitting custom key comparison logic.
315
+ * @public
316
+ */
317
+ export class LRUDictionary extends LRUCache {
318
+ /**
319
+ * Construct a new LRUDictionary to hold up to `limit` entries.
320
+ * @param limit The maximum number of entries permitted in the dictionary.
321
+ * @param compareKeys The function used to compare keys within the dictionary.
322
+ */
323
+ constructor(limit, compareKeys) {
324
+ super(limit, new Dictionary(compareKeys));
325
+ }
326
+ }
327
327
  //# sourceMappingURL=LRUMap.js.map