Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1526)

Unified Diff: pkg/front_end/lib/src/base/jenkins_smi_hash.dart

Issue 2508483002: Move scanner tests into the front_end package. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/test/generated/scanner_test.dart ('k') | pkg/front_end/lib/src/scanner/reader.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/src/base/jenkins_smi_hash.dart
diff --git a/pkg/front_end/lib/src/base/jenkins_smi_hash.dart b/pkg/front_end/lib/src/base/jenkins_smi_hash.dart
new file mode 100644
index 0000000000000000000000000000000000000000..846b26ba148f565fa760126cd599bfed6425abb8
--- /dev/null
+++ b/pkg/front_end/lib/src/base/jenkins_smi_hash.dart
@@ -0,0 +1,51 @@
+// Copyright (c) 2016, 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.
+
+/// Jenkins hash function, optimized for small integers.
+/// Borrowed from sdk/lib/math/jenkins_smi_hash.dart.
Brian Wilkerson 2016/11/15 20:51:37 "Borrowed from sdk/lib/math/jenkins_smi_hash.dart"
Paul Berry 2016/11/15 22:54:23 Done.
+///
+/// Where performance is critical, use [hash2], [hash3], or [hash4], or the
+/// pattern `finish(combine(combine(...combine(0, a), b)..., z))`, where a..z
+/// are hash codes to be combined.
+///
+/// For ease of use, you may also use this pattern:
+/// `(new JenkinsSmiHash()..add(a)..add(b)....add(z)).hashCode`, where a..z are
+/// the sub-objects whose hashes should be combined. This pattern performs the
+/// same operations as the performance critical variant, but allocates an extra
+/// object.
+class JenkinsSmiHash {
+ /// Accumulates the hash code [value] into the running hash [hash].
+ static int combine(int hash, int value) {
+ hash = 0x1fffffff & (hash + value);
+ hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
+ return hash ^ (hash >> 6);
+ }
+
+ /// Finalizes a running hash produced by [combine].
+ static int finish(int hash) {
+ hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
+ hash = hash ^ (hash >> 11);
+ return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
+ }
+
+ /// Combines together two hash codes.
+ static int hash2(a, b) => finish(combine(combine(0, a), b));
+
+ /// Combines together three hash codes.
+ static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c));
+
+ /// Combines together four hash codes.
+ static int hash4(a, b, c, d) =>
+ finish(combine(combine(combine(combine(0, a), b), c), d));
+
+ int _hash = 0;
+
+ /// Accumulates the object [o] into the hash.
+ void add(Object o) {
+ _hash = combine(_hash, o.hashCode);
+ }
+
+ /// Finalizes the hash and return the resulting hashcode.
+ int get hashCode => finish(_hash);
+}
« no previous file with comments | « pkg/analyzer/test/generated/scanner_test.dart ('k') | pkg/front_end/lib/src/scanner/reader.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698