Chromium Code Reviews| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 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 |
|
Lasse Reichstein Nielsen
2013/07/09 06:15:44
Extra spaces added.
zarah
2013/07/09 08:20:54
Done.
| |
| 63 // A list to identify cyclic maps during toString() calls. | |
| 64 static List _toStringList = new List(); | |
| 65 | |
| 63 /** | 66 /** |
| 64 * Returns a string representing the specified map. The returned string | 67 * Returns a string representing the specified map. The returned string |
| 65 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':]. | 68 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':]. |
| 66 * The value returned by its [toString] method is used to represent each | 69 * The value returned by its [toString] method is used to represent each |
| 67 * key or value. | 70 * key or value. |
| 68 * | 71 * |
| 69 * If the map collection contains a reference to itself, either | 72 * If the map collection contains a reference to itself, either |
| 70 * directly as a key or value, or indirectly through other collections | 73 * directly as a key or value, or indirectly through other collections |
| 71 * or maps, the contained reference is rendered as [:'{...}':]. This | 74 * or maps, the contained reference is rendered as [:'{...}':]. This |
| 72 * prevents the infinite regress that would otherwise occur. So, for example, | 75 * prevents the infinite regress that would otherwise occur. So, for example, |
| 73 * calling this method on a map whose sole entry maps the string key 'me' | 76 * calling this method on a map whose sole entry maps the string key 'me' |
| 74 * to a reference to the map would return [:'{me: {...}}':]. | 77 * to a reference to the map would return [:'{me: {...}}':]. |
| 75 * | 78 * |
| 76 * A typical implementation of a map's [toString] method will | 79 * A typical implementation of a map's [toString] method will |
| 77 * simply return the results of this method applied to the collection. | 80 * simply return the results of this method applied to the collection. |
| 78 */ | 81 */ |
| 79 static String mapToString(Map m) => ToString.mapToString(m); | 82 static String mapToString(Map 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; | |
|
Lasse Reichstein Nielsen
2013/07/09 06:15:44
Consider moving this assignment into the condition
zarah
2013/07/09 08:20:54
Not done, as discussed.
| |
| 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 } | |
| 80 | 109 |
| 81 static _id(x) => x; | 110 static _id(x) => x; |
| 82 | 111 |
| 83 /** | 112 /** |
| 84 * Fills a map with key/value pairs computed from [iterable]. | 113 * Fills a map with key/value pairs computed from [iterable]. |
| 85 * | 114 * |
| 86 * This method is used by Map classes in the named constructor fromIterable. | 115 * This method is used by Map classes in the named constructor fromIterable. |
| 87 */ | 116 */ |
| 88 static void _fillMapWithMappedIterable(Map map, Iterable iterable, | 117 static void _fillMapWithMappedIterable(Map map, Iterable iterable, |
| 89 key(element), value(element)) { | 118 key(element), value(element)) { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 112 map[keyIterator.current] = valueIterator.current; | 141 map[keyIterator.current] = valueIterator.current; |
| 113 hasNextKey = keyIterator.moveNext(); | 142 hasNextKey = keyIterator.moveNext(); |
| 114 hasNextValue = valueIterator.moveNext(); | 143 hasNextValue = valueIterator.moveNext(); |
| 115 } | 144 } |
| 116 | 145 |
| 117 if (hasNextKey || hasNextValue) { | 146 if (hasNextKey || hasNextValue) { |
| 118 throw new ArgumentError("Iterables do not have same length."); | 147 throw new ArgumentError("Iterables do not have same length."); |
| 119 } | 148 } |
| 120 } | 149 } |
| 121 } | 150 } |
| OLD | NEW |