From 555167499ae316c4fd4859b6728fcceae2cc03d5 Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Mon, 28 Aug 2017 17:17:47 +0200 Subject: [PATCH] Bump 0.14.0 --- CHANGELOG.md | 4 +- README.md | 1 + endpoint.js | 2 + package.json | 6 ++- static-disjoint-set.js | 105 +++++++++++++++++++++++++++++------- test/static-disjoint-set.js | 34 ++++++++++++ 6 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 test/static-disjoint-set.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 74af8b44..e3de3db6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 34a0e90e..60a7b1ff 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/endpoint.js b/endpoint.js index 7567881d..d89c1395 100644 --- a/endpoint.js +++ b/endpoint.js @@ -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, diff --git a/package.json b/package.json index 6586572f..915926cf 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -22,7 +22,9 @@ "bit set", "bit array", "bit vector", + "counter", "data structures", + "disjoint set", "structures", "heap", "fibonacci heap", @@ -34,7 +36,7 @@ "bag", "multiset", "multimap", - "counter", + "union find", "sparse set", "suffix tree", "symspell", diff --git a/static-disjoint-set.js b/static-disjoint-set.js index 4fa3585e..005547d5 100644 --- a/static-disjoint-set.js +++ b/static-disjoint-set.js @@ -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; } /** @@ -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; diff --git a/test/static-disjoint-set.js b/test/static-disjoint-set.js new file mode 100644 index 00000000..985ca2a5 --- /dev/null +++ b/test/static-disjoint-set.js @@ -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]]); + }); +});