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

Side by Side Diff: pkg/serialization/lib/src/serialization_helpers.dart

Issue 14246008: Allow Object when doing lookups. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Forgot to upload before committing Created 7 years, 5 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 | « pkg/mdv_observe/lib/src/observable_map.dart ('k') | runtime/lib/array.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * This contains extra functions and classes useful for implementing 6 * This contains extra functions and classes useful for implementing
7 * serialiation. Some or all of these will be removed once the functionality is 7 * serialiation. Some or all of these will be removed once the functionality is
8 * available in the core library. 8 * available in the core library.
9 */ 9 */
10 library serialization_helpers; 10 library serialization_helpers;
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 * This provides an identity map which also allows true, false, and null 183 * This provides an identity map which also allows true, false, and null
184 * as valid keys. In the interests of avoiding duplicating map code, and 184 * as valid keys. In the interests of avoiding duplicating map code, and
185 * because hashCode for arbitrary objects is currently very slow on the VM, 185 * because hashCode for arbitrary objects is currently very slow on the VM,
186 * just do a linear lookup. 186 * just do a linear lookup.
187 */ 187 */
188 class IdentityMap<K, V> implements Map<K, V> { 188 class IdentityMap<K, V> implements Map<K, V> {
189 189
190 final List<K> keys = <K>[]; 190 final List<K> keys = <K>[];
191 final List<V> values = <V>[]; 191 final List<V> values = <V>[];
192 192
193 V operator [](K key) { 193 V operator [](Object key) {
194 var index = _indexOf(key); 194 var index = _indexOf(key);
195 return (index == -1) ? null : values[index]; 195 return (index == -1) ? null : values[index];
196 } 196 }
197 197
198 void operator []=(K key, V value) { 198 void operator []=(K key, V value) {
199 var index = _indexOf(key); 199 var index = _indexOf(key);
200 if (index == -1) { 200 if (index == -1) {
201 keys.add(key); 201 keys.add(key);
202 values.add(value); 202 values.add(value);
203 } else { 203 } else {
204 values[index] = value; 204 values[index] = value;
205 } 205 }
206 } 206 }
207 207
208 V putIfAbsent(K key, Function ifAbsent) { 208 V putIfAbsent(K key, Function ifAbsent) {
209 var index = _indexOf(key); 209 var index = _indexOf(key);
210 if (index == -1) { 210 if (index == -1) {
211 keys.add(key); 211 keys.add(key);
212 values.add(ifAbsent()); 212 values.add(ifAbsent());
213 return values.last; 213 return values.last;
214 } else { 214 } else {
215 return values[index]; 215 return values[index];
216 } 216 }
217 } 217 }
218 218
219 int _indexOf(K key) { 219 int _indexOf(Object key) {
220 // Go backwards on the guess that we are most likely to access the most 220 // Go backwards on the guess that we are most likely to access the most
221 // recently added. 221 // recently added.
222 // Make strings and primitives unique 222 // Make strings and primitives unique
223 var compareEquality = isPrimitive(key); 223 var compareEquality = isPrimitive(key);
224 for (var i = keys.length - 1; i >= 0; i--) { 224 for (var i = keys.length - 1; i >= 0; i--) {
225 var equal = compareEquality ? key == keys[i] : identical(key, keys[i]); 225 var equal = compareEquality ? key == keys[i] : identical(key, keys[i]);
226 if (equal) return i; 226 if (equal) return i;
227 } 227 }
228 return -1; 228 return -1;
229 } 229 }
230 230
231 bool containsKey(K key) => _indexOf(key) != -1; 231 bool containsKey(Object key) => _indexOf(key) != -1;
232 void forEach(f(K key, V value)) { 232 void forEach(f(K key, V value)) {
233 for (var i = 0; i < keys.length; i++) { 233 for (var i = 0; i < keys.length; i++) {
234 f(keys[i], values[i]); 234 f(keys[i], values[i]);
235 } 235 }
236 } 236 }
237 237
238 V remove(K key) { 238 V remove(Object key) {
239 var index = _indexOf(key); 239 var index = _indexOf(key);
240 if (index == -1) return null; 240 if (index == -1) return null;
241 keys.removeAt(index); 241 keys.removeAt(index);
242 return values.removeAt(index); 242 return values.removeAt(index);
243 } 243 }
244 244
245 int get length => keys.length; 245 int get length => keys.length;
246 void clear() { 246 void clear() {
247 keys.clear(); 247 keys.clear();
248 values.clear(); 248 values.clear();
249 } 249 }
250 bool get isEmpty => keys.isEmpty; 250 bool get isEmpty => keys.isEmpty;
251 bool get isNotEmpty => !isEmpty; 251 bool get isNotEmpty => !isEmpty;
252 252
253 // Note that this is doing an equality comparison. 253 // Note that this is doing an equality comparison.
254 bool containsValue(x) => values.contains(x); 254 bool containsValue(Object x) => values.contains(x);
255 255
256 void addAll(Map<K, V> other) { 256 void addAll(Map<K, V> other) {
257 other.forEach((K key, V value) { 257 other.forEach((K key, V value) {
258 this[key] = value; 258 this[key] = value;
259 }); 259 });
260 } 260 }
261 } 261 }
OLDNEW
« no previous file with comments | « pkg/mdv_observe/lib/src/observable_map.dart ('k') | runtime/lib/array.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698