Skip to content

Commit

Permalink
Bump 0.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Aug 28, 2017
1 parent 07a6e9f commit 5551674
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Changelog

## O.14.0 (provisional)
## O.14.0

* Adding `DynamicArray`.
* Adding `SparseSet`.
* Adding `StaticDisjointSet`.
* Adding iterator methods to `BitSet`.
* Adding `#.rank` & `#.select` to `BitSet`.
* `BitSet` now relies on `Uint32Array` rather than `Uint8Array`.
* Improving `BitSet` performances.
* Using `obliterator` to handle iterators.

## 0.13.0

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Full documentation for the library can be found [here](https://yomguithereal.git
* [BitSet](https://yomguithereal.github.io/mnemonist/bit-set)
* [Bloom Filter](https://yomguithereal.github.io/mnemonist/bloom-filter)
* [Burkhard-Keller Tree](https://yomguithereal.github.io/mnemonist/bk-tree)
* [StaticDisjointSet](https://yomguithereal.github.io/mnemonist/static-disjoint-set)
* [Dynamic Arrays](https://yomguithereal.github.io/mnemonist/dynamic-array)
* [Fibonacci Heap](https://yomguithereal.github.io/mnemonist/fibonacci-heap)
* [Heap](https://yomguithereal.github.io/mnemonist/heap)
Expand Down
2 changes: 2 additions & 0 deletions endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = {
BitSet: require('./bit-set.js'),
BloomFilter: require('./bloom-filter.js'),
BKTree: require('./bk-tree.js'),
StaticDisjointSet: require('./static-disjoint-set.js'),
DynamicArray: require('./dynamic-array.js'),
FibonacciHeap: FibonacciHeap,
MinFibonacciHeap: FibonacciHeap.MinFibonacciHeap,
MaxFibonacciHeap: FibonacciHeap.MaxFibonacciHeap,
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mnemonist",
"version": "0.13.0",
"version": "0.14.0",
"description": "Curated collection of data structures for the JavaScript language.",
"main": "endpoint.js",
"scripts": {
Expand All @@ -22,7 +22,9 @@
"bit set",
"bit array",
"bit vector",
"counter",
"data structures",
"disjoint set",
"structures",
"heap",
"fibonacci heap",
Expand All @@ -34,7 +36,7 @@
"bag",
"multiset",
"multimap",
"counter",
"union find",
"sparse set",
"suffix tree",
"symspell",
Expand Down
105 changes: 85 additions & 20 deletions static-disjoint-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function StaticDisjointSet(size) {
this.dimension = size;
this.parents = new ParentsTypedArray(size);
this.ranks = new RanksTypedArray(size);

// Initializing parents
for (var i = 0; i < size; i++)
this.parents[i] = i;
}

/**
Expand Down Expand Up @@ -97,31 +101,92 @@ StaticDisjointSet.prototype.union = function(x, y) {
return this;
};

// TODO: possible to track size of sets
/**
* Method returning whether two items are connected.
*
* @param {number} x - First item.
* @param {number} y - Second item.
* @return {boolean}
*/
StaticDisjointSet.prototype.connected = function(x, y) {
var xRoot = this.find(x);

return xRoot === this.find(y);
};

/**
* Method returning the set mapping.
*
* @return {TypedArray}
*/
StaticDisjointSet.prototype.mapping = function() {
var MappingClass = helpers.getPointerArray(this.dimension);

var ids = {},
mapping = new MappingClass(this.size),
c = 0;

var r;

for (var i = 0, l = this.parents.length; i < l; i++) {
r = this.find(i);

if (typeof ids[r] === 'undefined') {
mapping[i] = c;
ids[r] = c++;
}
else {
mapping[i] = ids[r];
}
}

return mapping;
};

/**
* Method used to compile the disjoint set into an array of arrays.
*
* @return {array}
*/
StaticDisjointSet.prototype.compile = function() {
var ids = {},
result = [],
c = 0;

var r;

for (var i = 0, l = this.parents.length; i < l; i++) {
r = this.find(i);

if (typeof ids[r] === 'undefined') {
result[c] = [i];
ids[r] = c++;
}
else {
result[ids[r]].push(i);
}
}

// i = 0;
// map = {};
// result = new Array(this.dimension);
// for set in sets:
// if not root:
// continue
// result[i] = []
// map[i++] = root
return result;
};

// for set in sets:
// root = find(set)
// result[map[root]].push(set)
/**
* Convenience known methods.
*/
StaticDisjointSet.prototype.inspect = function() {
var array = this.compile();

// return result
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: StaticDisjointSet,
enumerable: false
});

return array;
};

// TODO: method returning sets map
// TODO: method to iterate over sets (need to use a linked multimap)
// TODO: method to test connectivity
// TODO: possibility to iterate on sets using a heap (convoluted)

/**
* Exporting.
*/
module.exports = {
StaticDisjointSet: StaticDisjointSet
};
module.exports = StaticDisjointSet;
34 changes: 34 additions & 0 deletions test/static-disjoint-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Mnemonist SparseSet Unit Tests
* ===============================
*/
var assert = require('assert'),
StaticDisjointSet = require('../static-disjoint-set.js');

describe('StaticDisjointSet', function() {

it('should be possible to have a set working.', function() {
var sets = new StaticDisjointSet(10);

sets.union(0, 1);
sets.union(1, 5);
sets.union(0, 7);

sets.union(8, 9);

sets.union(2, 3);
sets.union(2, 4);

assert.strictEqual(sets.size, 10);
assert.strictEqual(sets.dimension, 4);
assert.strictEqual(sets.connected(1, 7), true);
assert.strictEqual(sets.connected(6, 0), false);

var mapping = sets.mapping();

assert.deepEqual(mapping, [0, 0, 1, 1, 1, 0, 2, 0, 3, 3]);

var compiled = sets.compile();
assert.deepEqual(compiled, [[0, 1, 5, 7], [2, 3, 4], [6], [8, 9]]);
});
});

0 comments on commit 5551674

Please sign in to comment.