Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 49a8dfb

Browse files
committed
Merge pull request #2165 from Hixie/hashCodes
Provide an API for hashCode getters.
2 parents 61b16cb + 6b99c21 commit 49a8dfb

File tree

6 files changed

+130
-16
lines changed

6 files changed

+130
-16
lines changed

sky/engine/core/core.gni

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,10 @@ core_idl_files = get_path_info([
258258
"abspath")
259259

260260
core_dart_files = get_path_info([
261+
"dart/hash_codes.dart",
261262
"dart/hooks.dart",
262-
"dart/natives.dart",
263263
"dart/lerp.dart",
264+
"dart/natives.dart",
264265
"dart/painting.dart",
265266
"dart/text.dart",
266267
"dart/window.dart",
@@ -276,9 +277,9 @@ core_dart_files = get_path_info([
276277
"painting/Paint.dart",
277278
"painting/PaintingStyle.dart",
278279
"painting/Point.dart",
279-
"painting/Rect.dart",
280280
"painting/RRect.dart",
281281
"painting/RSTransform.dart",
282+
"painting/Rect.dart",
282283
"painting/Size.dart",
283284
"painting/TransferMode.dart",
284285
"painting/VertexMode.dart",

sky/engine/core/dart/hash_codes.dart

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2015 The Chromium 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+
5+
part of dart_ui;
6+
7+
class _HashEnd { const _HashEnd(); }
8+
const _HashEnd _hashEnd = const _HashEnd();
9+
10+
/// Combine up to twenty values' hashCodes into one value.
11+
///
12+
/// If you only need to handle one value's hashCode, then just refer to its
13+
/// [hashCode] getter directly.
14+
///
15+
/// If you need to combine an arbitrary number of values from a List or other
16+
/// Iterable, use [hashList]. The output of hashList can be used as one of the
17+
/// arguments to this function.
18+
///
19+
/// For example:
20+
///
21+
/// int hashCode => hashValues(foo, bar, hashList(quux), baz);
22+
int hashValues(
23+
Object arg01, Object arg02, [ Object arg03 = _hashEnd,
24+
Object arg04 = _hashEnd, Object arg05 = _hashEnd, Object arg06 = _hashEnd,
25+
Object arg07 = _hashEnd, Object arg08 = _hashEnd, Object arg09 = _hashEnd,
26+
Object arg10 = _hashEnd, Object arg11 = _hashEnd, Object arg12 = _hashEnd,
27+
Object arg13 = _hashEnd, Object arg14 = _hashEnd, Object arg15 = _hashEnd,
28+
Object arg16 = _hashEnd, Object arg17 = _hashEnd, Object arg18 = _hashEnd,
29+
Object arg19 = _hashEnd, Object arg20 = _hashEnd ]) {
30+
int result = 373;
31+
assert(arg01 is! Iterable);
32+
result = 37 * result + arg01.hashCode;
33+
assert(arg02 is! Iterable);
34+
result = 37 * result + arg02.hashCode;
35+
if (arg03 != _hashEnd) {
36+
assert(arg03 is! Iterable);
37+
result = 37 * result + arg03.hashCode;
38+
if (arg04 != _hashEnd) {
39+
assert(arg04 is! Iterable);
40+
result = 37 * result + arg04.hashCode;
41+
if (arg05 != _hashEnd) {
42+
assert(arg05 is! Iterable);
43+
result = 37 * result + arg05.hashCode;
44+
if (arg06 != _hashEnd) {
45+
assert(arg06 is! Iterable);
46+
result = 37 * result + arg06.hashCode;
47+
if (arg07 != _hashEnd) {
48+
assert(arg07 is! Iterable);
49+
result = 37 * result + arg07.hashCode;
50+
if (arg08 != _hashEnd) {
51+
assert(arg08 is! Iterable);
52+
result = 37 * result + arg08.hashCode;
53+
if (arg09 != _hashEnd) {
54+
assert(arg09 is! Iterable);
55+
result = 37 * result + arg09.hashCode;
56+
if (arg10 != _hashEnd) {
57+
assert(arg10 is! Iterable);
58+
result = 37 * result + arg10.hashCode;
59+
if (arg11 != _hashEnd) {
60+
assert(arg11 is! Iterable);
61+
result = 37 * result + arg11.hashCode;
62+
if (arg12 != _hashEnd) {
63+
assert(arg12 is! Iterable);
64+
result = 37 * result + arg12.hashCode;
65+
if (arg13 != _hashEnd) {
66+
assert(arg13 is! Iterable);
67+
result = 37 * result + arg13.hashCode;
68+
if (arg14 != _hashEnd) {
69+
assert(arg14 is! Iterable);
70+
result = 37 * result + arg14.hashCode;
71+
if (arg15 != _hashEnd) {
72+
assert(arg15 is! Iterable);
73+
result = 37 * result + arg15.hashCode;
74+
if (arg16 != _hashEnd) {
75+
assert(arg16 is! Iterable);
76+
result = 37 * result + arg16.hashCode;
77+
if (arg17 != _hashEnd) {
78+
assert(arg17 is! Iterable);
79+
result = 37 * result + arg17.hashCode;
80+
if (arg18 != _hashEnd) {
81+
assert(arg18 is! Iterable);
82+
result = 37 * result + arg18.hashCode;
83+
if (arg19 != _hashEnd) {
84+
assert(arg19 is! Iterable);
85+
result = 37 * result + arg19.hashCode;
86+
if (arg20 != _hashEnd) {
87+
assert(arg20 is! Iterable);
88+
result = 37 * result + arg20.hashCode;
89+
// I can see my house from here!
90+
}
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}
104+
}
105+
}
106+
}
107+
}
108+
return result;
109+
}
110+
111+
/// Combine the hashCodes of an arbitrary number of values from an Iterable into
112+
/// one value. This function will return the same value if given "null" as if
113+
/// given an empty list.
114+
int hashList(Iterable<Object> args) {
115+
int result = 373;
116+
if (args != null) {
117+
for (Object arg in args) {
118+
assert(arg is! Iterable);
119+
result = 37 * result + arg.hashCode;
120+
}
121+
}
122+
return result;
123+
}

sky/engine/core/painting/OffsetBase.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,5 @@ abstract class OffsetBase {
2525
_dy == typedOther._dy;
2626
}
2727

28-
int get hashCode {
29-
int result = 373;
30-
result = 37 * result + _dx.hashCode;
31-
result = 37 * result + _dy.hashCode;
32-
return result;
33-
}
28+
int get hashCode => hashValues(_dx, _dy);
3429
}

sky/engine/core/painting/Point.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ class Point {
4747
y == typedOther.y;
4848
}
4949

50-
int get hashCode {
51-
int result = 373;
52-
result = 37 * result + x.hashCode;
53-
result = 37 * result + y.hashCode;
54-
return result;
55-
}
50+
int get hashCode => hashValues(x, y);
5651

5752
String toString() => "Point(${x.toStringAsFixed(1)}, ${y.toStringAsFixed(1)})";
5853
}

sky/engine/core/painting/RRect.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class RRect {
229229
return true;
230230
}
231231

232-
int get hashCode => _value.fold(373, (value, item) => (37 * value + item.hashCode));
232+
int get hashCode => hashList(_value);
233233

234234
String toString() => "RRect.fromLTRBXY(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)}, ${radiusX.toStringAsFixed(1)}, ${radiusY.toStringAsFixed(1)})";
235235
}

sky/engine/core/painting/Rect.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Rect {
143143
return true;
144144
}
145145

146-
int get hashCode => _value.fold(373, (value, item) => (37 * value + item.hashCode));
146+
int get hashCode => hashList(_value);
147147

148148
String toString() => "Rect.fromLTRB(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)})";
149149
}

0 commit comments

Comments
 (0)