|
5 | 5 | import 'dart:collection'; |
6 | 6 | import 'dart:math' as math; |
7 | 7 |
|
| 8 | +import 'list_extensions.dart' show ListExtensions; // For `reverseRange`. |
8 | 9 | import 'utils.dart'; |
9 | 10 |
|
10 | | -/// Creates a new map from [map] with new keys and values. |
11 | | -/// |
12 | | -/// The return values of [key] are used as the keys and the return values of |
13 | | -/// [value] are used as the values for the new map. |
14 | | -@Deprecated('Use Map.map or a for loop in a Map literal.') |
15 | | -Map<K2, V2> mapMap<K1, V1, K2, V2>(Map<K1, V1> map, |
16 | | - {K2 Function(K1, V1)? key, V2 Function(K1, V1)? value}) { |
17 | | - var keyFn = key ?? (mapKey, _) => mapKey as K2; |
18 | | - var valueFn = value ?? (_, mapValue) => mapValue as V2; |
19 | | - |
20 | | - var result = <K2, V2>{}; |
21 | | - map.forEach((mapKey, mapValue) { |
22 | | - result[keyFn(mapKey, mapValue)] = valueFn(mapKey, mapValue); |
23 | | - }); |
24 | | - return result; |
25 | | -} |
26 | | - |
27 | 11 | /// Returns a new map with all key/value pairs in both [map1] and [map2]. |
28 | 12 | /// |
29 | 13 | /// If there are keys that occur in both maps, the [value] function is used to |
@@ -109,45 +93,6 @@ S? maxBy<S, T>(Iterable<S> values, T Function(S) orderBy, |
109 | 93 | return maxValue; |
110 | 94 | } |
111 | 95 |
|
112 | | -/// Returns the [transitive closure][] of [graph]. |
113 | | -/// |
114 | | -/// [transitive closure]: https://en.wikipedia.org/wiki/Transitive_closure |
115 | | -/// |
116 | | -/// Interprets [graph] as a directed graph with a vertex for each key and edges |
117 | | -/// from each key to the values that the key maps to. |
118 | | -/// |
119 | | -/// Assumes that every vertex in the graph has a key to represent it, even if |
120 | | -/// that vertex has no outgoing edges. This isn't checked, but if it's not |
121 | | -/// satisfied, the function may crash or provide unexpected output. For example, |
122 | | -/// `{"a": ["b"]}` is not valid, but `{"a": ["b"], "b": []}` is. |
123 | | -@Deprecated('This method will be removed. Consider using package:graphs.') |
124 | | -Map<T, Set<T>> transitiveClosure<T>(Map<T, Iterable<T>> graph) { |
125 | | - // This uses [Warshall's algorithm][], modified not to add a vertex from each |
126 | | - // node to itself. |
127 | | - // |
128 | | - // [Warshall's algorithm]: https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm#Applications_and_generalizations. |
129 | | - var result = <T, Set<T>>{}; |
130 | | - graph.forEach((vertex, edges) { |
131 | | - result[vertex] = Set<T>.from(edges); |
132 | | - }); |
133 | | - |
134 | | - // Lists are faster to iterate than maps, so we create a list since we're |
135 | | - // iterating repeatedly. |
136 | | - var keys = graph.keys.toList(); |
137 | | - for (var vertex1 in keys) { |
138 | | - for (var vertex2 in keys) { |
139 | | - for (var vertex3 in keys) { |
140 | | - if (result[vertex2]!.contains(vertex1) && |
141 | | - result[vertex1]!.contains(vertex3)) { |
142 | | - result[vertex2]!.add(vertex3); |
143 | | - } |
144 | | - } |
145 | | - } |
146 | | - } |
147 | | - |
148 | | - return result; |
149 | | -} |
150 | | - |
151 | 96 | /// Returns the [strongly connected components][] of [graph], in topological |
152 | 97 | /// order. |
153 | 98 | /// |
@@ -209,5 +154,5 @@ List<Set<T>> stronglyConnectedComponents<T>(Map<T, Iterable<T>> graph) { |
209 | 154 |
|
210 | 155 | // Tarjan's algorithm produces a reverse-topological sort, so we reverse it to |
211 | 156 | // get a normal topological sort. |
212 | | - return result.reversed.toList(); |
| 157 | + return result..reverseRange(0, result.length); |
213 | 158 | } |
0 commit comments