socket-function 0.44.0 → 0.46.0
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.
- package/package.json +1 -1
- package/src/misc.ts +20 -7
package/package.json
CHANGED
package/src/misc.ts
CHANGED
|
@@ -295,23 +295,36 @@ export function sort<T>(arr: T[], sortKey: (obj: T) => unknown) {
|
|
|
295
295
|
|
|
296
296
|
export class QueueLimited<T> {
|
|
297
297
|
private items: T[] = [];
|
|
298
|
-
private
|
|
298
|
+
private nextIndex = 0;
|
|
299
299
|
constructor(private readonly maxCount: number) { }
|
|
300
300
|
|
|
301
|
-
public push(item: T) {
|
|
302
|
-
this.
|
|
303
|
-
this.
|
|
301
|
+
public push(item: T): void {
|
|
302
|
+
this.items[this.nextIndex] = item;
|
|
303
|
+
this.nextIndex = (this.nextIndex + 1) % this.maxCount;
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
-
public getAllUnordered() {
|
|
306
|
+
public getAllUnordered(): T[] {
|
|
307
307
|
return this.items;
|
|
308
308
|
}
|
|
309
|
-
public reset() {
|
|
309
|
+
public reset(): void {
|
|
310
310
|
this.items = [];
|
|
311
|
-
this.
|
|
311
|
+
this.nextIndex = 0;
|
|
312
|
+
}
|
|
313
|
+
public clear(): void {
|
|
314
|
+
this.reset();
|
|
315
|
+
}
|
|
316
|
+
public getOldest(): T | undefined {
|
|
317
|
+
if (this.items.length === 0) return undefined;
|
|
318
|
+
let index = this.nextIndex - 1;
|
|
319
|
+
|
|
320
|
+
if (index === -1) {
|
|
321
|
+
index += this.maxCount;
|
|
322
|
+
}
|
|
323
|
+
return this.items[index];
|
|
312
324
|
}
|
|
313
325
|
}
|
|
314
326
|
|
|
327
|
+
|
|
315
328
|
export function binarySearchBasic<T, V>(array: T[], getVal: (val: T) => V, searchValue: V): number {
|
|
316
329
|
return binarySearchIndex(array.length, i => compare(getVal(array[i]), searchValue));
|
|
317
330
|
}
|