From 6b99c214b2e1019b3768abb5f1f703454e5221d4 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Wed, 9 Dec 2015 22:00:16 -0800 Subject: [PATCH] Provide an API for hashCode getters. --- sky/engine/core/core.gni | 5 +- sky/engine/core/dart/hash_codes.dart | 123 +++++++++++++++++++++++ sky/engine/core/painting/OffsetBase.dart | 7 +- sky/engine/core/painting/Point.dart | 7 +- sky/engine/core/painting/RRect.dart | 2 +- sky/engine/core/painting/Rect.dart | 2 +- 6 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 sky/engine/core/dart/hash_codes.dart diff --git a/sky/engine/core/core.gni b/sky/engine/core/core.gni index 97f832567..c21793699 100644 --- a/sky/engine/core/core.gni +++ b/sky/engine/core/core.gni @@ -258,9 +258,10 @@ core_idl_files = get_path_info([ "abspath") core_dart_files = get_path_info([ + "dart/hash_codes.dart", "dart/hooks.dart", - "dart/natives.dart", "dart/lerp.dart", + "dart/natives.dart", "dart/painting.dart", "dart/text.dart", "dart/window.dart", @@ -276,9 +277,9 @@ core_dart_files = get_path_info([ "painting/Paint.dart", "painting/PaintingStyle.dart", "painting/Point.dart", - "painting/Rect.dart", "painting/RRect.dart", "painting/RSTransform.dart", + "painting/Rect.dart", "painting/Size.dart", "painting/TransferMode.dart", "painting/VertexMode.dart", diff --git a/sky/engine/core/dart/hash_codes.dart b/sky/engine/core/dart/hash_codes.dart new file mode 100644 index 000000000..c09cf82c3 --- /dev/null +++ b/sky/engine/core/dart/hash_codes.dart @@ -0,0 +1,123 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of dart_ui; + +class _HashEnd { const _HashEnd(); } +const _HashEnd _hashEnd = const _HashEnd(); + +/// Combine up to twenty values' hashCodes into one value. +/// +/// If you only need to handle one value's hashCode, then just refer to its +/// [hashCode] getter directly. +/// +/// If you need to combine an arbitrary number of values from a List or other +/// Iterable, use [hashList]. The output of hashList can be used as one of the +/// arguments to this function. +/// +/// For example: +/// +/// int hashCode => hashValues(foo, bar, hashList(quux), baz); +int hashValues( + Object arg01, Object arg02, [ Object arg03 = _hashEnd, + Object arg04 = _hashEnd, Object arg05 = _hashEnd, Object arg06 = _hashEnd, + Object arg07 = _hashEnd, Object arg08 = _hashEnd, Object arg09 = _hashEnd, + Object arg10 = _hashEnd, Object arg11 = _hashEnd, Object arg12 = _hashEnd, + Object arg13 = _hashEnd, Object arg14 = _hashEnd, Object arg15 = _hashEnd, + Object arg16 = _hashEnd, Object arg17 = _hashEnd, Object arg18 = _hashEnd, + Object arg19 = _hashEnd, Object arg20 = _hashEnd ]) { + int result = 373; + assert(arg01 is! Iterable); + result = 37 * result + arg01.hashCode; + assert(arg02 is! Iterable); + result = 37 * result + arg02.hashCode; + if (arg03 != _hashEnd) { + assert(arg03 is! Iterable); + result = 37 * result + arg03.hashCode; + if (arg04 != _hashEnd) { + assert(arg04 is! Iterable); + result = 37 * result + arg04.hashCode; + if (arg05 != _hashEnd) { + assert(arg05 is! Iterable); + result = 37 * result + arg05.hashCode; + if (arg06 != _hashEnd) { + assert(arg06 is! Iterable); + result = 37 * result + arg06.hashCode; + if (arg07 != _hashEnd) { + assert(arg07 is! Iterable); + result = 37 * result + arg07.hashCode; + if (arg08 != _hashEnd) { + assert(arg08 is! Iterable); + result = 37 * result + arg08.hashCode; + if (arg09 != _hashEnd) { + assert(arg09 is! Iterable); + result = 37 * result + arg09.hashCode; + if (arg10 != _hashEnd) { + assert(arg10 is! Iterable); + result = 37 * result + arg10.hashCode; + if (arg11 != _hashEnd) { + assert(arg11 is! Iterable); + result = 37 * result + arg11.hashCode; + if (arg12 != _hashEnd) { + assert(arg12 is! Iterable); + result = 37 * result + arg12.hashCode; + if (arg13 != _hashEnd) { + assert(arg13 is! Iterable); + result = 37 * result + arg13.hashCode; + if (arg14 != _hashEnd) { + assert(arg14 is! Iterable); + result = 37 * result + arg14.hashCode; + if (arg15 != _hashEnd) { + assert(arg15 is! Iterable); + result = 37 * result + arg15.hashCode; + if (arg16 != _hashEnd) { + assert(arg16 is! Iterable); + result = 37 * result + arg16.hashCode; + if (arg17 != _hashEnd) { + assert(arg17 is! Iterable); + result = 37 * result + arg17.hashCode; + if (arg18 != _hashEnd) { + assert(arg18 is! Iterable); + result = 37 * result + arg18.hashCode; + if (arg19 != _hashEnd) { + assert(arg19 is! Iterable); + result = 37 * result + arg19.hashCode; + if (arg20 != _hashEnd) { + assert(arg20 is! Iterable); + result = 37 * result + arg20.hashCode; + // I can see my house from here! + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + return result; +} + +/// Combine the hashCodes of an arbitrary number of values from an Iterable into +/// one value. This function will return the same value if given "null" as if +/// given an empty list. +int hashList(Iterable args) { + int result = 373; + if (args != null) { + for (Object arg in args) { + assert(arg is! Iterable); + result = 37 * result + arg.hashCode; + } + } + return result; +} diff --git a/sky/engine/core/painting/OffsetBase.dart b/sky/engine/core/painting/OffsetBase.dart index b48de8454..f753d6434 100644 --- a/sky/engine/core/painting/OffsetBase.dart +++ b/sky/engine/core/painting/OffsetBase.dart @@ -25,10 +25,5 @@ abstract class OffsetBase { _dy == typedOther._dy; } - int get hashCode { - int result = 373; - result = 37 * result + _dx.hashCode; - result = 37 * result + _dy.hashCode; - return result; - } + int get hashCode => hashValues(_dx, _dy); } diff --git a/sky/engine/core/painting/Point.dart b/sky/engine/core/painting/Point.dart index 22a32b108..be8bc2140 100644 --- a/sky/engine/core/painting/Point.dart +++ b/sky/engine/core/painting/Point.dart @@ -47,12 +47,7 @@ class Point { y == typedOther.y; } - int get hashCode { - int result = 373; - result = 37 * result + x.hashCode; - result = 37 * result + y.hashCode; - return result; - } + int get hashCode => hashValues(x, y); String toString() => "Point(${x.toStringAsFixed(1)}, ${y.toStringAsFixed(1)})"; } diff --git a/sky/engine/core/painting/RRect.dart b/sky/engine/core/painting/RRect.dart index 10b5c7958..40ef79019 100644 --- a/sky/engine/core/painting/RRect.dart +++ b/sky/engine/core/painting/RRect.dart @@ -229,7 +229,7 @@ class RRect { return true; } - int get hashCode => _value.fold(373, (value, item) => (37 * value + item.hashCode)); + int get hashCode => hashList(_value); String toString() => "RRect.fromLTRBXY(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)}, ${radiusX.toStringAsFixed(1)}, ${radiusY.toStringAsFixed(1)})"; } diff --git a/sky/engine/core/painting/Rect.dart b/sky/engine/core/painting/Rect.dart index cfff95995..01ace79ab 100644 --- a/sky/engine/core/painting/Rect.dart +++ b/sky/engine/core/painting/Rect.dart @@ -143,7 +143,7 @@ class Rect { return true; } - int get hashCode => _value.fold(373, (value, item) => (37 * value + item.hashCode)); + int get hashCode => hashList(_value); String toString() => "Rect.fromLTRB(${left.toStringAsFixed(1)}, ${top.toStringAsFixed(1)}, ${right.toStringAsFixed(1)}, ${bottom.toStringAsFixed(1)})"; } -- GitLab