|
| 1 | +// Copyright (c) 2022, the MarchDev Toolkit project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +// TODO: move into separate package |
| 6 | + |
| 7 | +class MdEquals { |
| 8 | + const MdEquals._(); |
| 9 | + |
| 10 | + static bool map<T, U>(Map<T, U>? a, Map<T, U>? b) { |
| 11 | + if (a == null) { |
| 12 | + return b == null; |
| 13 | + } |
| 14 | + if (b == null || a.length != b.length) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + if (identical(a, b)) { |
| 18 | + return true; |
| 19 | + } |
| 20 | + for (final T key in a.keys) { |
| 21 | + final av = a[key]; |
| 22 | + final bv = b[key]; |
| 23 | + if (!b.containsKey(key)) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + if (bv.runtimeType != av.runtimeType) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + if (av is Map && bv is Map) { |
| 30 | + return map(av, bv); |
| 31 | + } |
| 32 | + if (av is List && bv is List) { |
| 33 | + return list(av, bv); |
| 34 | + } |
| 35 | + if (av is Set && bv is Set) { |
| 36 | + return set(av, bv); |
| 37 | + } |
| 38 | + } |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + static bool list<T>(List<T>? a, List<T>? b) { |
| 43 | + if (a == null) { |
| 44 | + return b == null; |
| 45 | + } |
| 46 | + if (b == null || a.length != b.length) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + if (identical(a, b)) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + for (int index = 0; index < a.length; index += 1) { |
| 53 | + final av = a[index]; |
| 54 | + final bv = b[index]; |
| 55 | + if (bv.runtimeType != av.runtimeType) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + if (av is Map && bv is Map) { |
| 59 | + return map(av, bv); |
| 60 | + } |
| 61 | + if (av is List && bv is List) { |
| 62 | + return list(av, bv); |
| 63 | + } |
| 64 | + if (av is Set && bv is Set) { |
| 65 | + return set(av, bv); |
| 66 | + } |
| 67 | + if (av != bv) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + static bool set<T>(Set<T>? a, Set<T>? b) { |
| 75 | + if (a == null) { |
| 76 | + return b == null; |
| 77 | + } |
| 78 | + if (b == null || a.length != b.length) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + if (identical(a, b)) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + for (final T av in a) { |
| 85 | + if (!b.contains(av)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + return true; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +class MdHash { |
| 94 | + const MdHash._(); |
| 95 | + |
| 96 | + static int _hash<T>(T item) { |
| 97 | + int hash = item.hashCode; |
| 98 | + |
| 99 | + if (item is Map) { |
| 100 | + hash = map(item); |
| 101 | + } |
| 102 | + if (item is List) { |
| 103 | + hash = list(item); |
| 104 | + } |
| 105 | + if (item is Set) { |
| 106 | + hash = set(item); |
| 107 | + } |
| 108 | + |
| 109 | + return hash; |
| 110 | + } |
| 111 | + |
| 112 | + static int map<T, U>(Map<T, U>? m) { |
| 113 | + if (m == null || m.isEmpty) { |
| 114 | + return 0; |
| 115 | + } |
| 116 | + |
| 117 | + int hash = 0; |
| 118 | + for (final key in m.keys) { |
| 119 | + final value = m[key]; |
| 120 | + hash ^= _hash(key) ^ _hash(value); |
| 121 | + } |
| 122 | + |
| 123 | + return hash; |
| 124 | + } |
| 125 | + |
| 126 | + static int list<T>(List<T>? l) { |
| 127 | + if (l == null || l.isEmpty) { |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + int hash = 0; |
| 132 | + for (final value in l) { |
| 133 | + hash ^= _hash(value); |
| 134 | + } |
| 135 | + |
| 136 | + return hash; |
| 137 | + } |
| 138 | + |
| 139 | + static int set<T>(Set<T>? s) { |
| 140 | + if (s == null || s.isEmpty) { |
| 141 | + return 0; |
| 142 | + } |
| 143 | + |
| 144 | + int hash = 0; |
| 145 | + for (final value in s) { |
| 146 | + hash ^= _hash(value); |
| 147 | + } |
| 148 | + |
| 149 | + return hash; |
| 150 | + } |
| 151 | +} |
0 commit comments