Skip to content

Commit

Permalink
Deprecate hash functions (#757)
Browse files Browse the repository at this point in the history
Folks should use Object.hash bits instead
Replaced impl w/ Object.hash bits
  • Loading branch information
kevmoo authored Feb 4, 2025
1 parent ec4ed8e commit 003ef51
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.2.3-wip

- Deprecate all `hash` functions. Use functions in `Object` instead.

## 3.2.2

- Fix a number of doc comment issues.
Expand Down
30 changes: 8 additions & 22 deletions lib/src/core/hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,17 @@
// limitations under the License.

/// Generates a hash code for multiple [objects].
int hashObjects(Iterable objects) =>
_finish(objects.fold(0, (h, i) => _combine(h, i.hashCode)));
@Deprecated('Use Object.hashAll instead')
int hashObjects(Iterable objects) => Object.hashAll(objects);

/// Generates a hash code for two objects.
int hash2(a, b) => _finish(_combine(_combine(0, a.hashCode), b.hashCode));
@Deprecated('Use Object.hash instead')
int hash2(a, b) => Object.hash(a, b);

/// Generates a hash code for three objects.
int hash3(a, b, c) => _finish(
_combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode));
@Deprecated('Use Object.hash instead')
int hash3(a, b, c) => Object.hash(a, b, c);

/// Generates a hash code for four objects.
int hash4(a, b, c, d) => _finish(_combine(
_combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode),
d.hashCode));

// Jenkins hash functions

int _combine(int hash, int value) {
hash = 0x1fffffff & (hash + value);
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}

int _finish(int hash) {
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
@Deprecated('Use Object.hash instead')
int hash4(a, b, c, d) => Object.hash(a, b, c, d);
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
Quiver is a set of utility libraries for Dart that makes using many Dart
libraries easier and more convenient, or adds additional functionality.
repository: https://github.com/google/quiver-dart
version: 3.2.2
version: 3.2.3-wip

# When updating SDK lower bound, also update .github/workflows/dart.yml to test
# against the new lower bound.
Expand Down
2 changes: 2 additions & 0 deletions test/core/hash_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// ignore_for_file: deprecated_member_use_from_same_package

library quiver.core.hash_test;

import 'package:quiver/src/core/hash.dart';
Expand Down

0 comments on commit 003ef51

Please sign in to comment.