| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library engine.utilities.general; | 5 library engine.utilities.general; |
| 6 | 6 |
| 7 import 'dart:collection'; |
| 7 import 'dart:developer' show UserTag; | 8 import 'dart:developer' show UserTag; |
| 8 | 9 |
| 9 /** | 10 /** |
| 10 * Jenkins hash function, optimized for small integers. | 11 * Jenkins hash function, optimized for small integers. |
| 11 * Borrowed from sdk/lib/math/jenkins_smi_hash.dart. | 12 * Borrowed from sdk/lib/math/jenkins_smi_hash.dart. |
| 12 */ | 13 */ |
| 13 class JenkinsSmiHash { | 14 class JenkinsSmiHash { |
| 14 static int combine(int hash, int value) { | 15 static int combine(int hash, int value) { |
| 15 hash = 0x1fffffff & (hash + value); | 16 hash = 0x1fffffff & (hash + value); |
| 16 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | 17 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 17 return hash ^ (hash >> 6); | 18 return hash ^ (hash >> 6); |
| 18 } | 19 } |
| 19 | 20 |
| 20 static int finish(int hash) { | 21 static int finish(int hash) { |
| 21 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 22 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 22 hash = hash ^ (hash >> 11); | 23 hash = hash ^ (hash >> 11); |
| 23 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 24 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 24 } | 25 } |
| 25 | 26 |
| 26 static int hash2(a, b) => finish(combine(combine(0, a), b)); | 27 static int hash2(a, b) => finish(combine(combine(0, a), b)); |
| 27 | 28 |
| 28 static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c)); | 29 static int hash3(a, b, c) => finish(combine(combine(combine(0, a), b), c)); |
| 29 | 30 |
| 30 static int hash4(a, b, c, d) => | 31 static int hash4(a, b, c, d) => |
| 31 finish(combine(combine(combine(combine(0, a), b), c), d)); | 32 finish(combine(combine(combine(combine(0, a), b), c), d)); |
| 32 } | 33 } |
| 33 | 34 |
| 34 /** | 35 /** |
| 36 * A simple limited queue. |
| 37 */ |
| 38 class LimitedQueue<E> extends ListQueue<E> { |
| 39 final int limit; |
| 40 |
| 41 /** |
| 42 * Create a queue with [limit] items. |
| 43 */ |
| 44 LimitedQueue(this.limit); |
| 45 |
| 46 @override |
| 47 void add(E o) { |
| 48 super.add(o); |
| 49 while (length > limit) { |
| 50 remove(first); |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 55 /** |
| 35 * Helper class for gathering performance statistics. This class is modeled on | 56 * Helper class for gathering performance statistics. This class is modeled on |
| 36 * the UserTag class in dart:developer so that it can interoperate easily with | 57 * the UserTag class in dart:developer so that it can interoperate easily with |
| 37 * it. | 58 * it. |
| 38 */ | 59 */ |
| 39 abstract class PerformanceTag { | 60 abstract class PerformanceTag { |
| 40 /** | 61 /** |
| 41 * Return a list of all [PerformanceTag]s which have been created. | 62 * Return a list of all [PerformanceTag]s which have been created. |
| 42 */ | 63 */ |
| 43 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); | 64 static List<PerformanceTag> get all => _PerformanceTagImpl.all.toList(); |
| 44 | 65 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 166 |
| 146 makeCurrentWhile(f()) { | 167 makeCurrentWhile(f()) { |
| 147 PerformanceTag prevTag = makeCurrent(); | 168 PerformanceTag prevTag = makeCurrent(); |
| 148 try { | 169 try { |
| 149 return f(); | 170 return f(); |
| 150 } finally { | 171 } finally { |
| 151 prevTag.makeCurrent(); | 172 prevTag.makeCurrent(); |
| 152 } | 173 } |
| 153 } | 174 } |
| 154 } | 175 } |
| OLD | NEW |