X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcircular-array.ts;h=511b699c8827a2a214efe63e2fce9dfabfcee73a;hb=e1e012cc5e191a56cf5ec4939b3906dfc1eb3edb;hp=2ffee18e3a14652e1203a9af1f215a885917dbd4;hpb=4342a45e734bdf35316b34623176f741b2f81e86;p=poolifier.git diff --git a/src/circular-array.ts b/src/circular-array.ts index 2ffee18e..511b699c 100644 --- a/src/circular-array.ts +++ b/src/circular-array.ts @@ -1,9 +1,12 @@ -// Copyright Jerome Benoit. 2021-2023. All Rights Reserved. +// Copyright Jerome Benoit. 2021-2024. All Rights Reserved. -const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024 +export const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024 /** * Array with a maximum length and shifting items when full. + * + * @typeParam T - Type of items. + * @internal */ export class CircularArray extends Array { public size: number @@ -17,6 +20,7 @@ export class CircularArray extends Array { } } + /** @inheritDoc */ public push (...items: T[]): number { const length = super.push(...items) if (length > this.size) { @@ -25,6 +29,7 @@ export class CircularArray extends Array { return this.length } + /** @inheritDoc */ public unshift (...items: T[]): number { const length = super.unshift(...items) if (length > this.size) { @@ -33,6 +38,7 @@ export class CircularArray extends Array { return this.length } + /** @inheritDoc */ public concat (...items: Array>): CircularArray { const concatenatedCircularArray = super.concat( items as T[] @@ -47,18 +53,29 @@ export class CircularArray extends Array { return concatenatedCircularArray } - public splice (start: number, deleteCount?: number, ...items: T[]): T[] { - let itemsRemoved: T[] - if (arguments.length >= 3 && deleteCount !== undefined) { - itemsRemoved = super.splice(start, deleteCount) - // FIXME: that makes the items insert not in place - this.push(...items) + /** @inheritDoc */ + public splice ( + start: number, + deleteCount?: number, + ...items: T[] + ): CircularArray { + let itemsRemoved: T[] = [] + if (arguments.length >= 3 && deleteCount != null) { + itemsRemoved = super.splice(start, deleteCount, ...items) + if (this.length > this.size) { + const itemsOverflowing = super.splice(0, this.length - this.size) + itemsRemoved = new CircularArray( + itemsRemoved.length + itemsOverflowing.length, + ...itemsRemoved, + ...itemsOverflowing + ) + } } else if (arguments.length === 2) { itemsRemoved = super.splice(start, deleteCount) } else { itemsRemoved = super.splice(start) } - return itemsRemoved + return itemsRemoved as CircularArray } public resize (size: number): void {