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 /// Some common utilities used by other libraries in this package. | 5 /// Some common utilities used by other libraries in this package. |
6 library smoke.src.common; | 6 library smoke.src.common; |
7 | 7 |
8 import 'package:smoke/smoke.dart' as smoke show isSubclassOf; | 8 import 'package:smoke/smoke.dart' as smoke show isSubclassOf; |
9 | 9 |
10 /// Returns [input] adjusted to be within [min] and [max] length. Truncating it | 10 /// Returns [input] adjusted to be within [min] and [max] length. Truncating it |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 if (f is _Func0) return 0; | 77 if (f is _Func0) return 0; |
78 return -1; | 78 return -1; |
79 } | 79 } |
80 | 80 |
81 /// Shallow comparison of two lists. | 81 /// Shallow comparison of two lists. |
82 bool compareLists(List a, List b, {bool unordered: false}) { | 82 bool compareLists(List a, List b, {bool unordered: false}) { |
83 if (a == null && b != null) return false; | 83 if (a == null && b != null) return false; |
84 if (a != null && b == null) return false; | 84 if (a != null && b == null) return false; |
85 if (a.length != b.length) return false; | 85 if (a.length != b.length) return false; |
86 if (unordered) { | 86 if (unordered) { |
87 var bSet = new Set()..addAll(b); | 87 var countMap = {}; |
88 for (int i = 0; i < a.length; i++) { | 88 for (var x in b) { |
89 if (!bSet.contains(a[i])) return false; | 89 var count = countMap[x]; |
| 90 if (count == null) count = 0; |
| 91 countMap[x] = count + 1; |
90 } | 92 } |
| 93 for (var x in a) { |
| 94 var count = countMap[x]; |
| 95 if (count == null) return false; |
| 96 if (count == 1) { |
| 97 countMap.remove(x); |
| 98 } else { |
| 99 countMap[x] = count - 1; |
| 100 } |
| 101 } |
| 102 return countMap.isEmpty; |
91 } else { | 103 } else { |
92 for (int i = 0; i < a.length; i++) { | 104 for (int i = 0; i < a.length; i++) { |
93 if (a[i] != b[i]) return false; | 105 if (a[i] != b[i]) return false; |
94 } | 106 } |
95 } | 107 } |
96 return true; | 108 return true; |
97 } | 109 } |
98 | 110 |
99 /// Shallow comparison of two maps. | 111 /// Shallow comparison of two maps. |
100 bool compareMaps(Map a, Map b) { | 112 bool compareMaps(Map a, Map b) { |
101 if (a == null && b != null) return false; | 113 if (a == null && b != null) return false; |
102 if (a != null && b == null) return false; | 114 if (a != null && b == null) return false; |
103 if (a.length != b.length) return false; | 115 if (a.length != b.length) return false; |
104 for (var k in a.keys) { | 116 for (var k in a.keys) { |
105 if (!b.containsKey(k) || a[k] != b[k]) return false; | 117 if (!b.containsKey(k) || a[k] != b[k]) return false; |
106 } | 118 } |
107 return true; | 119 return true; |
108 } | 120 } |
OLD | NEW |