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 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 static List _toStringList = new List(); | |
|
floitsch
2013/07/08 12:00:50
add comment.
zarah
2013/07/08 14:35:15
Done.
| |
| 64 | |
| 63 /** | 65 /** |
| 64 * Returns a string representing the specified map. The returned string | 66 * Returns a string representing the specified map. The returned string |
| 65 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':]. | 67 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':]. |
| 66 * The value returned by its [toString] method is used to represent each | 68 * The value returned by its [toString] method is used to represent each |
| 67 * key or value. | 69 * key or value. |
| 68 * | 70 * |
| 69 * If the map collection contains a reference to itself, either | 71 * If the map collection contains a reference to itself, either |
| 70 * directly as a key or value, or indirectly through other collections | 72 * directly as a key or value, or indirectly through other collections |
| 71 * or maps, the contained reference is rendered as [:'{...}':]. This | 73 * or maps, the contained reference is rendered as [:'{...}':]. This |
| 72 * prevents the infinite regress that would otherwise occur. So, for example, | 74 * 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' | 75 * calling this method on a map whose sole entry maps the string key 'me' |
| 74 * to a reference to the map would return [:'{me: {...}}':]. | 76 * to a reference to the map would return [:'{me: {...}}':]. |
| 75 * | 77 * |
| 76 * A typical implementation of a map's [toString] method will | 78 * A typical implementation of a map's [toString] method will |
| 77 * simply return the results of this method applied to the collection. | 79 * simply return the results of this method applied to the collection. |
| 78 */ | 80 */ |
| 79 static String mapToString(Map m) => ToString.mapToString(m); | 81 static String mapToString(Map m) { |
| 82 for(int i = 0; i < _toStringList.length; i++) { | |
|
floitsch
2013/07/08 12:00:50
Same comments as for the other toString methods. (
zarah
2013/07/08 14:35:15
Done.
| |
| 83 if(identical(_toStringList[i], m)) | |
| 84 return '{...}'; | |
| 85 } | |
| 86 | |
| 87 _toStringList.add(m); | |
| 88 var result = new StringBuffer(); | |
| 89 result.write('{'); | |
| 90 bool first = true; | |
| 91 m.forEach((k, v) { | |
| 92 if(!first) { | |
| 93 result.write(', '); | |
| 94 } | |
| 95 first = false; | |
| 96 result.write(k); | |
| 97 result.write(': '); | |
| 98 result.write(v); | |
| 99 }); | |
| 100 result.write('}'); | |
| 101 _toStringList.remove(m); | |
| 102 return result.toString(); | |
| 103 } | |
| 80 | 104 |
| 81 static _id(x) => x; | 105 static _id(x) => x; |
| 82 | 106 |
| 83 /** | 107 /** |
| 84 * Fills a map with key/value pairs computed from [iterable]. | 108 * Fills a map with key/value pairs computed from [iterable]. |
| 85 * | 109 * |
| 86 * This method is used by Map classes in the named constructor fromIterable. | 110 * This method is used by Map classes in the named constructor fromIterable. |
| 87 */ | 111 */ |
| 88 static void _fillMapWithMappedIterable(Map map, Iterable iterable, | 112 static void _fillMapWithMappedIterable(Map map, Iterable iterable, |
| 89 key(element), value(element)) { | 113 key(element), value(element)) { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 112 map[keyIterator.current] = valueIterator.current; | 136 map[keyIterator.current] = valueIterator.current; |
| 113 hasNextKey = keyIterator.moveNext(); | 137 hasNextKey = keyIterator.moveNext(); |
| 114 hasNextValue = valueIterator.moveNext(); | 138 hasNextValue = valueIterator.moveNext(); |
| 115 } | 139 } |
| 116 | 140 |
| 117 if (hasNextKey || hasNextValue) { | 141 if (hasNextKey || hasNextValue) { |
| 118 throw new ArgumentError("Iterables do not have same length."); | 142 throw new ArgumentError("Iterables do not have same length."); |
| 119 } | 143 } |
| 120 } | 144 } |
| 121 } | 145 } |
| OLD | NEW |