| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * Helper class which implements complex [Map] operations | 8 * Helper class which implements complex [Map] operations |
| 9 * in term of basic ones ([Map.keys], [Map.operator []], | 9 * in term of basic ones ([Map.keys], [Map.operator []], |
| 10 * [Map.operator []=] and [Map.remove].) Not all methods are | 10 * [Map.operator []=] and [Map.remove].) Not all methods are |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 static Iterable getValues(Map map) { | 53 static Iterable getValues(Map map) { |
| 54 return map.keys.map((key) => map[key]); | 54 return map.keys.map((key) => map[key]); |
| 55 } | 55 } |
| 56 | 56 |
| 57 static int length(Map map) => map.keys.length; | 57 static int length(Map map) => map.keys.length; |
| 58 | 58 |
| 59 static bool isEmpty(Map map) => map.keys.isEmpty; | 59 static bool isEmpty(Map map) => map.keys.isEmpty; |
| 60 | 60 |
| 61 static bool isNotEmpty(Map map) => map.keys.isNotEmpty; | 61 static bool isNotEmpty(Map map) => map.keys.isNotEmpty; |
| 62 | 62 |
| 63 // A list to identify cyclic maps during toString() calls. | |
| 64 static List _toStringList = new List(); | |
| 65 | |
| 66 /** | 63 /** |
| 67 * Returns a string representing the specified map. The returned string | 64 * Returns a string representing the specified map. The returned string |
| 68 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':]. | 65 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':]. |
| 69 * The value returned by its [toString] method is used to represent each | 66 * The value returned by its [toString] method is used to represent each |
| 70 * key or value. | 67 * key or value. |
| 71 * | 68 * |
| 72 * If the map collection contains a reference to itself, either | 69 * If the map collection contains a reference to itself, either |
| 73 * directly as a key or value, or indirectly through other collections | 70 * directly as a key or value, or indirectly through other collections |
| 74 * or maps, the contained reference is rendered as [:'{...}':]. This | 71 * or maps, the contained reference is rendered as [:'{...}':]. This |
| 75 * prevents the infinite regress that would otherwise occur. So, for example, | 72 * prevents the infinite regress that would otherwise occur. So, for example, |
| 76 * calling this method on a map whose sole entry maps the string key 'me' | 73 * calling this method on a map whose sole entry maps the string key 'me' |
| 77 * to a reference to the map would return [:'{me: {...}}':]. | 74 * to a reference to the map would return [:'{me: {...}}':]. |
| 78 * | 75 * |
| 79 * A typical implementation of a map's [toString] method will | 76 * A typical implementation of a map's [toString] method will |
| 80 * simply return the results of this method applied to the collection. | 77 * simply return the results of this method applied to the collection. |
| 81 */ | 78 */ |
| 82 static String mapToString(Map m) { | 79 static String mapToString(Map m) => ToString.mapToString(m); |
| 83 for (int i = 0; i < _toStringList.length; i++) { | |
| 84 if (identical(_toStringList[i], m)) { return '{...}'; } | |
| 85 } | |
| 86 | |
| 87 var result = new StringBuffer(); | |
| 88 try { | |
| 89 _toStringList.add(m); | |
| 90 result.write('{'); | |
| 91 bool first = true; | |
| 92 m.forEach((k, v) { | |
| 93 if(!first) { | |
| 94 result.write(', '); | |
| 95 } | |
| 96 first = false; | |
| 97 result.write(k); | |
| 98 result.write(': '); | |
| 99 result.write(v); | |
| 100 }); | |
| 101 result.write('}'); | |
| 102 } finally { | |
| 103 assert(identical(_toStringList.last, m)); | |
| 104 _toStringList.removeLast(); | |
| 105 } | |
| 106 | |
| 107 return result.toString(); | |
| 108 } | |
| 109 | 80 |
| 110 static _id(x) => x; | 81 static _id(x) => x; |
| 111 | 82 |
| 112 /** | 83 /** |
| 113 * Fills a map with key/value pairs computed from [iterable]. | 84 * Fills a map with key/value pairs computed from [iterable]. |
| 114 * | 85 * |
| 115 * This method is used by Map classes in the named constructor fromIterable. | 86 * This method is used by Map classes in the named constructor fromIterable. |
| 116 */ | 87 */ |
| 117 static void _fillMapWithMappedIterable(Map map, Iterable iterable, | 88 static void _fillMapWithMappedIterable(Map map, Iterable iterable, |
| 118 key(element), value(element)) { | 89 key(element), value(element)) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 141 map[keyIterator.current] = valueIterator.current; | 112 map[keyIterator.current] = valueIterator.current; |
| 142 hasNextKey = keyIterator.moveNext(); | 113 hasNextKey = keyIterator.moveNext(); |
| 143 hasNextValue = valueIterator.moveNext(); | 114 hasNextValue = valueIterator.moveNext(); |
| 144 } | 115 } |
| 145 | 116 |
| 146 if (hasNextKey || hasNextValue) { | 117 if (hasNextKey || hasNextValue) { |
| 147 throw new ArgumentError("Iterables do not have same length."); | 118 throw new ArgumentError("Iterables do not have same length."); |
| 148 } | 119 } |
| 149 } | 120 } |
| 150 } | 121 } |
| OLD | NEW |