hashtablejs

0.0.1 • Public • Published

hashtable

Hashtable data structure implementation in JavaScript.

Stores entries (key, value) in buckets based on hashcode returned by hashing function. Entries are stored in an array within bucket if hashcodes collide, and retrieved by unique key value.

Hash table

source

Install

npm install hashtablejs
bower install hashtable

Usage

const Hashtable = require('hashtablejs');
 
function Point(x, y) {
  this.x = x;
  this.y = y;
}
 
function hashCode(point) {
  return `Point:${point.x},${point.y}`;
}
 
function equals(pointA, pointB) {
  return (pointA.x === pointB.x &&
          pointA.y === pointB.y);
}
 
const ht = new Hashtable(hashCode, equals);
 
console.log(ht.put(new Point(2,3), 'red')); // undefined
console.log(ht.has(new Point(2,3)); // true
console.log(ht.get(new Point(2,3)); // 'red'
console.log(ht.put(new Point(2,3), 'green')); // 'red'
console.log(ht.put(new Point(8,9), 'blue')); // undefined
console.log(ht.size()); // 2
console.log(ht.remove(new Point(8,9))); // 'blue'
console.log(ht.clear()); // true
console.log(ht.size()); // 0

API

Hashtable(hashCodeFn, equalsFn);
hashtable.has(key);
hashtable.put(key, value);
hashtable.get(key);
hashtable.remove(key);
hashtable.size();
hashtable.clear();
hashtable.entries();
hashtable.values();
hashtable.keys();
hashtable.each(fn);

Test

npm test

License

MIT

Package Sidebar

Install

npm i hashtablejs

Weekly Downloads

2

Version

0.0.1

License

MIT

Last publish

Collaborators

  • miguelmota