Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/typedObjects/hash.js

Issue 6253377650425856: WIP - Dictionary types implementation (Closed)
Patch Set: Created Jan. 9, 2015, 3:34 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/typedObjects/dictionaryTypes.js ('k') | lib/typedObjects/objectTypes.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/typedObjects/hash.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/typedObjects/hash.js
@@ -0,0 +1,56 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+"use strict";
+
+/*
+ * Note: This implements the hash function by Paul Hsieh, see
+ * http://www.azillionmonkeys.com/qed/hash.html. The code below is based on Paul
+ * Hsieh's code which is licensed under LGPL 2.1.
+ */
+
+exports.calculateHash = function calculateHash(/**string*/ str)
+{
+ let length = str.length | 0;
+ let hash = length << 1;
+
+ for (let i = 0; i < length - 1; i += 2)
+ {
+ // Main loop, process two characters at a time
+ hash += str.get(i);
+ let tmp = (str.get(i + 1) << 11) ^ hash;
+ hash = (hash << 16) ^ tmp;
+ hash += hash >>> 11;
+ }
+
+ if (length & 1)
+ {
+ // Process remaining character
+ hash += str.get(length - 1);
+ hash ^= hash << 11;
+ hash += hash >>> 17;
+ }
+
+ // "Avalanching" of final 127 bits
+ hash ^= hash << 3;
+ hash += hash >>> 5;
+ hash ^= hash << 4;
+ hash += hash >>> 17;
+ hash ^= hash << 25;
+ hash += hash >>> 6;
+ return hash >>> 0;
+}
« no previous file with comments | « lib/typedObjects/dictionaryTypes.js ('k') | lib/typedObjects/objectTypes.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld