remove pushRecycling, test case for trimAndRecycle, docs

This commit is contained in:
Jörg Breitbart
2018-10-26 16:11:47 +02:00
parent 982006836b
commit 93d7a3021d
4 changed files with 31 additions and 27 deletions
+16
View File
@@ -257,4 +257,20 @@ describe('CircularList', () => {
assert.equal(list.get(3), 4);
});
});
describe('trimAndRecycle', function(): void {
it('should return correct element', function(): void {
const list = new CircularList<number[]>(5);
list.push([0]);
list.push([1]);
list.push([2]);
list.push([3]);
list.push([4]);
assert.equal(list.trimAndRecycle()[0], 0);
assert.equal(list.trimAndRecycle()[0], 1);
assert.equal(list.trimAndRecycle()[0], 2);
assert.equal(list.trimAndRecycle()[0], 3);
assert.equal(list.trimAndRecycle()[0], 4);
assert.equal(list.trimAndRecycle()[0], 0);
});
});
});
+7 -16
View File
@@ -101,23 +101,14 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
}
/**
* Recycling push variant with a callback.
* The callback gets the value at the current position to be overwritten.
* Return the new value from the callback.
* Recycling trim.
* This is used to recycle buffer lines in Terminal.scroll when
* the list is at maxLength as a push replacement.
* Returns the old line as new one to be recycled.
* Note: There are no bound checks for performance reasons,
* the method is a special optimization for Terminal.scroll,
* do not use it anywhere else.
*/
public pushRecycling(callback: (item: T | undefined) => T): void {
this._array[this._getCyclicIndex(this._length)] = callback(this._array[this._getCyclicIndex(this._length)]);
if (this._length === this._maxLength) {
this._startIndex++;
if (this._startIndex === this._maxLength) {
this._startIndex = 0;
}
this.emit('trim', 1);
} else {
this._length++;
}
}
public trimAndRecycle(): T | undefined {
this._startIndex = ++this._startIndex % this._maxLength;
this.emit('trim', 1);
+1 -1
View File
@@ -28,7 +28,7 @@ export interface ICircularList<T> extends IEventEmitter {
get(index: number): T | undefined;
set(index: number, value: T): void;
push(value: T): void;
pushRecycling(callback: (item: T | undefined) => T): void;
trimAndRecycle(): T | undefined;
pop(): T | undefined;
splice(start: number, deleteCount: number, ...items: T[]): void;
trimStart(count: number): void;