HashArray is a data structure that combines the best feature of a hash (O(1) retrieval) and an array (length and ordering). Think of it as a super-lightweight, extensible, self-indexing set database in memory.
npm install hasharray
So as not to scare the faint of heart with all the technical goodness contained therein:
var HashArray = require('hasharray'),
ha = new HashArray('id');
ha.add({id: 'someId0', name: 'Josh'},
{id: 'someId1', name: 'Joseph'},
{id: 'someId2', name: 'Kuba'},
{id: 'someId3', name: 'Ty'});
console.log(ha.get('someId0').name); // 'Kuba'
HashArray(keyfields, callback, options)
keyfields
var HashArray = require('HashArray');
new HashArray('firstname'); // one key, depth of 1 (e.g. `item.firstname`)
new HashArray(['firstname']); // same as above
// one key, depth of 2 (e.g. `item.first.name`)
new HashArray([['first', 'name']]);
// two keys, depth of 1 (e.g. `item.firstname` AND `item.lastname`)
new HashArray(['firstname', 'lastname']);
// multiple keys, depth of 2 (e.g. `item.name.first` AND `item.name.last`)
new HashArray([['name', 'first'], ['name', 'last']]);
callback
A callback function can be specified to monitor changes to the HashArray as they occur:
var ha = new HashArray('someKey', function(type, whatChanged) {
// type will be 'add', 'addMap', 'remove', 'removeByKey', or 'construct'
// whatChanged will be the items that were changed
});
options
{
ignoreDuplicates: false; // When true, any attempt to add items that collide
// with any items in the HashArray will be ignored.
// default is false.
}
add(...items)
: insert all arguments.addAll(Array of items)
: insert all items in the passed in Array.addMap(key, item)
: adds a single mapping from a key to a item.addOne(item)
: adds a single item, skipping dispatch of event.
get(key)
: if a single item exists forkey
, returns that item. If no item exists, returnsundefined
. If multiple items exist for the key, returns anArray
.getAll(keys)
: unions all items for all provided keys and returns an Array. Ensures no duplicates even ifkeys
independently return sets that intersect.getAsArray(key)
: likeget()
except if no item exists, returns an emptyArray
.sample(count, keys)
: samples all items in the HashArray, returning a randomArray
of sizecount
. Ifcount
is larger than theHashArray
, returns all items in the theHashArray
remove(...items)
: removes all items in arguments.removeByKey(...keys)
: removes all items that match thekeys
provided.removeAll()
: clears out all items in theHashArray
intersection(HashArray)
: returns a clonedHashArray
whose items are the intersection betweenthis
and the passed inHashArray
(this
^argument
).complement(HashArray)
: returns a clonedHashArray
whose items are the complement betweenthis
and the passed inHashArray
(this
\argument
).
has(key)
: returnstrue
if any items exist for the provided key.hasMultiple(key)
: returnstrue
if multiple items exist for the provided key (e.g.get(key)
would return anArray
)collides(item)
: returnstrue
if the argument would collide with any other item in thisHashArray
for any key in theHashArray
.
forEach(keys, callback)
: iterates through a union of all items that match the provided keys argument and calls the callback passing in each item as an argument.forEachDeep(keys, key, callback)
: iterates through a union of all items that match the provided keys argument and passes the value (at the provided key argument) as an argument to the callback.
sum(keys, key, weightKey)
: sums all values for a union of all objects found for thekeys
argument provided at thekey
you provide. Weights the summation byitem[weightKey]
or by1.0
if noweightKey
is provided.average(keys, key, weightKey)
: similar tosum()
except returns the average.
filter(keys, callbackOrKey)
: returns a newHashArray
that is a clone of the current one but filtered by the providedkeys
.
objectAt(item, key)
: internally used to find a value onitem
atkey
. For example,objectAt(obj, 'firstname')
would returnobj['firstname']
.objectAt(obj, ['first', 'name'])
would returnobj['first']['name']
. Returns undefined if the key does not map properly to the provided object.clone(callback, ignoreItems)
: shallow clones theHashArray
. IfignoreItems
is true, does not clone the items just the settings.
My goal with this data structure was to attempt to get the ordered features of an Array while keeping lookup O(1) for any arbitrary keys. The cost is a small loss of memory.
In addition HashArray works with deep keys. Consider the following array of customer objects:
var customers = [{
id: 1337,
name: {
first: 'Bob',
last: 'Winkle',
},
dob: new Date(1985, 1, 4),
address: {
city: 'Chicago',
zip: 60616
}
}...]
If we had multiple people who lived in the zip code 60616
, ideally we would want to index the data by zip code so that if we had to rapidly retrieve all those people we could do so.
With HashArray, we could index the above data for O(1) retrieval by id
, ['name', 'first']
, and ['name', 'last]
like so:
var HashArray = require('hasharray'),
ha = new HashArray(['id', ['name', 'first'], ['name', 'last']]);
ha.addAll(customers);
// At this point we have indexed everything by ['name', 'first'] so there is already an array built internal to `ha` that
// contains all the 'Bob' customers. So this operation is O(1).
var bobs = ha.get('Bob');
Note: the order of the bobs
array above will be the order in which they were inserted.
Normally when you use a standard JavaScript Object to map keys to values, the only way to retrieve the count of objects is to loop over all the keys which is O(n). However, with HashArray if you want to determine the length of all customers in O(1), it is as simple as:
ha.all.length; // in addition, ha.all is an ordered array of all customers in the order in which they were added!
At this time, I am also working on adding functions for statistical analysis, like sum(...)
. See the tests for more information. I'll be adding more to this documentation as I go.
var HashArray = require ('hasharray');
// Create new hasharray with two key mappings.
var ha = new HashArray(['name', 'zip']);
// Add 2 objects to the hash.
var item1 = {name: 'Josh', zip: '54321'};
var item2 = {name: 'Josh', zip: '12345'};
ha.add(item1, item2);
if (ha.has('Josh'))
console.log(ha.get('Josh')); // Will output two objects to the console
// Display the number of unique objects. In this case, 2.
console.log(ha.all.length);
// Remove an element by one of the keys
ha.removeByKey('54321'); // This removes item1
// Remove item2 directly
ha.remove(item2);
var HashArray = require ('hasharray');
var ha = new HashArray([
['name', 'last'], // Internally maps obj.name.last -> obj
['name', 'first'], // Internally maps obj.name.first -> obj
'zip'
]);
ha.add({
name: {
first: 'Josh',
last: 'Jung'
},
zip: 60616
});
console.log(ha.get(60616) === ha.get('Josh') == ha.get('Jung')); // true
var ha = new HashArray(['firstName', 'lastName']);
var person1 = {firstName: 'Bill', lastName: 'William'},
person2 = {firstName: 'Bob', lastName: 'William'};
ha.add(person1, person2);
console.log(ha.getAsArray('William')); // [person1, person2]
var ha = new HashArray(['firstName', 'lastName']);
var person1 = {firstName: 'Victor', lastName: 'Victor'},
person2 = {firstName: 'Victor', lastName: 'Manning'},
person3 = {firstName: 'Manning', lastName: 'Victor'};
person4 = {firstName: 'John', lastName: 'Smith'};
ha.add(person1, person2, person3, person4);
console.log(ha.getAll(['Victor', 'Smith'])); // [person1, person2, person3, person4]
console.log(ha.getAll(['John', 'Smith'])); // [person4]
If two items contain the same key, they are appended to an array at that key location.
var HashArray = require ('hasharray');
var ha = new HashArray([
['name', 'last'],
['name', 'first']
]);
ha.add({
name: {
first: 'Josh',
last: 'Jung'
}
},
{
name: {
first: 'Josh',
last: 'Mills'
}
},
{
name: {
first: 'Josh',
last: 'Willis'
}
});
console.log(ha.get('Josh').length); // Will be 3
console.log(ha.get('Willis')); // Will be {name: {first: 'Josh', last: 'Willis'} }
If you need to check if an item already exists for a given key, simply use has(...)
:
ha.has('someKeyValue');
// Here we index by item.type and item.data.speed
var ha = new HashArray(['type', ['data', 'speed']]);
var a = {type: 'airplane', data: {speed: 100, weight: 10000}},
b = {type: 'airplane', data: {speed: 100, weight: 20000}},
c = {type: 'airplane', data: {speed: 25, weight: 50000}};
d = {type: 'boat', data: {speed: 10, weight: 100000}};
e = {type: 'boat', data: {speed: 5, weight: 200000}};
ha.add(a, b, c, d, e);
// Loop through just airplanes
ha.forEach('airplane', function (airplane) {console.log(airplane);});
// Loop through airplanes AND boats
ha.forEach(['airplane', 'boat'], function (airplane) {console.log(airplane);});
// Loop through all items that have a speed of 100
ha.forEach(100, function (airplane) {console.log(airplane);});
forEachDeep()
differs from forEach()
in that it passes a value by key you specify to the callback
:
// Here we index by item.type and item.data.speed
var ha = new HashArray(['type', ['data', 'speed']]);
var a = {type: 'airplane', data: {speed: 100, weight: 10000}},
b = {type: 'airplane', data: {speed: 100, weight: 20000}},
c = {type: 'airplane', data: {speed: 25, weight: 50000}};
d = {type: 'boat', data: {speed: 10, weight: 100000}};
e = {type: 'boat', data: {speed: 5, weight: 200000}};
ha.add(a, b, c, d, e);
// Loop through all items that have a speed of 100 and only pass the speed to the callback
ha.forEachDeep(100, ['data', 'speed'], function (speed) {
console.log('Speed is: ' + speed);
});
Cloning makes a new HashArray clone of the original, ensuring that no Array objects are shared.
Keep in mind that cloning does deep clone objects in the collection. Therefore if you clone an object with three Object items, the clonee will be a new HashArray but will contain references to the original objects.
var HashArray = require ('hasharray');
...
var ha = new HashArray(['someKey']);
...
var clonee = ha.clone();
HashArray uses jclass, which is an implementation of John Resig's simple inheritance model.
You can easily extend HashArray:
var MyCustomHashArray = HashArray._extend({
...
init: function init(keyFields)
{
console.log('My custom hash array!');
init._super(keyFields);
}
...
});
var myCustomHashArray = new MyCustomHashArray();
See the jclass
documentation for more information.
>mocha
START
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
76 passing (25ms)
The MIT License (MIT)
Copyright (c) 2014 Joshua Jung
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.