Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
this addresses #259
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed May 10, 2014
1 parent 50bcb9f commit fe7602c
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions js/liquid-dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,6 @@ var LiquidDict = function() {

/******************************************************************************/

// How the key is derived dictates the number and size of buckets.

// Currently, key = 'k' + [2 lsb of 4 chars] + [len]
// [len] at the end is convenient because we can look it up easily using
// String.slice() since the first part of the key is fixed width.

// http://jsperf.com/makekey-concat-vs-join

var makeKey = function(word) {
var len = word.length;
var i = len >> 2;
return 'k' +
(word.charCodeAt( 0) & 0x03) +
(word.charCodeAt( i) & 0x03) +
(word.charCodeAt( i+i) & 0x03) +
(word.charCodeAt(i+i+i) & 0x03) +
len;
};

/******************************************************************************/

var meltBucket = function(ldict, len, bucket) {
ldict.frozenBucketCount -= 1;
var map = {};
Expand All @@ -84,7 +63,7 @@ var melt = function(ldict) {
for ( var key in buckets ) {
bucket = buckets[key];
if ( typeof bucket === 'string' ) {
buckets[key] = meltBucket(ldict, parseInt(key.slice(5), 10), bucket);
buckets[key] = meltBucket(ldict, key.charCodeAt(0) & 0xFF, bucket);
}
}
};
Expand All @@ -103,8 +82,33 @@ var freezeBucket = function(ldict, bucket) {

/******************************************************************************/

// How the key is derived dictates the number and size of buckets.
//
// http://jsperf.com/makekey-concat-vs-join/3
//
// Question: Why is using a prototyped function better than a standalone
// helper function?

LiquidDict.prototype.makeKey = function(word) {
var len = word.length;
if ( len > 255 ) {
// console.error('HTTP Switchboard> liquid-dict.js/makeKey(): len > 255');
return undefined;
}
var i = len >> 2;
return String.fromCharCode(
(word.charCodeAt( 0) & 0x03) << 14 |
(word.charCodeAt( i) & 0x03) << 12 |
(word.charCodeAt( i+i) & 0x03) << 10 |
(word.charCodeAt(i+i+i) & 0x03) << 8 |
len
);
};

/******************************************************************************/

LiquidDict.prototype.test = function(word) {
var key = makeKey(word);
var key = this.makeKey(word);
var bucket = this.dict[key];
if ( bucket === undefined ) {
return false;
Expand Down Expand Up @@ -138,7 +142,10 @@ LiquidDict.prototype.test = function(word) {
/******************************************************************************/

LiquidDict.prototype.add = function(word) {
var key = makeKey(word);
var key = this.makeKey(word);
if ( key === undefined ) {
return false;
}
var bucket = this.dict[key];
if ( bucket === undefined ) {
this.dict[key] = bucket = {};
Expand Down

0 comments on commit fe7602c

Please sign in to comment.