Move jenkins_smi_hash to package:analyzer
Change-Id: Ic458da90e5bdb6b3b7fee26088b17dd560ff3a68
Reviewed-on: https://dart-review.googlesource.com/c/77861
Commit-Queue: Peter von der Ahé <ahe@google.com>
Auto-Submit: Peter von der Ahé <ahe@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/generated/utilities_general.dart b/pkg/analyzer/lib/src/generated/utilities_general.dart
index 5571721..9722bc8 100644
--- a/pkg/analyzer/lib/src/generated/utilities_general.dart
+++ b/pkg/analyzer/lib/src/generated/utilities_general.dart
@@ -8,8 +8,6 @@
import 'package:yaml/yaml.dart';
-export 'package:front_end/src/base/jenkins_smi_hash.dart' show JenkinsSmiHash;
-
/**
* Test if the given [value] is `false` or the string "false" (case-insensitive).
*/
@@ -55,6 +53,56 @@
*/
String toUpperCase(Object value) => value?.toString()?.toUpperCase();
+/// Jenkins hash function, optimized for small integers.
+///
+/// Static methods borrowed from sdk/lib/math/jenkins_smi_hash.dart. Non-static
+/// methods are an enhancement for the "front_end" package.
+///
+/// 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);
+}
+
/**
* A simple limited queue.
*/
diff --git a/pkg/front_end/lib/src/base/jenkins_smi_hash.dart b/pkg/front_end/lib/src/base/jenkins_smi_hash.dart
deleted file mode 100644
index 8901ba4..0000000
--- a/pkg/front_end/lib/src/base/jenkins_smi_hash.dart
+++ /dev/null
@@ -1,53 +0,0 @@
-// 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.
-///
-/// Static methods borrowed from sdk/lib/math/jenkins_smi_hash.dart. Non-static
-/// methods are an enhancement for the "front_end" package.
-///
-/// 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);
-}
diff --git a/pkg/front_end/test/scanner_test.dart b/pkg/front_end/test/scanner_test.dart
index 9eb2956..936ad38 100644
--- a/pkg/front_end/test/scanner_test.dart
+++ b/pkg/front_end/test/scanner_test.dart
@@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.
import 'package:front_end/src/base/errors.dart';
-import 'package:front_end/src/base/jenkins_smi_hash.dart';
import 'package:front_end/src/fasta/scanner/abstract_scanner.dart'
show AbstractScanner;
import 'package:front_end/src/scanner/errors.dart';
@@ -1518,6 +1517,20 @@
}
}
+// TODO(ahe): Remove this when http://dartbug.com/11617 is fixed.
+int combineHash(int hash, int value) {
+ hash = 0x1fffffff & (hash + value);
+ hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
+ return hash ^ (hash >> 6);
+}
+
+// TODO(ahe): Remove this when http://dartbug.com/11617 is fixed.
+int finishHash(int hash) {
+ hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
+ hash = hash ^ (hash >> 11);
+ return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
+}
+
class TestError {
final int offset;
final ErrorCode errorCode;
@@ -1527,13 +1540,13 @@
@override
get hashCode {
- var h = new JenkinsSmiHash()..add(offset)..add(errorCode);
+ int h = combineHash(combineHash(0, offset), errorCode.hashCode);
if (arguments != null) {
for (Object argument in arguments) {
- h.add(argument);
+ h = combineHash(h, argument.hashCode);
}
}
- return h.hashCode;
+ return finishHash(h);
}
@override