|
6 | 6 | import { Emitter, Event } from 'vs/base/common/event';
|
7 | 7 | import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
|
8 | 8 |
|
| 9 | +interface PriorityQueue<T> { |
| 10 | + length: number; |
| 11 | + add(value: T): void; |
| 12 | + remove(value: T): void; |
| 13 | + |
| 14 | + removeMin(): T | undefined; |
| 15 | + toSortedArray(): T[]; |
| 16 | +} |
| 17 | + |
| 18 | +class SimplePriorityQueue<T> implements PriorityQueue<T> { |
| 19 | + private isSorted = false; |
| 20 | + private items: T[]; |
| 21 | + |
| 22 | + constructor(items: T[], private readonly compare: (a: T, b: T) => number) { |
| 23 | + this.items = items; |
| 24 | + } |
| 25 | + |
| 26 | + get length(): number { |
| 27 | + return this.items.length; |
| 28 | + } |
| 29 | + |
| 30 | + add(value: T): void { |
| 31 | + this.items.push(value); |
| 32 | + this.isSorted = false; |
| 33 | + } |
| 34 | + |
| 35 | + remove(value: T): void { |
| 36 | + this.items.splice(this.items.indexOf(value), 1); |
| 37 | + this.isSorted = false; |
| 38 | + } |
| 39 | + |
| 40 | + removeMin(): T | undefined { |
| 41 | + this.ensureSorted(); |
| 42 | + return this.items.shift(); |
| 43 | + } |
| 44 | + |
| 45 | + getMin(): T | undefined { |
| 46 | + this.ensureSorted(); |
| 47 | + return this.items[0]; |
| 48 | + } |
| 49 | + |
| 50 | + toSortedArray(): T[] { |
| 51 | + this.ensureSorted(); |
| 52 | + return [...this.items]; |
| 53 | + } |
| 54 | + |
| 55 | + private ensureSorted() { |
| 56 | + if (!this.isSorted) { |
| 57 | + this.items.sort(this.compare); |
| 58 | + this.isSorted = true; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
9 | 63 | export type TimeOffset = number;
|
10 | 64 |
|
11 | 65 | export interface Scheduler {
|
@@ -326,57 +380,3 @@ function createDateClass(scheduler: Scheduler): DateConstructor {
|
326 | 380 |
|
327 | 381 | return SchedulerDate as any;
|
328 | 382 | }
|
329 |
| - |
330 |
| -interface PriorityQueue<T> { |
331 |
| - length: number; |
332 |
| - add(value: T): void; |
333 |
| - remove(value: T): void; |
334 |
| - |
335 |
| - removeMin(): T | undefined; |
336 |
| - toSortedArray(): T[]; |
337 |
| -} |
338 |
| - |
339 |
| -class SimplePriorityQueue<T> implements PriorityQueue<T> { |
340 |
| - private isSorted = false; |
341 |
| - private items: T[]; |
342 |
| - |
343 |
| - constructor(items: T[], private readonly compare: (a: T, b: T) => number) { |
344 |
| - this.items = items; |
345 |
| - } |
346 |
| - |
347 |
| - get length(): number { |
348 |
| - return this.items.length; |
349 |
| - } |
350 |
| - |
351 |
| - add(value: T): void { |
352 |
| - this.items.push(value); |
353 |
| - this.isSorted = false; |
354 |
| - } |
355 |
| - |
356 |
| - remove(value: T): void { |
357 |
| - this.items.splice(this.items.indexOf(value), 1); |
358 |
| - this.isSorted = false; |
359 |
| - } |
360 |
| - |
361 |
| - removeMin(): T | undefined { |
362 |
| - this.ensureSorted(); |
363 |
| - return this.items.shift(); |
364 |
| - } |
365 |
| - |
366 |
| - getMin(): T | undefined { |
367 |
| - this.ensureSorted(); |
368 |
| - return this.items[0]; |
369 |
| - } |
370 |
| - |
371 |
| - toSortedArray(): T[] { |
372 |
| - this.ensureSorted(); |
373 |
| - return [...this.items]; |
374 |
| - } |
375 |
| - |
376 |
| - private ensureSorted() { |
377 |
| - if (!this.isSorted) { |
378 |
| - this.items.sort(this.compare); |
379 |
| - this.isSorted = true; |
380 |
| - } |
381 |
| - } |
382 |
| -} |
0 commit comments