structurae
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -224,3 +224,2 @@ // Type definitions for structurae | ||
select(index: number): number; | ||
private updateRanks(): void; | ||
static getLength(size: number): number; | ||
@@ -227,0 +226,0 @@ } |
/** | ||
* Uses Uint32Array as a vector or array of bits. | ||
* | ||
@@ -36,3 +37,3 @@ * @extends Uint32Array | ||
* @param {number} index | ||
* @param {number} [value] | ||
* @param {number} [value=1] | ||
* @returns {BitArray} | ||
@@ -39,0 +40,0 @@ */ |
@@ -7,3 +7,3 @@ const BitArray = require('./bit-array'); | ||
* | ||
* @extends Uint16Array | ||
* @extends BitArray | ||
*/ | ||
@@ -15,3 +15,3 @@ class RankedBitArray extends BitArray { | ||
* @param {number} index | ||
* @param {number} [value] | ||
* @param {number} [value=1] | ||
* @returns {RankedBitArray} | ||
@@ -21,3 +21,6 @@ */ | ||
super.setBit(index, value); | ||
this.updateRanks(this.lastPosition.bucket, value || -1); | ||
const change = value || -1; | ||
for (let i = (this.length >> 1) + this.lastPosition.bucket; i < this.length; i++) { | ||
this[i] += change; | ||
} | ||
return this; | ||
@@ -36,14 +39,2 @@ } | ||
/** | ||
* @private | ||
* @param {number} bucket | ||
* @param {number} value | ||
* @returns {void} | ||
*/ | ||
updateRanks(bucket, value) { | ||
for (let i = (this.length >> 1) + bucket; i < this.length; i++) { | ||
this[i] += value; | ||
} | ||
} | ||
/** | ||
* Returns the rank of a bit at a given index. | ||
@@ -50,0 +41,0 @@ * |
@@ -238,4 +238,3 @@ /** | ||
getByteOffset(index, field) { | ||
const { offset, offsets } = this; | ||
return (index << offset) + offsets[field]; | ||
return (index << this.offset) + this.offsets[field]; | ||
} | ||
@@ -268,2 +267,26 @@ | ||
/** | ||
* Stores a given object as a record at a given index. | ||
* | ||
* @param {number} index the index of the record | ||
* @param {Object} object the object to be stored | ||
* @returns {RecordArray} | ||
* @example | ||
* const people = new RecordArray([ | ||
* { name: 'age', type: 'Uint8' }, | ||
* { name: 'score', type: 'Float32' }, | ||
* ], 20); | ||
* | ||
* person.set(0, 'age', 10).set(0, 'score', 5.0).toObject(0); | ||
* //=> { age: 10, score: 5.0 } | ||
*/ | ||
fromObject(index, object) { | ||
const { fields } = this; | ||
for (let i = 0; i < fields.length; i++) { | ||
const { name } = fields[i]; | ||
if (Reflect.has(object, name)) this.set(index, name, object[name]); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Returns the length of underlying ArrayBuffer required to hold the given amount of records. | ||
@@ -270,0 +293,0 @@ * |
{ | ||
"name": "structurae", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Data structures for performance-sensitive modern JavaScript applications.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,4 +11,4 @@ # Structurae | ||
- [BitField](https://github.com/zandaqo/structurae#BitField) - stores and operates on data in Numbers and BigInts treating them as bitfields. | ||
- BitArray - an array of bits implemented with Uint32Array. | ||
- RankedBitArray - extends BitArray with O(1) time rank and O(logN) select methods. | ||
- [BitArray](https://github.com/zandaqo/structurae#BitArray) - an array of bits implemented with Uint32Array. | ||
- [RankedBitArray](https://github.com/zandaqo/structurae#RankedBitArray) - extends BitArray with O(1) time rank and O(logN) select methods. | ||
- [Graphs](https://github.com/zandaqo/structurae#Graphs): | ||
@@ -40,2 +40,3 @@ - [Graph](https://github.com/zandaqo/structurae#Graph) - extends an adjacency list/matrix structure and provides methods for traversal (BFS, DFS), | ||
- [Structurae: Data Structures for Heigh-Performance JavaScript](https://blog.usejournal.com/structurae-data-structures-for-high-performance-javascript-9b7da4c73f8) | ||
- [Structurae 1.0: Graphs, Strings, and WebAssembly](https://medium.com/@zandaqo/structurae-1-0-graphs-strings-and-webassembly-25dd964d5a70) | ||
@@ -205,2 +206,37 @@ ## Overview | ||
### BitArray | ||
BitArray uses Uint32Array as an array or vector of bits. It's a simpler version of BitField that only sets and checks individual bits: | ||
```javascript | ||
const array = new BitArray(10); | ||
array.getBit(0) | ||
//=> 0 | ||
array.setBit(0).getBit(0); | ||
//=> 1 | ||
array.size | ||
//=> 10 | ||
array.length | ||
//=> 1 | ||
``` | ||
BitArray is the base class for [Pool](https://github.com/zandaqo/structurae#Pool) and [RankedBitArray](https://github.com/zandaqo/structurae#RankedBitArray) classes. | ||
It's useful in cases where one needs more bits than can be stored in a number, but doesn't want to use BigInts as it is done by [BitField](https://github.com/zandaqo/structurae#BitField). | ||
### RankedBitArray | ||
RankedBitArray is an extension of BitArray with methods to efficiently calculate rank and select. | ||
The rank is calculated in constant time where as select has O(logN) time complexity. | ||
This is often used as a basic element in implementing succinct data structures. | ||
```javascript | ||
const array = new RankedBitArray(10); | ||
array.setBit(1).setBit(3).setBit(7); | ||
array.rank(2); | ||
//=> 1 | ||
array.rank(7); | ||
//=> 2 | ||
array.select(2); | ||
//=> 3 | ||
``` | ||
### Graphs | ||
@@ -500,2 +536,6 @@ Structurae offers classes that implement Adjacency List (`UnweightedAdjacencyList`, `WeightedAdjacencyList`) and Adjacency Matrix (`UnweightedAdjacencyMatrix`, | ||
//=> { age: 10, score: 5.0, name: '' } | ||
// populate record from an object | ||
people.fromObject(0, { age: 15, score: 6.0, name: ''}) | ||
people.toObject(0); | ||
//=> { age: 15, score: 6.0, name: '' } | ||
``` | ||
@@ -502,0 +542,0 @@ |
158528
24
4370
736