OLD | NEW |
1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 part of quiver.core; | 15 part of quiver.core; |
16 | 16 |
17 /** | 17 /// Generates a hash code for multiple [objects]. |
18 * Generates a hash code for multiple [objects]. | |
19 */ | |
20 int hashObjects(Iterable objects) => | 18 int hashObjects(Iterable objects) => |
21 _finish(objects.fold(0, (h, i) => _combine(h, i.hashCode))); | 19 _finish(objects.fold(0, (h, i) => _combine(h, i.hashCode))); |
22 | 20 |
23 /** | 21 /// Generates a hash code for two objects. |
24 * Generates a hash code for two objects. | |
25 */ | |
26 int hash2(a, b) => _finish(_combine(_combine(0, a.hashCode), b.hashCode)); | 22 int hash2(a, b) => _finish(_combine(_combine(0, a.hashCode), b.hashCode)); |
27 | 23 |
28 /** | 24 /// Generates a hash code for three objects. |
29 * Generates a hash code for three objects. | |
30 */ | |
31 int hash3(a, b, c) => _finish( | 25 int hash3(a, b, c) => _finish( |
32 _combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode)); | 26 _combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode)); |
33 | 27 |
34 /** | 28 /// Generates a hash code for four objects. |
35 * Generates a hash code for four objects. | |
36 */ | |
37 int hash4(a, b, c, d) => _finish(_combine( | 29 int hash4(a, b, c, d) => _finish(_combine( |
38 _combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode), | 30 _combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode), |
39 d.hashCode)); | 31 d.hashCode)); |
40 | 32 |
41 // Jenkins hash functions | 33 // Jenkins hash functions |
42 | 34 |
43 int _combine(int hash, int value) { | 35 int _combine(int hash, int value) { |
44 hash = 0x1fffffff & (hash + value); | 36 hash = 0x1fffffff & (hash + value); |
45 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | 37 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
46 return hash ^ (hash >> 6); | 38 return hash ^ (hash >> 6); |
47 } | 39 } |
48 | 40 |
49 int _finish(int hash) { | 41 int _finish(int hash) { |
50 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 42 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
51 hash = hash ^ (hash >> 11); | 43 hash = hash ^ (hash >> 11); |
52 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 44 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
53 } | 45 } |
OLD | NEW |