X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcircular-array.ts;h=bb958d8287519acf91efc4c7a1468fe422da0a86;hb=251d6ac2d29d7361d671a8df17af51b414ea3b83;hp=8adf4b5fb6ec2a49b3d2e0e3e0d6fe4e6237c5a7;hpb=78099a150dc54d7adab495195fa5f133fd54e114;p=poolifier.git diff --git a/src/circular-array.ts b/src/circular-array.ts index 8adf4b5f..bb958d82 100644 --- a/src/circular-array.ts +++ b/src/circular-array.ts @@ -3,7 +3,7 @@ const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024 /** - * Array with a maximum length shifting items when full. + * Array with a maximum length and shifting items when full. */ export class CircularArray extends Array { public size: number @@ -17,6 +17,7 @@ export class CircularArray extends Array { } } + /** @inheritDoc */ public push (...items: T[]): number { const length = super.push(...items) if (length > this.size) { @@ -25,6 +26,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 +35,7 @@ export class CircularArray extends Array { return this.length } + /** @inheritDoc */ public concat (...items: Array>): CircularArray { const concatenatedCircularArray = super.concat( items as T[] @@ -47,18 +50,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 {