OLD | NEW |
1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 /** | 15 /// Collection classes and related utilities. |
16 * Collection classes and related utilities. | |
17 */ | |
18 library quiver.collection; | 16 library quiver.collection; |
19 | 17 |
20 import 'dart:collection'; | 18 import 'dart:collection'; |
21 import 'dart:math'; | 19 import 'dart:math'; |
22 | 20 |
23 import 'package:quiver/core.dart'; | 21 import 'package:quiver/core.dart'; |
24 import 'package:quiver/iterables.dart'; | 22 import 'package:quiver/iterables.dart'; |
25 | 23 |
26 part 'src/collection/bimap.dart'; | 24 part 'src/collection/bimap.dart'; |
27 part 'src/collection/lru_map.dart'; | 25 part 'src/collection/lru_map.dart'; |
28 part 'src/collection/multimap.dart'; | 26 part 'src/collection/multimap.dart'; |
29 part 'src/collection/treeset.dart'; | 27 part 'src/collection/treeset.dart'; |
30 part 'src/collection/delegates/iterable.dart'; | 28 part 'src/collection/delegates/iterable.dart'; |
31 part 'src/collection/delegates/list.dart'; | 29 part 'src/collection/delegates/list.dart'; |
32 part 'src/collection/delegates/map.dart'; | 30 part 'src/collection/delegates/map.dart'; |
33 part 'src/collection/delegates/queue.dart'; | 31 part 'src/collection/delegates/queue.dart'; |
34 part 'src/collection/delegates/set.dart'; | 32 part 'src/collection/delegates/set.dart'; |
35 | 33 |
36 /** | 34 /// Checks [List]s [a] and [b] for equality. |
37 * Checks [List]s [a] and [b] for equality. | 35 /// |
38 * | 36 /// Returns `true` if [a] and [b] are both null, or they are the same length |
39 * Returns `true` if [a] and [b] are both null, or they are the same length and | 37 /// and every element of [a] is equal to the corresponding element at the same |
40 * every element of [a] is equal to the corresponding element at the same index | 38 /// index in [b]. |
41 * in [b]. | |
42 */ | |
43 bool listsEqual(List a, List b) { | 39 bool listsEqual(List a, List b) { |
44 if (a == b) return true; | 40 if (a == b) return true; |
45 if (a == null || b == null) return false; | 41 if (a == null || b == null) return false; |
46 if (a.length != b.length) return false; | 42 if (a.length != b.length) return false; |
47 | 43 |
48 for (int i = 0; i < a.length; i++) { | 44 for (int i = 0; i < a.length; i++) { |
49 if (a[i] != b[i]) return false; | 45 if (a[i] != b[i]) return false; |
50 } | 46 } |
51 | 47 |
52 return true; | 48 return true; |
53 } | 49 } |
54 | 50 |
55 /** | 51 /// Checks [Map]s [a] and [b] for equality. |
56 * Checks [Map]s [a] and [b] for equality. | 52 /// |
57 * | 53 /// Returns `true` if [a] and [b] are both null, or they are the same length |
58 * Returns `true` if [a] and [b] are both null, or they are the same length and | 54 /// and every key `k` in [a] exists in [b] and the values `a[k] == b[k]`. |
59 * every key `k` in [a] exists in [b] and the values `a[k] == b[k]`. | |
60 */ | |
61 bool mapsEqual(Map a, Map b) { | 55 bool mapsEqual(Map a, Map b) { |
62 if (a == b) return true; | 56 if (a == b) return true; |
63 if (a == null || b == null) return false; | 57 if (a == null || b == null) return false; |
64 if (a.length != b.length) return false; | 58 if (a.length != b.length) return false; |
65 | 59 |
66 for (var k in a.keys) { | 60 for (var k in a.keys) { |
67 var bValue = b[k]; | 61 var bValue = b[k]; |
68 if (bValue == null && !b.containsKey(k)) return false; | 62 if (bValue == null && !b.containsKey(k)) return false; |
69 if (bValue != a[k]) return false; | 63 if (bValue != a[k]) return false; |
70 } | 64 } |
71 | 65 |
72 return true; | 66 return true; |
73 } | 67 } |
74 | 68 |
75 /** | 69 /// Checks [Set]s [a] and [b] for equality. |
76 * Checks [Set]s [a] and [b] for equality. | 70 /// |
77 * | 71 /// Returns `true` if [a] and [b] are both null, or they are the same length and |
78 * Returns `true` if [a] and [b] are both null, or they are the same length and | 72 /// every element in [b] exists in [a]. |
79 * every element in [b] exists in [a]. | |
80 */ | |
81 bool setsEqual(Set a, Set b) { | 73 bool setsEqual(Set a, Set b) { |
82 if (a == b) return true; | 74 if (a == b) return true; |
83 if (a == null || b == null) return false; | 75 if (a == null || b == null) return false; |
84 if (a.length != b.length) return false; | 76 if (a.length != b.length) return false; |
85 | 77 |
86 return a.containsAll(b); | 78 return a.containsAll(b); |
87 } | 79 } |
| 80 |
| 81 /// Returns the index of the first item in [elements] where [predicate] |
| 82 /// evaluates to true. |
| 83 /// |
| 84 /// Returns -1 if there are no items where [predicate] evaluates to true. |
| 85 int indexOf<T>(Iterable<T> elements, bool predicate(T element)) { |
| 86 if (elements is List<T>) { |
| 87 for (int i = 0; i < elements.length; i++) { |
| 88 if (predicate(elements[i])) return i; |
| 89 } |
| 90 return -1; |
| 91 } |
| 92 |
| 93 int i = 0; |
| 94 for (T element in elements) { |
| 95 if (predicate(element)) return i; |
| 96 i++; |
| 97 } |
| 98 return -1; |
| 99 } |
OLD | NEW |