persistent-hash-trie
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -12,2 +12,3 @@ 'use strict' | ||
var keys = require('./keys') | ||
var hash = require('./hash') | ||
@@ -45,4 +46,17 @@ var suites = [ | ||
keys(1000) | ||
// hash(1), | ||
// hash(3), | ||
// hash(5), | ||
// hash(10), | ||
// hash(50), | ||
// hash(100), | ||
// hash(500), | ||
// hash(1000), | ||
// hash(5000), | ||
// hash(10000), | ||
// hash(50000), | ||
// hash(100000) | ||
] | ||
_.each(suites, run) |
@@ -28,6 +28,14 @@ var _ = require('lodash') | ||
var makeInt = function(){ | ||
var len = randInt(10) | ||
return repeat(len, function(){ return randInt(9) }).join('') | ||
} | ||
module.exports = { | ||
words: function(len){ | ||
return repeat(len, makeWord) | ||
}, | ||
ints: function(len){ | ||
return repeat(len, makeInt) | ||
} | ||
} |
'use strict' | ||
var log = console.log.bind(console) | ||
var formatNumber = require('format-number')({ truncate: 2 }) | ||
@@ -9,6 +10,5 @@ var print = function(){ | ||
log('//-------------------------//') | ||
log('Results:') | ||
this.map(function(results){ | ||
log(results.name + ' : ' + results.count + ' ± ' + results.stats.rme + '% ops/sec') | ||
log(results.name + ' : ' + formatNumber(results.hz) + ' ± ' + formatNumber(results.stats.rme) + '% ops/sec') | ||
}) | ||
@@ -15,0 +15,0 @@ } |
module.exports = [ | ||
{ name: 'shallow-copy reference ', module: require('./reference-versions/shallow-copy.js') }, | ||
{ name: 'current ', module: require('..') }, | ||
{ name: 'v0.2.3 ', module: require('./previous-versions/0.2.3/') }, | ||
{ name: 'v0.2.4 ', module: require('./previous-versions/0.2.4/') }, | ||
{ name: 'v0.3.0 ', module: require('./previous-versions/0.3.0/') } | ||
// { name: 'current ', module: require('..') }, | ||
{ name: 'v0.2.1 ', module: require('./previous-versions/0.2.1/') }, | ||
// { name: 'v0.2.2 ', module: require('./previous-versions/0.2.2/') }, | ||
// { name: 'v0.2.3 ', module: require('./previous-versions/0.2.3/') }, | ||
// { name: 'v0.2.4 ', module: require('./previous-versions/0.2.4/') }, | ||
// { name: 'v0.3.0 ', module: require('./previous-versions/0.3.0/') }, | ||
{ name: 'v0.3.1 ', module: require('./previous-versions/0.3.1/') } | ||
] |
@@ -5,3 +5,3 @@ | ||
"description": "Pure string:val storage, using structural sharing", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"main": "src/persistent-hash-trie.js", | ||
@@ -13,2 +13,5 @@ "scripts": { | ||
}, | ||
"dependencies": { | ||
"string-hash": "1.1.0" | ||
}, | ||
"devDependencies": { | ||
@@ -20,3 +23,4 @@ "mocha": "1.8.1", | ||
"better-stack-traces": "0.0.4", | ||
"string-hash": "1.1.0" | ||
"string-hash": "1.1.0", | ||
"format-number": "0.0.0" | ||
}, | ||
@@ -39,4 +43,2 @@ "repository": { | ||
"firefox/17.0", | ||
"opera/10.0", | ||
"opera/12.0", | ||
"safari/5.0.5", | ||
@@ -43,0 +45,0 @@ "safari/5.1" |
'use strict' | ||
var util = require('./util') | ||
var hash = require('./hash') | ||
var lib = module.exports | ||
@@ -37,27 +39,4 @@ //# persistent Hash Trie | ||
//# Hashing functions | ||
// Int, Int -> Int | ||
// gets a <= 5 bit section of a hash, shifted from the left position | ||
// in practice, a 32 bit splits into 7 chunks - 6 of 5 bits, one of 2 | ||
var mask = function(hash, from){ return (hash >>> from) & 0x01f } | ||
// String, Int, Function -> Int | ||
// gets a chunk of a hash, given a string and a hashing function | ||
// the hashing function should return a 32 bit hash. | ||
var hashMask = function(str, from, hash){ | ||
return mask(hash(str), from) | ||
} | ||
// hash function for strings, based on Java's String.hashCode: | ||
// http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#hashCode() | ||
var hash = function(str){ | ||
var h = 0 | ||
var l = str.length | ||
for ( var i = 0; i < l; i += 1 ) | ||
h = h * 31 + str.charCodeAt(i) | ||
return h | ||
} | ||
// to allow hooks for other implementations/tests to override the default | ||
@@ -71,3 +50,3 @@ // hash and equality functions (which are the necessary ones for creating | ||
eq : function(a, b){ return a === b }, | ||
hash : hash | ||
hash : hash.hash | ||
} | ||
@@ -79,3 +58,3 @@ | ||
// a Trie is a store of children values; the most basic type of non-leaf node. | ||
var Trie = function(children){ | ||
var Trie = lib.Trie = function(children){ | ||
return { type: 'trie', children: children || {} } | ||
@@ -86,3 +65,3 @@ } | ||
// Node that represents a specific value | ||
var Value = function(key, value){ | ||
var Value = lib.Value = function(key, value){ | ||
return { type: 'value', key: key, value: value } | ||
@@ -94,3 +73,3 @@ } | ||
// values will just slung into a hashmap node | ||
var Hashmap = function(values){ | ||
var Hashmap = lib.Hashmap = function(values){ | ||
return { type: 'hashmap', values: values } | ||
@@ -133,5 +112,8 @@ } | ||
// then it simply stores the objects in a Hashmap node. | ||
lib.assoc = function(node, key, val, opts){ | ||
return assoc(node, key, val, opts || defaultOpts, 0) | ||
} | ||
var assoc = function(node, key, val, opts, depth){ | ||
return assocFns[node.type](node, key, val, opts || defaultOpts, depth || 0) | ||
return assocFns[node.type](node, key, val, opts, depth) | ||
} | ||
@@ -141,3 +123,3 @@ | ||
trie: function(node, key, val, opts, depth){ | ||
var path = hashMask(key, depth, opts.hash) | ||
var path = hash.mask(key, depth, opts.hash) | ||
var child = node.children[path] | ||
@@ -149,4 +131,4 @@ | ||
value: function(node, key, val, opts, depth){ | ||
var nodePath = hashMask(node.key, depth, opts.hash) | ||
var keyPath = hashMask(key, depth, opts.hash) | ||
var nodePath = hash.mask(node.key, depth, opts.hash) | ||
var keyPath = hash.mask(key, depth, opts.hash) | ||
@@ -217,4 +199,8 @@ var makeHashmap = function(){ | ||
// Trie. | ||
lib.dissoc = function(node, key, opts){ | ||
return dissoc(node, key, opts || defaultOpts, 0) | ||
} | ||
var dissoc = function(node, key, opts, depth){ | ||
return dissocFns[node.type](node, key, opts || defaultOpts, depth || 0) | ||
return dissocFns[node.type](node, key, opts, depth) | ||
} | ||
@@ -224,3 +210,3 @@ | ||
trie: function(node, key, opts, depth){ | ||
var path = hashMask(key, depth, opts.hash) | ||
var path = hash.mask(key, depth, opts.hash) | ||
var child = node.children[path] | ||
@@ -274,3 +260,3 @@ | ||
// Has recurses down a node, using hashMask to navigate a 'path' down branches. | ||
// Has recurses down a node, using hash.mask to navigate a 'path' down branches. | ||
// If a value node is found, if its key is equal to the key provided, then the | ||
@@ -281,4 +267,8 @@ // Trie contains the key, and true is returned. | ||
// a key, it also means that the key is in the trie,. | ||
lib.has = function(trie, key, opts){ | ||
return has(trie, key, opts || defaultOpts, 0) | ||
} | ||
var has = function(trie, key, opts, depth){ | ||
return hasFns[trie.type](trie, key, opts || defaultOpts, depth || 0) | ||
return hasFns[trie.type](trie, key, opts, depth) | ||
} | ||
@@ -288,3 +278,3 @@ | ||
trie: function(node, key, opts, depth){ | ||
var child = node.children[hashMask(key, depth, opts.hash)] | ||
var child = node.children[hash.mask(key, depth, opts.hash)] | ||
if ( child === undefined ) return false | ||
@@ -309,4 +299,8 @@ else return has(child, key, opts, depth + 1) | ||
// and returns that instead. | ||
lib.get = function(trie, key, opts){ | ||
return get(trie, key, opts || defaultOpts, 0) | ||
} | ||
var get = function(trie, key, opts, depth){ | ||
return getFns[trie.type](trie, key, opts || defaultOpts, depth || 0) | ||
return getFns[trie.type](trie, key, opts, depth) | ||
} | ||
@@ -316,3 +310,3 @@ | ||
trie: function(node, key, opts, depth){ | ||
var child = node.children[hashMask(key, depth, opts.hash)] | ||
var child = node.children[hash.mask(key, depth, opts.hash)] | ||
if ( child === undefined ) return undefined | ||
@@ -332,4 +326,2 @@ else return get(child, key, opts, depth + 1) | ||
// Node -> Object | ||
@@ -345,5 +337,7 @@ | ||
// pure from an API perspective) | ||
lib.mutable = function(node){ | ||
return mutable(node, {}) | ||
} | ||
var mutable = function(node, curr){ | ||
curr = curr || {} | ||
mutableFns[node.type](node, curr) | ||
@@ -355,3 +349,3 @@ return curr | ||
trie: function(node, curr){ | ||
for ( var path in node.children ) mutable(node.children[path], curr) | ||
for ( var path in node.children ) if ( node.children.hasOwnProperty(path) ) mutable(node.children[path], curr) | ||
}, | ||
@@ -362,3 +356,3 @@ value: function(node, curr){ | ||
hashmap: function(node, curr){ | ||
for ( var key in node.values ) mutable(node.values[key], curr) | ||
for ( var key in node.values ) if ( node.values.hasOwnProperty(key) ) mutable(node.values[key], curr) | ||
} | ||
@@ -371,4 +365,7 @@ } | ||
// implemented similarly to `mutable` | ||
lib.keys = function(node){ | ||
return keys(node, []) | ||
} | ||
var keys = function(node, arr){ | ||
arr = arr || [] | ||
keysFns[node.type](node, arr) | ||
@@ -380,3 +377,3 @@ return arr | ||
trie: function(node, arr){ | ||
for ( var path in node.children ) keys(node.children[path], arr) | ||
for ( var path in node.children ) if ( node.children.hasOwnProperty(path) ) keys(node.children[path], arr) | ||
}, | ||
@@ -387,16 +384,4 @@ value: function(node, arr){ | ||
hashmap: function(node, arr){ | ||
for ( var key in node.values ) keys(node.values[key], arr) | ||
for ( var key in node.values ) if ( node.values.hasOwnProperty(key) ) keys(node.values[key], arr) | ||
} | ||
} | ||
module.exports = { | ||
Trie : Trie, | ||
Value : Value, | ||
Hashmap : Hashmap, | ||
has : has, | ||
get : get, | ||
assoc : assoc, | ||
dissoc : dissoc, | ||
mutable : mutable, | ||
keys : keys | ||
} |
@@ -7,2 +7,7 @@ 'use strict' | ||
describe('mutable', function(){ | ||
it('should have 1 param', function(){ | ||
a.equal(p.mutable.length, 1) | ||
}) | ||
it('should return a mutable version of a Trie', function(){ | ||
@@ -15,2 +20,10 @@ var t1 = p.assoc(p.Trie(), 'key', 'value') | ||
}) | ||
it('should be unaffected by changing the prototype', function(){ | ||
Object.prototype.foo = 'bar' | ||
a.deepEqual(p.mutable(p.Trie()), {}) | ||
delete Object.prototype.foo | ||
}) | ||
}) |
@@ -20,3 +20,3 @@ 'use strict' | ||
var t = p.assoc(p.Trie(), 'key', 'value', opts) | ||
var t = p.dissoc(t, 'some-non-present-key', opts) | ||
t = p.dissoc(t, 'some-non-present-key', opts) | ||
@@ -23,0 +23,0 @@ a.ok(_.isEmpty(t.children)) |
'use strict' | ||
require('better-stack-traces') | ||
var a = require('assert') | ||
@@ -10,6 +8,45 @@ var p = require('..') | ||
var log = function(msg){ | ||
console && typeof console.log === 'function' && console.log(msg) | ||
typeof console && typeof console.log === 'function' && console.log(msg) | ||
} | ||
describe('assoc/dissoc/has/get', function(){ | ||
describe('assoc', function(){ | ||
it('should have 3 params', function(){ | ||
a.equal(p.get.length, 3) | ||
}) | ||
}) | ||
describe('dissoc', function(){ | ||
it('should have 4 params', function(){ | ||
a.equal(p.get.length, 3) | ||
}) | ||
}) | ||
describe('get', function(){ | ||
it('should have 3 params', function(){ | ||
a.equal(p.get.length, 3) | ||
}) | ||
}) | ||
describe('has', function(){ | ||
it('should have 3 params', function(){ | ||
a.equal(p.has.length, 3) | ||
}) | ||
}) | ||
describe('keys', function(){ | ||
it('should have 1 param', function(){ | ||
a.equal(p.keys.length, 1) | ||
}) | ||
it('should be unaffected by changing the prototype', function(){ | ||
Object.prototype.foo = 'bar' | ||
a.deepEqual(p.keys(p.Trie()), []) | ||
delete Object.prototype.foo | ||
}) | ||
}) | ||
describe('assoc/dissoc/has/get together', function(){ | ||
it('should be able to assoc/get', function(){ | ||
@@ -122,3 +159,1 @@ var t = p.assoc(p.Trie(), 'key', 'value') | ||
}) | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
197297
1
7
80
3910
1
+ Addedstring-hash@1.1.0
+ Addedstring-hash@1.1.0(transitive)