Add some padding and a shrink threshold to CircularList

This is needed so that resize is still performant. Now the list size is only
rebuild (O(n)) when it goes beyond 40 entries of the initial max length. The
List is also rebuilt when the size shrinks below 120 of the actual backing max
length (init_max_length + 40 - 120).
This commit is contained in:
Daniel Imms
2017-08-06 14:17:30 -07:00
parent 109d467b95
commit 27f7679494
2 changed files with 65 additions and 9 deletions
+29
View File
@@ -5,6 +5,13 @@
import { assert } from 'chai';
import { CircularList } from './CircularList';
class TestCircularList<T> extends CircularList<T> {
public get array(): T[] { return this._array; }
}
const MAX_LENGTH_PADDING = 40;
const MAX_LENGTH_SHRINK_REBUILD_THRESHOLD = 120;
describe('CircularList', () => {
describe('push', () => {
it('should push values onto the array', () => {
@@ -68,6 +75,28 @@ describe('CircularList', () => {
list.maxLength = 4;
assert.equal(list.maxLength, 4);
});
it('should resize the backing array\'s size only when it increases beyond the allocated padding', () => {
const initSize = 200;
const list = new TestCircularList<string>(initSize);
const expectedBackingMaxLength = initSize + MAX_LENGTH_PADDING;
assert.equal(list.array.length, expectedBackingMaxLength);
list.maxLength = expectedBackingMaxLength;
assert.equal(list.array.length, expectedBackingMaxLength);
list.maxLength = expectedBackingMaxLength + 1;
assert.equal(list.array.length, expectedBackingMaxLength + 1 + MAX_LENGTH_PADDING);
});
it('should resize the backing array\'s size only when it reduces beyond the shrink threshold', () => {
const initSize = 200;
const list = new TestCircularList<string>(initSize);
const expectedThresholdMaxLength = initSize + MAX_LENGTH_PADDING - MAX_LENGTH_SHRINK_REBUILD_THRESHOLD;
assert.equal(list.array.length, initSize + MAX_LENGTH_PADDING);
list.maxLength = expectedThresholdMaxLength;
assert.equal(list.array.length, initSize + MAX_LENGTH_PADDING);
list.maxLength = expectedThresholdMaxLength - 1;
assert.equal(list.array.length, expectedThresholdMaxLength - 1 + MAX_LENGTH_PADDING);
});
});
describe('length', () => {