Line data Source code
1 : // Copyright 2026 The Authors
2 : //
3 : // Licensed under the Apache License, Version 2.0 (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
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : /// Return true if all items in [a] are also in [b]
16 : ///
17 : /// This is different to `ListEquality` in the
18 : /// [`collection`](https://pub.dev/packages/collection) package
19 : /// as this function does not require the list elements to be in the
20 : /// same order.
21 2 : bool hasTheSameElements<T>(Iterable<T> a, Iterable<T> b) {
22 6 : if (a.length != b.length) {
23 : return false;
24 : }
25 :
26 4 : var checklist = List<bool>.filled(b.length, false, growable: false);
27 :
28 6 : for (int i = 0; i < a.length; i++) {
29 : var found = false;
30 6 : for (var j = 0; j < b.length; j++) {
31 2 : if (checklist[j]) continue;
32 :
33 6 : if (a.elementAt(i) == b.elementAt(j)) {
34 2 : checklist[j] = true;
35 : found = true;
36 : break;
37 : }
38 : }
39 : if (!found) {
40 : return false;
41 : }
42 : }
43 :
44 : return true;
45 : }
46 :
47 : /// Return true if [a] is a sublist of [b].
48 : ///
49 : /// Does not care if the lists are in the same order.
50 2 : bool isSubList<T>(List<T> a, List<T> b) {
51 6 : if (a.length > b.length) {
52 : return false;
53 : }
54 :
55 4 : var checklist = List<bool>.filled(b.length, false, growable: false);
56 :
57 6 : for (var i = 0; i < a.length; i++) {
58 : var found = false;
59 6 : for (var j = 0; j < b.length; j++) {
60 2 : if (checklist[j]) continue;
61 :
62 6 : if (a[i] == b[j]) {
63 2 : checklist[j] = true;
64 : found = true;
65 : break;
66 : }
67 : }
68 : if (!found) return false;
69 : }
70 : return true;
71 : }
|