Chromium Code Reviews| Index: sdk/lib/html/html_common/jenkins_smi_hash.dart |
| diff --git a/sdk/lib/html/html_common/jenkins_smi_hash.dart b/sdk/lib/html/html_common/jenkins_smi_hash.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cf5913f3e4f1704812d8547a653045a1798db923 |
| --- /dev/null |
| +++ b/sdk/lib/html/html_common/jenkins_smi_hash.dart |
| @@ -0,0 +1,47 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of html_common; |
| + |
| +/** |
| + * This is the [Jenkins hash function][1] but using masking to keep |
| + * values in SMI range. |
| + * |
| + * [1]: http://en.wikipedia.org/wiki/Jenkins_hash_function |
| + * |
| + * Use: |
| + * Hash each value with the hash of the previous value, then get the final |
| + * hash by calling finish. |
| + * |
| + * var hash = 0; |
| + * for (var value in values) { |
| + * hash = JenkinsSmiHash.combine(hash, value.hashCode); |
| + * } |
| + * hash = JenkinsSmiHash.finish(hash); |
| + */ |
| +class JenkinsSmiHash { |
| + // TODO: Bug 11617- This class should be optimized and standardized elsewhere. |
| + |
| + static int combine(int hash, int value) { |
| + hash = 0x1fffffff & (hash + value); |
| + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| + return hash ^ (hash >> 6); |
| + } |
| + |
| + static int finish(int hash) { |
| + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| + hash = hash ^ (hash >> 11); |
| + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| + } |
| + |
| + static int hash2(a, b) => JenkinsSmiHash.finish( |
|
sra1
2013/06/28 22:25:02
Do you need the class name here?
blois
2013/06/28 22:35:37
Done.
|
| + JenkinsSmiHash.combine( |
| + JenkinsSmiHash.combine(0, a), b)); |
| + |
| + static int hash4(a, b, c, d) => JenkinsSmiHash.finish( |
| + JenkinsSmiHash.combine( |
| + JenkinsSmiHash.combine( |
| + JenkinsSmiHash.combine( |
| + JenkinsSmiHash.combine(0, a), b), c), d)); |
| +} |