Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: corelib/src/implementation/maps.dart

Issue 10890030: Move core and coreimpl from corelib/ to lib/core and lib/coreimpl. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « corelib/src/implementation/list.dart ('k') | corelib/src/implementation/object.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /*
6 * Helper class which implements complex [Map] operations
7 * in term of basic ones ([Map.getKeys], [Map.operator []],
8 * [Map.operator []=] and [Map.remove].) Not all methods are
9 * necessary to implement each particular operation.
10 */
11 class Maps {
12 static bool containsValue(Map map, value) {
13 for (final v in map.getValues()) {
14 if (value == v) {
15 return true;
16 }
17 }
18 return false;
19 }
20
21 static bool containsKey(Map map, key) {
22 for (final k in map.getKeys()) {
23 if (key == k) {
24 return true;
25 }
26 }
27 return false;
28 }
29
30 static putIfAbsent(Map map, key, ifAbsent()) {
31 if (map.containsKey(key)) {
32 return map[key];
33 }
34 final v = ifAbsent();
35 map[key] = v;
36 return v;
37 }
38
39 static clear(Map map) {
40 for (final k in map.getKeys()) {
41 map.remove(k);
42 }
43 }
44
45 static forEach(Map map, void f(key, value)) {
46 for (final k in map.getKeys()) {
47 f(k, map[k]);
48 }
49 }
50
51 static Collection getValues(Map map) {
52 final result = [];
53 for (final k in map.getKeys()) {
54 result.add(map[k]);
55 }
56 return result;
57 }
58
59 static int length(Map map) => map.getKeys().length;
60
61 static bool isEmpty(Map map) => length(map) == 0;
62
63 /**
64 * Returns a string representing the specified map. The returned string
65 * looks like this: [:'{key0: value0, key1: value1, ... keyN: valueN}':].
66 * The value returned by its [toString] method is used to represent each
67 * key or value.
68 *
69 * If the map collection contains a reference to itself, either
70 * directly as a key or value, or indirectly through other collections
71 * or maps, the contained reference is rendered as [:'{...}':]. This
72 * 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'
74 * to a reference to the map would return [:'{me: {...}}':].
75 *
76 * A typical implementation of a map's [toString] method will
77 * simply return the results of this method applied to the collection.
78 */
79 static String mapToString(Map m) {
80 var result = new StringBuffer();
81 _emitMap(m, result, new List());
82 return result.toString();
83 }
84
85 /**
86 * Appends a string representing the specified map to the specified
87 * string buffer. The string is formatted as per [mapToString].
88 * The [:visiting:] list contains references to all of the enclosing
89 * collections and maps (which are currently in the process of being
90 * emitted into [:result:]). The [:visiting:] parameter allows this method
91 * to generate a [:'[...]':] or [:'{...}':] where required. In other words,
92 * it allows this method and [_emitCollection] to identify recursive maps
93 * and collections.
94 */
95 static void _emitMap(Map m, StringBuffer result, List visiting) {
96 visiting.add(m);
97 result.add('{');
98
99 bool first = true;
100 m.forEach((k, v) {
101 if (!first) {
102 result.add(', ');
103 }
104 first = false;
105 Collections._emitObject(k, result, visiting);
106 result.add(': ');
107 Collections._emitObject(v, result, visiting);
108 });
109
110 result.add('}');
111 visiting.removeLast();
112 }
113 }
OLDNEW
« no previous file with comments | « corelib/src/implementation/list.dart ('k') | corelib/src/implementation/object.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698