sparse-array
Advanced tools
Comparing version 1.0.0 to 1.1.0
46
index.js
@@ -12,2 +12,4 @@ 'use strict' | ||
this._data = [] | ||
this._changed = false | ||
this._length = 0 | ||
} | ||
@@ -30,2 +32,3 @@ | ||
} | ||
this._changed = true | ||
} | ||
@@ -45,2 +48,45 @@ | ||
push (value) { | ||
this.set(this.length, value) | ||
return this.length | ||
} | ||
get length () { | ||
if (this._changed) { | ||
this._length = this._bitArrays.reduce(popCountReduce, 0) | ||
this._changed = false | ||
} | ||
return this._length | ||
} | ||
forEach (iterator) { | ||
let i = 0 | ||
while(i < this.length) { | ||
iterator(this.get(i), i, this) | ||
i++ | ||
} | ||
} | ||
map (iterator) { | ||
let i = 0 | ||
let mapped = new Array(this.length) | ||
while(i < this.length) { | ||
mapped[i] = iterator(this.get(i), i, this) | ||
i++ | ||
} | ||
return mapped | ||
} | ||
reduce (reducer, initialValue) { | ||
let i = 0 | ||
let acc = initialValue | ||
while(i < this.length) { | ||
acc = reducer(acc, this.get(i), i) | ||
i++ | ||
} | ||
return acc | ||
} | ||
_internalPositionFor (index, noCreate) { | ||
@@ -47,0 +93,0 @@ const bytePos = this._bytePosFor(index, noCreate) |
{ | ||
"name": "sparse-array", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Sparse array implementation in JS with no dependencies", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -15,16 +15,40 @@ # sparse-array | ||
``` | ||
Create: | ||
```js | ||
const SparseArray = require('sparse-array') | ||
const arr = new SparseArray() | ||
``` | ||
Set, get and unset: | ||
```js | ||
const index = 0 | ||
arr.set(index, 'value') | ||
arr.get(index) // 'value' | ||
arr.unset(index) | ||
arr.get(index) // undefined | ||
``` | ||
Iterate: | ||
```js | ||
arr.forEach((elem, index) => { | ||
console.log('elem: %j at %d', elem, index) | ||
}) | ||
const mapped = arr.map((elem, index) => { | ||
return elem + 1 | ||
}) | ||
const result = arr.reduce((acc, elem, index) => { | ||
return acc + Number(elem) | ||
}, 0) | ||
``` | ||
## License | ||
ISC |
6960
7
229
53