directed-graph-typed 1.53.2 → 1.53.3

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.
@@ -59,6 +59,432 @@ export declare class DoublyLinkedListNode<E = any> {
59
59
  * 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
60
60
  * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
61
61
  * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
62
+ * @example
63
+ * // text editor operation history
64
+ * const actions = [
65
+ * { type: 'insert', content: 'first line of text' },
66
+ * { type: 'insert', content: 'second line of text' },
67
+ * { type: 'delete', content: 'delete the first line' }
68
+ * ];
69
+ * const editorHistory = new DoublyLinkedList<{ type: string; content: string }>(actions);
70
+ *
71
+ * console.log(editorHistory.last?.type); // 'delete'
72
+ * console.log(editorHistory.pop()?.content); // 'delete the first line'
73
+ * console.log(editorHistory.last?.type); // 'insert'
74
+ * @example
75
+ * // Browser history
76
+ * const browserHistory = new DoublyLinkedList<string>();
77
+ *
78
+ * browserHistory.push('home page');
79
+ * browserHistory.push('search page');
80
+ * browserHistory.push('details page');
81
+ *
82
+ * console.log(browserHistory.last); // 'details page'
83
+ * console.log(browserHistory.pop()); // 'details page'
84
+ * console.log(browserHistory.last); // 'search page'
85
+ * @example
86
+ * // Use DoublyLinkedList to implement music player
87
+ * // Define the Song interface
88
+ * interface Song {
89
+ * title: string;
90
+ * artist: string;
91
+ * duration: number; // duration in seconds
92
+ * }
93
+ *
94
+ * class Player {
95
+ * private playlist: DoublyLinkedList<Song>;
96
+ * private currentSong: ReturnType<typeof this.playlist.getNodeAt> | undefined;
97
+ *
98
+ * constructor(songs: Song[]) {
99
+ * this.playlist = new DoublyLinkedList<Song>();
100
+ * songs.forEach(song => this.playlist.push(song));
101
+ * this.currentSong = this.playlist.head;
102
+ * }
103
+ *
104
+ * // Play the next song in the playlist
105
+ * playNext(): Song | undefined {
106
+ * if (!this.currentSong?.next) {
107
+ * this.currentSong = this.playlist.head; // Loop to the first song
108
+ * } else {
109
+ * this.currentSong = this.currentSong.next;
110
+ * }
111
+ * return this.currentSong?.value;
112
+ * }
113
+ *
114
+ * // Play the previous song in the playlist
115
+ * playPrevious(): Song | undefined {
116
+ * if (!this.currentSong?.prev) {
117
+ * this.currentSong = this.playlist.tail; // Loop to the last song
118
+ * } else {
119
+ * this.currentSong = this.currentSong.prev;
120
+ * }
121
+ * return this.currentSong?.value;
122
+ * }
123
+ *
124
+ * // Get the current song
125
+ * getCurrentSong(): Song | undefined {
126
+ * return this.currentSong?.value;
127
+ * }
128
+ *
129
+ * // Loop through the playlist twice
130
+ * loopThroughPlaylist(): Song[] {
131
+ * const playedSongs: Song[] = [];
132
+ * const initialNode = this.currentSong;
133
+ *
134
+ * // Loop through the playlist twice
135
+ * for (let i = 0; i < this.playlist.size * 2; i++) {
136
+ * playedSongs.push(this.currentSong!.value);
137
+ * this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed
138
+ * }
139
+ *
140
+ * // Reset the current song to the initial song
141
+ * this.currentSong = initialNode;
142
+ * return playedSongs;
143
+ * }
144
+ * }
145
+ *
146
+ * const songs = [
147
+ * { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
148
+ * { title: 'Hotel California', artist: 'Eagles', duration: 391 },
149
+ * { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
150
+ * { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
151
+ * ];
152
+ * let player = new Player(songs);
153
+ * // should play the next song
154
+ * player = new Player(songs);
155
+ * const firstSong = player.getCurrentSong();
156
+ * const nextSong = player.playNext();
157
+ *
158
+ * // Expect the next song to be "Hotel California by Eagles"
159
+ * console.log(nextSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
160
+ * console.log(firstSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
161
+ *
162
+ * // should play the previous song
163
+ * player = new Player(songs);
164
+ * player.playNext(); // Move to the second song
165
+ * const currentSong = player.getCurrentSong();
166
+ * const previousSong = player.playPrevious();
167
+ *
168
+ * // Expect the previous song to be "Bohemian Rhapsody by Queen"
169
+ * console.log(previousSong); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
170
+ * console.log(currentSong); // { title: 'Hotel California', artist: 'Eagles', duration: 391 }
171
+ *
172
+ * // should loop to the first song when playing next from the last song
173
+ * player = new Player(songs);
174
+ * player.playNext(); // Move to the second song
175
+ * player.playNext(); // Move to the third song
176
+ * player.playNext(); // Move to the fourth song
177
+ *
178
+ * const nextSongToFirst = player.playNext(); // Should loop to the first song
179
+ *
180
+ * // Expect the next song to be "Bohemian Rhapsody by Queen"
181
+ * console.log(nextSongToFirst); // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 }
182
+ *
183
+ * // should loop to the last song when playing previous from the first song
184
+ * player = new Player(songs);
185
+ * player.playNext(); // Move to the first song
186
+ * player.playNext(); // Move to the second song
187
+ * player.playNext(); // Move to the third song
188
+ * player.playNext(); // Move to the fourth song
189
+ *
190
+ * const previousToLast = player.playPrevious(); // Should loop to the last song
191
+ *
192
+ * // Expect the previous song to be "Billie Jean by Michael Jackson"
193
+ * console.log(previousToLast); // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
194
+ *
195
+ * // should loop through the entire playlist
196
+ * player = new Player(songs);
197
+ * const playedSongs = player.loopThroughPlaylist();
198
+ *
199
+ * // The expected order of songs for two loops
200
+ * console.log(playedSongs); // [
201
+ * // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
202
+ * // { title: 'Hotel California', artist: 'Eagles', duration: 391 },
203
+ * // { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
204
+ * // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 },
205
+ * // { title: 'Bohemian Rhapsody', artist: 'Queen', duration: 354 },
206
+ * // { title: 'Hotel California', artist: 'Eagles', duration: 391 },
207
+ * // { title: 'Shape of You', artist: 'Ed Sheeran', duration: 233 },
208
+ * // { title: 'Billie Jean', artist: 'Michael Jackson', duration: 294 }
209
+ * // ]
210
+ * @example
211
+ * // Use DoublyLinkedList to implement LRU cache
212
+ * interface CacheEntry<K, V> {
213
+ * key: K;
214
+ * value: V;
215
+ * }
216
+ *
217
+ * class LRUCache<K = string, V = any> {
218
+ * private readonly capacity: number;
219
+ * private list: DoublyLinkedList<CacheEntry<K, V>>;
220
+ * private map: Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>;
221
+ *
222
+ * constructor(capacity: number) {
223
+ * if (capacity <= 0) {
224
+ * throw new Error('lru cache capacity must be greater than 0');
225
+ * }
226
+ * this.capacity = capacity;
227
+ * this.list = new DoublyLinkedList<CacheEntry<K, V>>();
228
+ * this.map = new Map<K, DoublyLinkedListNode<CacheEntry<K, V>>>();
229
+ * }
230
+ *
231
+ * // Get cached value
232
+ * get(key: K): V | undefined {
233
+ * const node = this.map.get(key);
234
+ *
235
+ * if (!node) return undefined;
236
+ *
237
+ * // Move the visited node to the head of the linked list (most recently used)
238
+ * this.moveToFront(node);
239
+ *
240
+ * return node.value.value;
241
+ * }
242
+ *
243
+ * // Set cache value
244
+ * set(key: K, value: V): void {
245
+ * // Check if it already exists
246
+ * const node = this.map.get(key);
247
+ *
248
+ * if (node) {
249
+ * // Update value and move to head
250
+ * node.value.value = value;
251
+ * this.moveToFront(node);
252
+ * return;
253
+ * }
254
+ *
255
+ * // Check capacity
256
+ * if (this.list.size >= this.capacity) {
257
+ * // Delete the least recently used element (the tail of the linked list)
258
+ * const removedNode = this.list.tail;
259
+ * if (removedNode) {
260
+ * this.map.delete(removedNode.value.key);
261
+ * this.list.pop();
262
+ * }
263
+ * }
264
+ *
265
+ * // Create new node and add to head
266
+ * const newEntry: CacheEntry<K, V> = { key, value };
267
+ * this.list.unshift(newEntry);
268
+ *
269
+ * // Save node reference in map
270
+ * const newNode = this.list.head;
271
+ * if (newNode) {
272
+ * this.map.set(key, newNode);
273
+ * }
274
+ * }
275
+ *
276
+ * // Move the node to the head of the linked list
277
+ * private moveToFront(node: DoublyLinkedListNode<CacheEntry<K, V>>): void {
278
+ * this.list.delete(node);
279
+ * this.list.unshift(node.value);
280
+ * }
281
+ *
282
+ * // Delete specific key
283
+ * delete(key: K): boolean {
284
+ * const node = this.map.get(key);
285
+ * if (!node) return false;
286
+ *
287
+ * // Remove from linked list
288
+ * this.list.delete(node);
289
+ * // Remove from map
290
+ * this.map.delete(key);
291
+ *
292
+ * return true;
293
+ * }
294
+ *
295
+ * // Clear cache
296
+ * clear(): void {
297
+ * this.list.clear();
298
+ * this.map.clear();
299
+ * }
300
+ *
301
+ * // Get the current cache size
302
+ * get size(): number {
303
+ * return this.list.size;
304
+ * }
305
+ *
306
+ * // Check if it is empty
307
+ * get isEmpty(): boolean {
308
+ * return this.list.isEmpty();
309
+ * }
310
+ * }
311
+ *
312
+ * // should set and get values correctly
313
+ * const cache = new LRUCache<string, number>(3);
314
+ * cache.set('a', 1);
315
+ * cache.set('b', 2);
316
+ * cache.set('c', 3);
317
+ *
318
+ * console.log(cache.get('a')); // 1
319
+ * console.log(cache.get('b')); // 2
320
+ * console.log(cache.get('c')); // 3
321
+ *
322
+ * // The least recently used element should be evicted when capacity is exceeded
323
+ * cache.clear();
324
+ * cache.set('a', 1);
325
+ * cache.set('b', 2);
326
+ * cache.set('c', 3);
327
+ * cache.set('d', 4); // This will eliminate 'a'
328
+ *
329
+ * console.log(cache.get('a')).toBeUndefined();
330
+ * expect(cache.get('b')); // 2
331
+ * console.log(cache.get('c')); // 3
332
+ * console.log(cache.get('d')); // 4
333
+ *
334
+ * // The priority of an element should be updated when it is accessed
335
+ * cache.clear();
336
+ * cache.set('a', 1);
337
+ * cache.set('b', 2);
338
+ * cache.set('c', 3);
339
+ *
340
+ * cache.get('a'); // access 'a'
341
+ * cache.set('d', 4); // This will eliminate 'b'
342
+ *
343
+ * console.log(cache.get('a')); // 1
344
+ * console.log(cache.get('b')).toBeUndefined();
345
+ * expect(cache.get('c')); // 3
346
+ * console.log(cache.get('d')); // 4
347
+ *
348
+ * // Should support updating existing keys
349
+ * cache.clear();
350
+ * cache.set('a', 1);
351
+ * cache.set('a', 10);
352
+ *
353
+ * console.log(cache.get('a')); // 10
354
+ *
355
+ * // Should support deleting specified keys
356
+ * cache.clear();
357
+ * cache.set('a', 1);
358
+ * cache.set('b', 2);
359
+ *
360
+ * console.log(cache.delete('a')); // true
361
+ * console.log(cache.get('a')).toBeUndefined();
362
+ * expect(cache.size); // 1
363
+ *
364
+ * // Should support clearing cache
365
+ * cache.clear();
366
+ * cache.set('a', 1);
367
+ * cache.set('b', 2);
368
+ * cache.clear();
369
+ *
370
+ * console.log(cache.size); // 0
371
+ * console.log(cache.isEmpty); // true
372
+ * @example
373
+ * // finding lyrics by timestamp in Coldplay's "Fix You"
374
+ * // Create a DoublyLinkedList to store song lyrics with timestamps
375
+ * const lyricsList = new DoublyLinkedList<{ time: number; text: string }>();
376
+ *
377
+ * // Detailed lyrics with precise timestamps (in milliseconds)
378
+ * const lyrics = [
379
+ * { time: 0, text: "When you try your best, but you don't succeed" },
380
+ * { time: 4000, text: 'When you get what you want, but not what you need' },
381
+ * { time: 8000, text: "When you feel so tired, but you can't sleep" },
382
+ * { time: 12000, text: 'Stuck in reverse' },
383
+ * { time: 16000, text: 'And the tears come streaming down your face' },
384
+ * { time: 20000, text: "When you lose something you can't replace" },
385
+ * { time: 24000, text: 'When you love someone, but it goes to waste' },
386
+ * { time: 28000, text: 'Could it be worse?' },
387
+ * { time: 32000, text: 'Lights will guide you home' },
388
+ * { time: 36000, text: 'And ignite your bones' },
389
+ * { time: 40000, text: 'And I will try to fix you' }
390
+ * ];
391
+ *
392
+ * // Populate the DoublyLinkedList with lyrics
393
+ * lyrics.forEach(lyric => lyricsList.push(lyric));
394
+ *
395
+ * // Test different scenarios of lyric synchronization
396
+ *
397
+ * // 1. Find lyric at exact timestamp
398
+ * const exactTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 36000);
399
+ * console.log(exactTimeLyric?.text); // 'And ignite your bones'
400
+ *
401
+ * // 2. Find lyric between timestamps
402
+ * const betweenTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 22000);
403
+ * console.log(betweenTimeLyric?.text); // "When you lose something you can't replace"
404
+ *
405
+ * // 3. Find first lyric when timestamp is less than first entry
406
+ * const earlyTimeLyric = lyricsList.findBackward(lyric => lyric.time <= -1000);
407
+ * console.log(earlyTimeLyric).toBeUndefined();
408
+ *
409
+ * // 4. Find last lyric when timestamp is after last entry
410
+ * const lateTimeLyric = lyricsList.findBackward(lyric => lyric.time <= 50000);
411
+ * expect(lateTimeLyric?.text); // 'And I will try to fix you'
412
+ * @example
413
+ * // cpu process schedules
414
+ * class Process {
415
+ * constructor(
416
+ * public id: number,
417
+ * public priority: number
418
+ * ) {}
419
+ *
420
+ * execute(): string {
421
+ * return `Process ${this.id} executed.`;
422
+ * }
423
+ * }
424
+ *
425
+ * class Scheduler {
426
+ * private queue: DoublyLinkedList<Process>;
427
+ *
428
+ * constructor() {
429
+ * this.queue = new DoublyLinkedList<Process>();
430
+ * }
431
+ *
432
+ * addProcess(process: Process): void {
433
+ * // Insert processes into a queue based on priority, keeping priority in descending order
434
+ * let current = this.queue.head;
435
+ * while (current && current.value.priority >= process.priority) {
436
+ * current = current.next;
437
+ * }
438
+ *
439
+ * if (!current) {
440
+ * this.queue.push(process);
441
+ * } else {
442
+ * this.queue.addBefore(current, process);
443
+ * }
444
+ * }
445
+ *
446
+ * executeNext(): string | undefined {
447
+ * // Execute tasks at the head of the queue in order
448
+ * const process = this.queue.shift();
449
+ * return process ? process.execute() : undefined;
450
+ * }
451
+ *
452
+ * listProcesses(): string[] {
453
+ * return this.queue.toArray().map(process => `Process ${process.id} (Priority: ${process.priority})`);
454
+ * }
455
+ *
456
+ * clear(): void {
457
+ * this.queue.clear();
458
+ * }
459
+ * }
460
+ *
461
+ * // should add processes based on priority
462
+ * let scheduler = new Scheduler();
463
+ * scheduler.addProcess(new Process(1, 10));
464
+ * scheduler.addProcess(new Process(2, 20));
465
+ * scheduler.addProcess(new Process(3, 15));
466
+ *
467
+ * console.log(scheduler.listProcesses()); // [
468
+ * // 'Process 2 (Priority: 20)',
469
+ * // 'Process 3 (Priority: 15)',
470
+ * // 'Process 1 (Priority: 10)'
471
+ * // ]
472
+ *
473
+ * // should execute the highest priority process
474
+ * scheduler = new Scheduler();
475
+ * scheduler.addProcess(new Process(1, 10));
476
+ * scheduler.addProcess(new Process(2, 20));
477
+ *
478
+ * console.log(scheduler.executeNext()); // 'Process 2 executed.'
479
+ * console.log(scheduler.listProcesses()); // ['Process 1 (Priority: 10)']
480
+ *
481
+ * // should clear all processes
482
+ * scheduler = new Scheduler();
483
+ * scheduler.addProcess(new Process(1, 10));
484
+ * scheduler.addProcess(new Process(2, 20));
485
+ *
486
+ * scheduler.clear();
487
+ * console.log(scheduler.listProcesses()); // []
62
488
  */
63
489
  export declare class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R, DoublyLinkedList<E, R>> {
64
490
  constructor(elements?: Iterable<E> | Iterable<R>, options?: DoublyLinkedListOptions<E, R>);