Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions bit-vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@ function createByteArray(capacity) {
*/
function BitVector(initialLengthOrOptions) {
var initialLength = initialLengthOrOptions || 0,
initialCapacity = 0,
policy = DEFAULT_GROWING_POLICY;

if (typeof initialLengthOrOptions === 'object') {
initialLength = (
initialLengthOrOptions.initialLength ||
initialLengthOrOptions.initialCapacity ||
0
);
initialCapacity = initialLengthOrOptions.initialCapacity || 0;
initialLength = typeof initialLengthOrOptions.initialLength === 'number' ?
initialLengthOrOptions.initialLength : initialCapacity;
policy = initialLengthOrOptions.policy || policy;
}

this.size = 0;
this.length = initialLength;
this.capacity = Math.ceil(this.length / 32) * 32;
this.capacity = Math.ceil(Math.max(this.length, initialCapacity) / 32) * 32;
this.policy = policy;
this.array = createByteArray(this.capacity);
}
Expand Down
32 changes: 32 additions & 0 deletions test/bit-vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ var assert = require('assert'),

describe('BitVector', function() {

it('should reserve capacity without adding bits to an explicitly empty vector.', function() {
var vector = new BitVector({initialCapacity: 65, initialLength: 0});

assert.strictEqual(vector.length, 0);
assert.strictEqual(vector.capacity, 96);
assert.strictEqual(vector.size, 0);
assert.deepStrictEqual(Array.from(vector.values()), []);
assert.strictEqual(vector.pop(), undefined);
assert.strictEqual(vector.push(1), 1);
assert.strictEqual(vector.capacity, 96);
assert.strictEqual(vector.get(0), 1);
assert.strictEqual(vector.size, 1);
});

it('should allocate for both the requested length and capacity.', function() {
var reserved = new BitVector({initialCapacity: 96, initialLength: 5}),
longer = new BitVector({initialCapacity: 32, initialLength: 65}),
empty = new BitVector({initialCapacity: 0, initialLength: 0}),
capacityOnly = new BitVector({initialCapacity: 65});

assert.strictEqual(reserved.length, 5);
assert.strictEqual(reserved.capacity, 96);
assert.strictEqual(longer.length, 65);
assert.strictEqual(longer.capacity, 96);
assert.strictEqual(empty.length, 0);
assert.strictEqual(empty.capacity, 0);
assert.strictEqual(empty.push(1), 1);
assert.strictEqual(empty.get(0), 1);
assert.strictEqual(capacityOnly.length, 65);
assert.strictEqual(capacityOnly.capacity, 96);
});

it('should be possible to create a dynamic bit vector.', function() {
var vector = new BitVector(74);

Expand Down