|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +part of dart.ui; |
| 5 | + |
| 6 | +// Examples can assume: |
| 7 | +// int foo = 0; |
| 8 | +// int bar = 0; |
| 9 | +// List<int> quux = <int>[]; |
| 10 | +// List<int>? thud; |
| 11 | +// int baz = 0; |
| 12 | + |
| 13 | +class _HashEnd { const _HashEnd(); } |
| 14 | +const _HashEnd _hashEnd = _HashEnd(); |
| 15 | + |
| 16 | +// ignore: avoid_classes_with_only_static_members |
| 17 | +/// Jenkins hash function, optimized for small integers. |
| 18 | +// |
| 19 | +// Borrowed from the dart sdk: sdk/lib/math/jenkins_smi_hash.dart. |
| 20 | +class _Jenkins { |
| 21 | + static int combine(int hash, Object? o) { |
| 22 | + assert(o is! Iterable); |
| 23 | + hash = 0x1fffffff & (hash + o.hashCode); |
| 24 | + hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 25 | + return hash ^ (hash >> 6); |
| 26 | + } |
| 27 | + |
| 28 | + static int finish(int hash) { |
| 29 | + hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 30 | + hash = hash ^ (hash >> 11); |
| 31 | + return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Combine up to twenty objects' hash codes into one value. |
| 36 | +/// |
| 37 | +/// If you only need to handle one object's hash code, then just refer to its |
| 38 | +/// [Object.hashCode] getter directly. |
| 39 | +/// |
| 40 | +/// If you need to combine an arbitrary number of objects from a [List] or other |
| 41 | +/// [Iterable], use [hashList]. The output of [hashList] can be used as one of |
| 42 | +/// the arguments to this function. |
| 43 | +/// |
| 44 | +/// For example: |
| 45 | +/// |
| 46 | +/// ```dart |
| 47 | +/// int get hashCode => hashValues(foo, bar, hashList(quux), baz); |
| 48 | +/// ``` |
| 49 | +/// |
| 50 | +/// ## Deprecation |
| 51 | +/// |
| 52 | +/// This function has been replaced by [Object.hash], so that it can be used |
| 53 | +/// outside of Flutter as well. The new function is a drop-in replacement. |
| 54 | +/// |
| 55 | +/// The [hashList] function has also been replaced, [Object.hashAll] is the new |
| 56 | +/// function. The example above therefore is better written as: |
| 57 | +/// |
| 58 | +/// ```dart |
| 59 | +/// int get hashCode => Object.hash(foo, bar, Object.hashAll(quux), baz); |
| 60 | +/// ``` |
| 61 | +/// |
| 62 | +/// If a parameter is nullable, then it needs special handling, |
| 63 | +/// because [Object.hashAll]'s argument is not nullable: |
| 64 | +/// |
| 65 | +/// ```dart |
| 66 | +/// int get hashCode => Object.hash(foo, bar, thud == null ? null : Object.hashAll(thud!), baz); |
| 67 | +/// ``` |
| 68 | +@Deprecated( |
| 69 | + 'Use Object.hash() instead. ' |
| 70 | + 'This feature was deprecated in v3.1.0-0.0.pre.897' |
| 71 | +) |
| 72 | +int hashValues( |
| 73 | + Object? arg01, Object? arg02, [ Object? arg03 = _hashEnd, |
| 74 | + Object? arg04 = _hashEnd, Object? arg05 = _hashEnd, Object? arg06 = _hashEnd, |
| 75 | + Object? arg07 = _hashEnd, Object? arg08 = _hashEnd, Object? arg09 = _hashEnd, |
| 76 | + Object? arg10 = _hashEnd, Object? arg11 = _hashEnd, Object? arg12 = _hashEnd, |
| 77 | + Object? arg13 = _hashEnd, Object? arg14 = _hashEnd, Object? arg15 = _hashEnd, |
| 78 | + Object? arg16 = _hashEnd, Object? arg17 = _hashEnd, Object? arg18 = _hashEnd, |
| 79 | + Object? arg19 = _hashEnd, Object? arg20 = _hashEnd ]) { |
| 80 | + int result = 0; |
| 81 | + result = _Jenkins.combine(result, arg01); |
| 82 | + result = _Jenkins.combine(result, arg02); |
| 83 | + if (!identical(arg03, _hashEnd)) { |
| 84 | + result = _Jenkins.combine(result, arg03); |
| 85 | + if (!identical(arg04, _hashEnd)) { |
| 86 | + result = _Jenkins.combine(result, arg04); |
| 87 | + if (!identical(arg05, _hashEnd)) { |
| 88 | + result = _Jenkins.combine(result, arg05); |
| 89 | + if (!identical(arg06, _hashEnd)) { |
| 90 | + result = _Jenkins.combine(result, arg06); |
| 91 | + if (!identical(arg07, _hashEnd)) { |
| 92 | + result = _Jenkins.combine(result, arg07); |
| 93 | + if (!identical(arg08, _hashEnd)) { |
| 94 | + result = _Jenkins.combine(result, arg08); |
| 95 | + if (!identical(arg09, _hashEnd)) { |
| 96 | + result = _Jenkins.combine(result, arg09); |
| 97 | + if (!identical(arg10, _hashEnd)) { |
| 98 | + result = _Jenkins.combine(result, arg10); |
| 99 | + if (!identical(arg11, _hashEnd)) { |
| 100 | + result = _Jenkins.combine(result, arg11); |
| 101 | + if (!identical(arg12, _hashEnd)) { |
| 102 | + result = _Jenkins.combine(result, arg12); |
| 103 | + if (!identical(arg13, _hashEnd)) { |
| 104 | + result = _Jenkins.combine(result, arg13); |
| 105 | + if (!identical(arg14, _hashEnd)) { |
| 106 | + result = _Jenkins.combine(result, arg14); |
| 107 | + if (!identical(arg15, _hashEnd)) { |
| 108 | + result = _Jenkins.combine(result, arg15); |
| 109 | + if (!identical(arg16, _hashEnd)) { |
| 110 | + result = _Jenkins.combine(result, arg16); |
| 111 | + if (!identical(arg17, _hashEnd)) { |
| 112 | + result = _Jenkins.combine(result, arg17); |
| 113 | + if (!identical(arg18, _hashEnd)) { |
| 114 | + result = _Jenkins.combine(result, arg18); |
| 115 | + if (!identical(arg19, _hashEnd)) { |
| 116 | + result = _Jenkins.combine(result, arg19); |
| 117 | + if (!identical(arg20, _hashEnd)) { |
| 118 | + result = _Jenkins.combine(result, arg20); |
| 119 | + // I can see my house from here! |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return _Jenkins.finish(result); |
| 139 | +} |
| 140 | + |
| 141 | +/// Combine the [Object.hashCode] values of an arbitrary number of objects from |
| 142 | +/// an [Iterable] into one value. This function will return the same value if |
| 143 | +/// given null as if given an empty list. |
| 144 | +/// |
| 145 | +/// ## Deprecation |
| 146 | +/// |
| 147 | +/// This function has been replaced by [Object.hashAll], so that it can be used |
| 148 | +/// outside of Flutter as well. The new function is a drop-in replacement, except |
| 149 | +/// that the argument must not be null. |
| 150 | +/// |
| 151 | +/// There is also a new function, [Object.hashAllUnordered], which is similar |
| 152 | +/// but returns the same hash code regardless of the order of the elements in |
| 153 | +/// the provided iterable. |
| 154 | +@Deprecated( |
| 155 | + 'Use Object.hashAll() or Object.hashAllUnordered() instead. ' |
| 156 | + 'This feature was deprecated in v3.1.0-0.0.pre.897' |
| 157 | +) |
| 158 | +int hashList(Iterable<Object?>? arguments) { |
| 159 | + int result = 0; |
| 160 | + if (arguments != null) { |
| 161 | + for (final Object? argument in arguments) { |
| 162 | + result = _Jenkins.combine(result, argument); |
| 163 | + } |
| 164 | + } |
| 165 | + return _Jenkins.finish(result); |
| 166 | +} |
0 commit comments