OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 * A [Map] is an associative container, mapping a key to a value. | 6 * A [Map] is an associative container, mapping a key to a value. |
7 * Null values are supported, but null keys are not. | 7 * Null values are supported, but null keys are not. |
8 */ | 8 */ |
9 abstract class Map<K, V> { | 9 abstract class Map<K, V> { |
10 /** | 10 /** |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 if ((key != null) && (!identical(key, _DELETED_KEY))) { | 355 if ((key != null) && (!identical(key, _DELETED_KEY))) { |
356 f(key, _values[i]); | 356 f(key, _values[i]); |
357 } | 357 } |
358 } | 358 } |
359 } | 359 } |
360 | 360 |
361 | 361 |
362 Collection<K> get keys { | 362 Collection<K> get keys { |
363 List<K> list = new List<K>(length); | 363 List<K> list = new List<K>(length); |
364 int i = 0; | 364 int i = 0; |
365 forEach(void _(K key, V value) { | 365 forEach((K key, V value) { |
366 list[i++] = key; | 366 list[i++] = key; |
367 }); | 367 }); |
368 return list; | 368 return list; |
369 } | 369 } |
370 | 370 |
371 Collection<V> get values { | 371 Collection<V> get values { |
372 List<V> list = new List<V>(length); | 372 List<V> list = new List<V>(length); |
373 int i = 0; | 373 int i = 0; |
374 forEach(void _(K key, V value) { | 374 forEach((K key, V value) { |
375 list[i++] = value; | 375 list[i++] = value; |
376 }); | 376 }); |
377 return list; | 377 return list; |
378 } | 378 } |
379 | 379 |
380 bool containsKey(K key) { | 380 bool containsKey(K key) { |
381 return (_probeForLookup(key) != -1); | 381 return (_probeForLookup(key) != -1); |
382 } | 382 } |
383 | 383 |
384 bool containsValue(V value) { | 384 bool containsValue(V value) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 if ((this[key] == null) && !(containsKey(key))) { | 467 if ((this[key] == null) && !(containsKey(key))) { |
468 value = ifAbsent(); | 468 value = ifAbsent(); |
469 this[key] = value; | 469 this[key] = value; |
470 } | 470 } |
471 return value; | 471 return value; |
472 } | 472 } |
473 | 473 |
474 Collection<K> get keys { | 474 Collection<K> get keys { |
475 List<K> list = new List<K>(length); | 475 List<K> list = new List<K>(length); |
476 int index = 0; | 476 int index = 0; |
477 _list.forEach(void _(_KeyValuePair<K, V> entry) { | 477 _list.forEach((_KeyValuePair<K, V> entry) { |
478 list[index++] = entry.key; | 478 list[index++] = entry.key; |
479 }); | 479 }); |
480 assert(index == length); | 480 assert(index == length); |
481 return list; | 481 return list; |
482 } | 482 } |
483 | 483 |
484 | 484 |
485 Collection<V> get values { | 485 Collection<V> get values { |
486 List<V> list = new List<V>(length); | 486 List<V> list = new List<V>(length); |
487 int index = 0; | 487 int index = 0; |
488 _list.forEach(void _(_KeyValuePair<K, V> entry) { | 488 _list.forEach((_KeyValuePair<K, V> entry) { |
489 list[index++] = entry.value; | 489 list[index++] = entry.value; |
490 }); | 490 }); |
491 assert(index == length); | 491 assert(index == length); |
492 return list; | 492 return list; |
493 } | 493 } |
494 | 494 |
495 void forEach(void f(K key, V value)) { | 495 void forEach(void f(K key, V value)) { |
496 _list.forEach(void _(_KeyValuePair<K, V> entry) { | 496 _list.forEach((_KeyValuePair<K, V> entry) { |
497 f(entry.key, entry.value); | 497 f(entry.key, entry.value); |
498 }); | 498 }); |
499 } | 499 } |
500 | 500 |
501 bool containsKey(K key) { | 501 bool containsKey(K key) { |
502 return _map.containsKey(key); | 502 return _map.containsKey(key); |
503 } | 503 } |
504 | 504 |
505 bool containsValue(V value) { | 505 bool containsValue(V value) { |
506 return _list.some(bool _(_KeyValuePair<K, V> entry) { | 506 return _list.some((_KeyValuePair<K, V> entry) { |
507 return (entry.value == value); | 507 return (entry.value == value); |
508 }); | 508 }); |
509 } | 509 } |
510 | 510 |
511 int get length { | 511 int get length { |
512 return _map.length; | 512 return _map.length; |
513 } | 513 } |
514 | 514 |
515 bool get isEmpty { | 515 bool get isEmpty { |
516 return length == 0; | 516 return length == 0; |
517 } | 517 } |
518 | 518 |
519 void clear() { | 519 void clear() { |
520 _map.clear(); | 520 _map.clear(); |
521 _list.clear(); | 521 _list.clear(); |
522 } | 522 } |
523 | 523 |
524 String toString() { | 524 String toString() { |
525 return Maps.mapToString(this); | 525 return Maps.mapToString(this); |
526 } | 526 } |
527 } | 527 } |
OLD | NEW |