pax_global_header 0000666 0000000 0000000 00000000064 12206215525 0014512 g ustar 00root root 0000000 0000000 52 comment=9f40361c7e2835a9b7b8eaa1cbab2a9f94ee22a2
imurmurhash-js-0.1.4/ 0000775 0000000 0000000 00000000000 12206215525 0014472 5 ustar 00root root 0000000 0000000 imurmurhash-js-0.1.4/LICENSE 0000664 0000000 0000000 00000002102 12206215525 0015472 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2013 Gary Court, Jens Taylor
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.
imurmurhash-js-0.1.4/README.md 0000664 0000000 0000000 00000011233 12206215525 0015751 0 ustar 00root root 0000000 0000000 iMurmurHash.js
==============
An incremental implementation of the MurmurHash3 (32-bit) hashing algorithm for JavaScript based on [Gary Court's implementation](https://github.com/garycourt/murmurhash-js) with [kazuyukitanimura's modifications](https://github.com/kazuyukitanimura/murmurhash-js).
This version works significantly faster than the non-incremental version if you need to hash many small strings into a single hash, since string concatenation (to build the single string to pass the non-incremental version) is fairly costly. In one case tested, using the incremental version was about 50% faster than concatenating 5-10 strings and then hashing.
Installation
------------
To use iMurmurHash in the browser, [download the latest version](https://raw.github.com/jensyt/imurmurhash-js/master/imurmurhash.min.js) and include it as a script on your site.
```html
```
---
To use iMurmurHash in Node.js, install the module using NPM:
```bash
npm install imurmurhash
```
Then simply include it in your scripts:
```javascript
MurmurHash3 = require('imurmurhash');
```
Quick Example
-------------
```javascript
// Create the initial hash
var hashState = MurmurHash3('string');
// Incrementally add text
hashState.hash('more strings');
hashState.hash('even more strings');
// All calls can be chained if desired
hashState.hash('and').hash('some').hash('more');
// Get a result
hashState.result();
// returns 0xe4ccfe6b
```
Functions
---------
### MurmurHash3 ([string], [seed])
Get a hash state object, optionally initialized with the given _string_ and _seed_. _Seed_ must be a positive integer if provided. Calling this function without the `new` keyword will return a cached state object that has been reset. This is safe to use as long as the object is only used from a single thread and no other hashes are created while operating on this one. If this constraint cannot be met, you can use `new` to create a new state object. For example:
```javascript
// Use the cached object, calling the function again will return the same
// object (but reset, so the current state would be lost)
hashState = MurmurHash3();
...
// Create a new object that can be safely used however you wish. Calling the
// function again will simply return a new state object, and no state loss
// will occur, at the cost of creating more objects.
hashState = new MurmurHash3();
```
Both methods can be mixed however you like if you have different use cases.
---
### MurmurHash3.prototype.hash (string)
Incrementally add _string_ to the hash. This can be called as many times as you want for the hash state object, including after a call to `result()`. Returns `this` so calls can be chained.
---
### MurmurHash3.prototype.result ()
Get the result of the hash as a 32-bit positive integer. This performs the tail and finalizer portions of the algorithm, but does not store the result in the state object. This means that it is perfectly safe to get results and then continue adding strings via `hash`.
```javascript
// Do the whole string at once
MurmurHash3('this is a test string').result();
// 0x70529328
// Do part of the string, get a result, then the other part
var m = MurmurHash3('this is a');
m.result();
// 0xbfc4f834
m.hash(' test string').result();
// 0x70529328 (same as above)
```
---
### MurmurHash3.prototype.reset ([seed])
Reset the state object for reuse, optionally using the given _seed_ (defaults to 0 like the constructor). Returns `this` so calls can be chained.
---
License (MIT)
-------------
Copyright (c) 2013 Gary Court, Jens Taylor
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.
imurmurhash-js-0.1.4/imurmurhash.js 0000664 0000000 0000000 00000010474 12206215525 0017402 0 ustar 00root root 0000000 0000000 /**
* @preserve
* JS Implementation of incremental MurmurHash3 (r150) (as of May 10, 2013)
*
* @author Jens Taylor
* @see http://github.com/homebrewing/brauhaus-diff
* @author Gary Court
* @see http://github.com/garycourt/murmurhash-js
* @author Austin Appleby
* @see http://sites.google.com/site/murmurhash/
*/
(function(){
var cache;
// Call this function without `new` to use the cached object (good for
// single-threaded environments), or with `new` to create a new object.
//
// @param {string} key A UTF-16 or ASCII string
// @param {number} seed An optional positive integer
// @return {object} A MurmurHash3 object for incremental hashing
function MurmurHash3(key, seed) {
var m = this instanceof MurmurHash3 ? this : cache;
m.reset(seed)
if (typeof key === 'string' && key.length > 0) {
m.hash(key);
}
if (m !== this) {
return m;
}
};
// Incrementally add a string to this hash
//
// @param {string} key A UTF-16 or ASCII string
// @return {object} this
MurmurHash3.prototype.hash = function(key) {
var h1, k1, i, top, len;
len = key.length;
this.len += len;
k1 = this.k1;
i = 0;
switch (this.rem) {
case 0: k1 ^= len > i ? (key.charCodeAt(i++) & 0xffff) : 0;
case 1: k1 ^= len > i ? (key.charCodeAt(i++) & 0xffff) << 8 : 0;
case 2: k1 ^= len > i ? (key.charCodeAt(i++) & 0xffff) << 16 : 0;
case 3:
k1 ^= len > i ? (key.charCodeAt(i) & 0xff) << 24 : 0;
k1 ^= len > i ? (key.charCodeAt(i++) & 0xff00) >> 8 : 0;
}
this.rem = (len + this.rem) & 3; // & 3 is same as % 4
len -= this.rem;
if (len > 0) {
h1 = this.h1;
while (1) {
k1 = (k1 * 0x2d51 + (k1 & 0xffff) * 0xcc9e0000) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (k1 * 0x3593 + (k1 & 0xffff) * 0x1b870000) & 0xffffffff;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1 = (h1 * 5 + 0xe6546b64) & 0xffffffff;
if (i >= len) {
break;
}
k1 = ((key.charCodeAt(i++) & 0xffff)) ^
((key.charCodeAt(i++) & 0xffff) << 8) ^
((key.charCodeAt(i++) & 0xffff) << 16);
top = key.charCodeAt(i++);
k1 ^= ((top & 0xff) << 24) ^
((top & 0xff00) >> 8);
}
k1 = 0;
switch (this.rem) {
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xffff) << 16;
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xffff) << 8;
case 1: k1 ^= (key.charCodeAt(i) & 0xffff);
}
this.h1 = h1;
}
this.k1 = k1;
return this;
};
// Get the result of this hash
//
// @return {number} The 32-bit hash
MurmurHash3.prototype.result = function() {
var k1, h1;
k1 = this.k1;
h1 = this.h1;
if (k1 > 0) {
k1 = (k1 * 0x2d51 + (k1 & 0xffff) * 0xcc9e0000) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (k1 * 0x3593 + (k1 & 0xffff) * 0x1b870000) & 0xffffffff;
h1 ^= k1;
}
h1 ^= this.len;
h1 ^= h1 >>> 16;
h1 = (h1 * 0xca6b + (h1 & 0xffff) * 0x85eb0000) & 0xffffffff;
h1 ^= h1 >>> 13;
h1 = (h1 * 0xae35 + (h1 & 0xffff) * 0xc2b20000) & 0xffffffff;
h1 ^= h1 >>> 16;
return h1 >>> 0;
};
// Reset the hash object for reuse
//
// @param {number} seed An optional positive integer
MurmurHash3.prototype.reset = function(seed) {
this.h1 = typeof seed === 'number' ? seed : 0;
this.rem = this.k1 = this.len = 0;
return this;
};
// A cached object to use. This can be safely used if you're in a single-
// threaded environment, otherwise you need to create new hashes to use.
cache = new MurmurHash3();
if (typeof(module) != 'undefined') {
module.exports = MurmurHash3;
} else {
this.MurmurHash3 = MurmurHash3;
}
}());
imurmurhash-js-0.1.4/imurmurhash.min.js 0000664 0000000 0000000 00000003546 12206215525 0020166 0 ustar 00root root 0000000 0000000 /**
* @preserve
* JS Implementation of incremental MurmurHash3 (r150) (as of May 10, 2013)
*
* @author Jens Taylor
* @see http://github.com/homebrewing/brauhaus-diff
* @author Gary Court
* @see http://github.com/garycourt/murmurhash-js
* @author Austin Appleby
* @see http://sites.google.com/site/murmurhash/
*/
!function(){function t(h,r){var s=this instanceof t?this:e;return s.reset(r),"string"==typeof h&&h.length>0&&s.hash(h),s!==this?s:void 0}var e;t.prototype.hash=function(t){var e,h,r,s,i;switch(i=t.length,this.len+=i,h=this.k1,r=0,this.rem){case 0:h^=i>r?65535&t.charCodeAt(r++):0;case 1:h^=i>r?(65535&t.charCodeAt(r++))<<8:0;case 2:h^=i>r?(65535&t.charCodeAt(r++))<<16:0;case 3:h^=i>r?(255&t.charCodeAt(r))<<24:0,h^=i>r?(65280&t.charCodeAt(r++))>>8:0}if(this.rem=3&i+this.rem,i-=this.rem,i>0){for(e=this.h1;;){if(h=4294967295&11601*h+3432906752*(65535&h),h=h<<15|h>>>17,h=4294967295&13715*h+461832192*(65535&h),e^=h,e=e<<13|e>>>19,e=4294967295&5*e+3864292196,r>=i)break;h=65535&t.charCodeAt(r++)^(65535&t.charCodeAt(r++))<<8^(65535&t.charCodeAt(r++))<<16,s=t.charCodeAt(r++),h^=(255&s)<<24^(65280&s)>>8}switch(h=0,this.rem){case 3:h^=(65535&t.charCodeAt(r+2))<<16;case 2:h^=(65535&t.charCodeAt(r+1))<<8;case 1:h^=65535&t.charCodeAt(r)}this.h1=e}return this.k1=h,this},t.prototype.result=function(){var t,e;return t=this.k1,e=this.h1,t>0&&(t=4294967295&11601*t+3432906752*(65535&t),t=t<<15|t>>>17,t=4294967295&13715*t+461832192*(65535&t),e^=t),e^=this.len,e^=e>>>16,e=4294967295&51819*e+2246770688*(65535&e),e^=e>>>13,e=4294967295&44597*e+3266445312*(65535&e),e^=e>>>16,e>>>0},t.prototype.reset=function(t){return this.h1="number"==typeof t?t:0,this.rem=this.k1=this.len=0,this},e=new t,"undefined"!=typeof module?module.exports=t:this.MurmurHash3=t}(); imurmurhash-js-0.1.4/package.json 0000664 0000000 0000000 00000001462 12206215525 0016763 0 ustar 00root root 0000000 0000000 {
"name": "imurmurhash",
"version": "0.1.4",
"description": "An incremental implementation of MurmurHash3",
"homepage": "https://github.com/jensyt/imurmurhash-js",
"main": "imurmurhash.js",
"files": [
"imurmurhash.js",
"imurmurhash.min.js",
"package.json",
"README.md"
],
"repository": {
"type": "git",
"url": "https://github.com/jensyt/imurmurhash-js"
},
"bugs": {
"url": "https://github.com/jensyt/imurmurhash-js/issues"
},
"keywords": [
"murmur",
"murmurhash",
"murmurhash3",
"hash",
"incremental"
],
"author": {
"name": "Jens Taylor",
"email": "jensyt@gmail.com",
"url": "https://github.com/homebrewing"
},
"license": "MIT",
"dependencies": {
},
"devDependencies": {
},
"engines": {
"node": ">=0.8.19"
}
}
imurmurhash-js-0.1.4/simplification.md 0000664 0000000 0000000 00000004041 12206215525 0020025 0 ustar 00root root 0000000 0000000 Proof of modifications
----------------------
Gary Court's version contains the following line:
```javascript
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff
```
which can be simplified to:
```javascript
k1 = (k1 * (c1 & 0xffff) + (k1 & 0xffff) * (c1 & 0xffff0000)) & 0xffffffff
```
Consider each half of the equation individually, letting `k1 = (a + b) & 0xffffffff`. Starting with `b`, we can simplify:
```javascript
b = (((k1 >>> 16) * c1) & 0xffff) << 16
b = (((k1 >>> 16) * (c1 & 0xffff)) & 0xffff) << 16
b = ((k1 & 0xffff0000) * (c1 & 0xffff)) & 0xffff0000
b = ((k1 & 0xffff0000) * (c1 & 0xffff)) & 0xffffffff
b = (k1 & 0xffff0000) * (c1 & 0xffff)
```
The last line is equal in this case because the entire expression `(a + b)` is ANDed with `0xffffffff`, so it can be factored out of `b`. Next, `a` can be expanded:
```javascript
a = (k1 & 0xffff) * c1
a = (k1 & 0xffff) * ((c1 & 0xffff) + (c1 & 0xffff0000))
a = (k1 & 0xffff) * (c1 & 0xffff) + (k1 & 0xffff) * (c1 & 0xffff0000)
```
Letting `a = e + f`, we get `k1 = (e + f + b) & 0xffffffff`. Combining `e + b`, we can find:
```javascript
e + b = (k1 & 0xffff) * (c1 & 0xffff) + (k1 & 0xffff0000) * (c1 & 0xffff)
e + b = ((k1 & 0xffff) + (k1 & 0xffff0000)) * (c1 & 0xffff)
e + b = k1 * (c1 & 0xffff)
```
Finally, putting it all together:
```javascript
k1 = ((e + b) + f) & 0xffffffff
k1 = (k1 * (c1 & 0xffff) + (k1 & 0xffff) * (c1 & 0xffff0000)) & 0xffffffff
````
Overall, all this does is multiply `k1` by `c1` and only keep the 32-bit result. Unfortunately, JavaScript can't handle the multiply directly since the result will be cast to floating point if it results in more than 53 bits of precision (and will lose the lower bits, exactly the ones we want to keep).
All other modifications to the algorithm are based on this change.
To see the original and modified versions this is based on, check out:
* [Gary Court's original version](https://github.com/garycourt/murmurhash-js)
* [kazuyukitanimura's modified version](https://github.com/kazuyukitanimura/murmurhash-js)