4
4
5
5
part of ui;
6
6
7
- class _HashEnd {
8
- const _HashEnd ();
9
- }
10
-
7
+ class _HashEnd { const _HashEnd (); }
11
8
const _HashEnd _hashEnd = _HashEnd ();
12
9
13
- /// Combine up to twenty values' hashCodes into one value.
10
+ /// Jenkins hash function, optimized for small integers.
11
+ //
12
+ // Borrowed from the dart sdk: sdk/lib/math/jenkins_smi_hash.dart.
13
+ class _Jenkins {
14
+ static int combine (int hash, Object o) {
15
+ assert (o is ! Iterable );
16
+ hash = 0x1fffffff & (hash + o.hashCode);
17
+ hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10 ));
18
+ hash = hash ^ (hash >> 6 );
19
+ return hash;
20
+ }
21
+
22
+ static int finish (int hash) {
23
+ hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3 ));
24
+ hash = hash ^ (hash >> 11 );
25
+ hash = 0x1fffffff & (hash + ((0x00003fff & hash) << 15 ));
26
+ return hash;
27
+ }
28
+ }
29
+
30
+ /// Combine up to twenty objects' hash codes into one value.
14
31
///
15
- /// If you only need to handle one value 's hashCode , then just refer to its
16
- /// [hashCode] getter directly.
32
+ /// If you only need to handle one object 's hash code , then just refer to its
33
+ /// [Object. hashCode] getter directly.
17
34
///
18
- /// If you need to combine an arbitrary number of values from a List or other
19
- /// Iterable, use [hashList] . The output of hashList can be used as one of the
20
- /// arguments to this function.
35
+ /// If you need to combine an arbitrary number of objects from a [ List] or other
36
+ /// [ Iterable] , use [hashList] . The output of [ hashList] can be used as one of
37
+ /// the arguments to this function.
21
38
///
22
39
/// For example:
23
40
///
24
- /// int hashCode => hashValues(foo, bar, hashList(quux), baz);
25
- int hashValues (Object arg01, Object arg02,
26
- [Object arg03 = _hashEnd,
27
- Object arg04 = _hashEnd,
28
- Object arg05 = _hashEnd,
29
- Object arg06 = _hashEnd,
30
- Object arg07 = _hashEnd,
31
- Object arg08 = _hashEnd,
32
- Object arg09 = _hashEnd,
33
- Object arg10 = _hashEnd,
34
- Object arg11 = _hashEnd,
35
- Object arg12 = _hashEnd,
36
- Object arg13 = _hashEnd,
37
- Object arg14 = _hashEnd,
38
- Object arg15 = _hashEnd,
39
- Object arg16 = _hashEnd,
40
- Object arg17 = _hashEnd,
41
- Object arg18 = _hashEnd,
42
- Object arg19 = _hashEnd,
43
- Object arg20 = _hashEnd]) {
44
- int result = 373 ;
45
- assert (arg01 is ! Iterable );
46
- result = 37 * result + arg01.hashCode;
47
- assert (arg02 is ! Iterable );
48
- result = 37 * result + arg02.hashCode;
41
+ /// ```dart
42
+ /// int hashCode => hashValues(foo, bar, hashList(quux), baz);
43
+ /// ```
44
+ int hashValues (
45
+ Object arg01, Object arg02, [ Object arg03 = _hashEnd,
46
+ Object arg04 = _hashEnd, Object arg05 = _hashEnd, Object arg06 = _hashEnd,
47
+ Object arg07 = _hashEnd, Object arg08 = _hashEnd, Object arg09 = _hashEnd,
48
+ Object arg10 = _hashEnd, Object arg11 = _hashEnd, Object arg12 = _hashEnd,
49
+ Object arg13 = _hashEnd, Object arg14 = _hashEnd, Object arg15 = _hashEnd,
50
+ Object arg16 = _hashEnd, Object arg17 = _hashEnd, Object arg18 = _hashEnd,
51
+ Object arg19 = _hashEnd, Object arg20 = _hashEnd ]) {
52
+ int result = 0 ;
53
+ result = _Jenkins .combine (result, arg01);
54
+ result = _Jenkins .combine (result, arg02);
49
55
if (arg03 != _hashEnd) {
50
- assert (arg03 is ! Iterable );
51
- result = 37 * result + arg03.hashCode;
56
+ result = _Jenkins .combine (result, arg03);
52
57
if (arg04 != _hashEnd) {
53
- assert (arg04 is ! Iterable );
54
- result = 37 * result + arg04.hashCode;
58
+ result = _Jenkins .combine (result, arg04);
55
59
if (arg05 != _hashEnd) {
56
- assert (arg05 is ! Iterable );
57
- result = 37 * result + arg05.hashCode;
60
+ result = _Jenkins .combine (result, arg05);
58
61
if (arg06 != _hashEnd) {
59
- assert (arg06 is ! Iterable );
60
- result = 37 * result + arg06.hashCode;
62
+ result = _Jenkins .combine (result, arg06);
61
63
if (arg07 != _hashEnd) {
62
- assert (arg07 is ! Iterable );
63
- result = 37 * result + arg07.hashCode;
64
+ result = _Jenkins .combine (result, arg07);
64
65
if (arg08 != _hashEnd) {
65
- assert (arg08 is ! Iterable );
66
- result = 37 * result + arg08.hashCode;
66
+ result = _Jenkins .combine (result, arg08);
67
67
if (arg09 != _hashEnd) {
68
- assert (arg09 is ! Iterable );
69
- result = 37 * result + arg09.hashCode;
68
+ result = _Jenkins .combine (result, arg09);
70
69
if (arg10 != _hashEnd) {
71
- assert (arg10 is ! Iterable );
72
- result = 37 * result + arg10.hashCode;
70
+ result = _Jenkins .combine (result, arg10);
73
71
if (arg11 != _hashEnd) {
74
- assert (arg11 is ! Iterable );
75
- result = 37 * result + arg11.hashCode;
72
+ result = _Jenkins .combine (result, arg11);
76
73
if (arg12 != _hashEnd) {
77
- assert (arg12 is ! Iterable );
78
- result = 37 * result + arg12.hashCode;
74
+ result = _Jenkins .combine (result, arg12);
79
75
if (arg13 != _hashEnd) {
80
- assert (arg13 is ! Iterable );
81
- result = 37 * result + arg13.hashCode;
76
+ result = _Jenkins .combine (result, arg13);
82
77
if (arg14 != _hashEnd) {
83
- assert (arg14 is ! Iterable );
84
- result = 37 * result + arg14.hashCode;
78
+ result = _Jenkins .combine (result, arg14);
85
79
if (arg15 != _hashEnd) {
86
- assert (arg15 is ! Iterable );
87
- result = 37 * result + arg15.hashCode;
80
+ result = _Jenkins .combine (result, arg15);
88
81
if (arg16 != _hashEnd) {
89
- assert (arg16 is ! Iterable );
90
- result = 37 * result + arg16.hashCode;
82
+ result = _Jenkins .combine (result, arg16);
91
83
if (arg17 != _hashEnd) {
92
- assert (arg17 is ! Iterable );
93
- result = 37 * result + arg17.hashCode;
84
+ result = _Jenkins .combine (result, arg17);
94
85
if (arg18 != _hashEnd) {
95
- assert (arg18 is ! Iterable );
96
- result = 37 * result + arg18.hashCode;
86
+ result = _Jenkins .combine (result, arg18);
97
87
if (arg19 != _hashEnd) {
98
- assert (arg19 is ! Iterable );
99
- result = 37 * result + arg19.hashCode;
88
+ result = _Jenkins .combine (result, arg19);
100
89
if (arg20 != _hashEnd) {
101
- assert (arg20 is ! Iterable );
102
- result = 37 * result + arg20.hashCode;
90
+ result = _Jenkins .combine (result, arg20);
103
91
// I can see my house from here!
104
92
}
105
93
}
@@ -119,19 +107,17 @@ int hashValues(Object arg01, Object arg02,
119
107
}
120
108
}
121
109
}
122
- return result;
110
+ return _Jenkins . finish ( result) ;
123
111
}
124
112
125
- /// Combine the hashCodes of an arbitrary number of values from an Iterable into
126
- /// one value. This function will return the same value if given "null" as if
127
- /// given an empty list.
128
- int hashList (Iterable <Object > args) {
129
- int result = 373 ;
130
- if (args != null ) {
131
- for (Object arg in args) {
132
- assert (arg is ! Iterable );
133
- result = 37 * result + arg.hashCode;
134
- }
113
+ /// Combine the [Object.hashCode] values of an arbitrary number of objects from
114
+ /// an [Iterable] into one value. This function will return the same value if
115
+ /// given null as if given an empty list.
116
+ int hashList (Iterable <Object > arguments) {
117
+ int result = 0 ;
118
+ if (arguments != null ) {
119
+ for (Object argument in arguments)
120
+ result = _Jenkins .combine (result, argument);
135
121
}
136
- return result;
122
+ return _Jenkins . finish ( result) ;
137
123
}
0 commit comments