sparse-array
Advanced tools
Comparing version 1.2.1 to 1.3.0
103
index.js
@@ -12,4 +12,5 @@ 'use strict' | ||
this._data = [] | ||
this._changed = false | ||
this._length = 0 | ||
this._changedLength = false | ||
this._changedData = false | ||
} | ||
@@ -25,11 +26,17 @@ | ||
this._unsetBit(index) | ||
this._changedLength = true | ||
this._changedData = true | ||
} | ||
} else { | ||
let needsSort = false | ||
if (pos === -1) { | ||
pos = this._data.length | ||
this._setBit(index) | ||
this._changedData = true | ||
} else { | ||
needsSort = true | ||
} | ||
this._setInternalPos(pos, index, value) | ||
this._setInternalPos(pos, index, value, needsSort) | ||
this._changedLength = true | ||
} | ||
this._changed = true | ||
} | ||
@@ -46,2 +53,3 @@ | ||
} | ||
this._sortData() | ||
return this._data[pos][1] | ||
@@ -56,6 +64,7 @@ } | ||
get length () { | ||
if (this._changed) { | ||
this._sortData() | ||
if (this._changedLength) { | ||
const last = this._data[this._data.length - 1] | ||
this._length = last ? last[0] + 1 : 0 | ||
this._changed = false | ||
this._changedLength = false | ||
} | ||
@@ -142,5 +151,27 @@ return this._length | ||
_setInternalPos(pos, index, elem) { | ||
this._data[pos] = [index, elem] | ||
this._data.sort(sortInternal) | ||
_setInternalPos(pos, index, value, needsSort) { | ||
const data =this._data | ||
const elem = [index, value] | ||
if (needsSort) { | ||
this._sortData() | ||
data[pos] = elem | ||
} else { | ||
// new element. just shove it into the array | ||
// but be nice about where we shove it | ||
// in order to make sorting it later easier | ||
if (data.length) { | ||
if (data[data.length - 1][0] >= index) { | ||
data.push(elem) | ||
} else if (data[0][0] <= index) { | ||
data.unshift(elem) | ||
} else { | ||
const randomIndex = Math.round(data.length / 2) | ||
this._data = data.slice(0, randomIndex).concat(elem).concat(data.slice(randomIndex)) | ||
} | ||
} else { | ||
this._data.push(elem) | ||
} | ||
this._changedData = true | ||
this._changedLength = true | ||
} | ||
} | ||
@@ -151,2 +182,54 @@ | ||
} | ||
_sortData () { | ||
if (this._changedData) { | ||
this._data.sort(sortInternal) | ||
} | ||
} | ||
bitField () { | ||
const bytes = [] | ||
let pendingBitsForResultingByte = 8 | ||
let pendingBitsForNewByte = 0 | ||
let resultingByte = 0 | ||
let newByte | ||
const pending = this._bitArrays.slice() | ||
while (pending.length || pendingBitsForNewByte) { | ||
if (pendingBitsForNewByte === 0) { | ||
newByte = pending.shift() | ||
pendingBitsForNewByte = 7 | ||
} | ||
const usingBits = Math.min(pendingBitsForNewByte, pendingBitsForResultingByte) | ||
const mask = ~(0b11111111 << usingBits) | ||
const masked = newByte & mask | ||
resultingByte |= masked << (8 - pendingBitsForResultingByte) | ||
newByte = newByte >>> usingBits | ||
pendingBitsForNewByte -= usingBits | ||
pendingBitsForResultingByte -= usingBits | ||
if (!pendingBitsForResultingByte || (!pendingBitsForNewByte && !pending.length)) { | ||
bytes.push(resultingByte) | ||
resultingByte = 0 | ||
pendingBitsForResultingByte = 8 | ||
} | ||
} | ||
// remove trailing zeroes | ||
for(var i = bytes.length - 1; i > 0; i--) { | ||
const value = bytes[i] | ||
if (value === 0) { | ||
bytes.pop() | ||
} else { | ||
break | ||
} | ||
} | ||
return bytes | ||
} | ||
compactArray () { | ||
this._sortData() | ||
return this._data.map(valueOnly) | ||
} | ||
} | ||
@@ -167,2 +250,6 @@ | ||
return a[0] - b[0] | ||
} | ||
function valueOnly (elem) { | ||
return elem[1] | ||
} |
{ | ||
"name": "sparse-array", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Sparse array implementation in JS with no dependencies", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,3 +15,3 @@ # sparse-array | ||
Create: | ||
### Create: | ||
@@ -23,3 +23,3 @@ ```js | ||
Set, get and unset: | ||
### Set, get and unset: | ||
@@ -37,3 +37,3 @@ ```js | ||
Iterate: | ||
### Iterate: | ||
@@ -54,3 +54,3 @@ ```js | ||
Find: | ||
### Find: | ||
@@ -61,5 +61,18 @@ ```js | ||
### Internal representation: | ||
#### Bit field: | ||
```js | ||
const bitField = arr.bitField() | ||
``` | ||
#### Compact array: | ||
```js | ||
const compacted = arr.compactArray() | ||
``` | ||
## License | ||
ISC |
13500
11
479
73