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

Side by Side Diff: sdk/lib/collection/hash_map.dart

Issue 12213010: New implementation of {,Linked}Hash{Set,Map}. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Made null elements work again, added tests for null. Created 7 years, 10 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
OLDNEW
(Empty)
1 part of dart.collection;
2
3 class HashMap<K, V> extends _HashTable<K> implements Map<K, V> {
4 static const int _INITIAL_CAPACITY = 8;
5 static const int _VALUE_INDEX = 1;
6
7 HashMap() : super(_INITIAL_CAPACITY);
8
9 factory HashMap.from(Map<K, V> other) {
10 return new HashMap<K, V>()..addAll(other);
11 }
12
13 int get _entrySize => 2;
14
15 V _value(int offset) => _table[offset + _VALUE_INDEX];
16 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; }
17
18 _copyEntry(List fromTable, int fromOffset, int toOffset) {
19 _table[toOffset + _VALUE_INDEX] = fromTable[fromOffset + _VALUE_INDEX];
20 }
21
22 bool containsKey(K key) {
23 return _get(key) >= 0;
24 }
25
26 bool containsValue(V value) {
27 for (int offset = 0; offset < _table.length; offset += _entrySize) {
28 if (!_isFree(_table[offset]) && _value(offset) == value) {
29 return true;
30 }
31 }
32 return false;
33 }
34
35 void addAll(Map<K, V> other) {
36 other.forEach((K key, V value) {
37 int offset = _put(key);
38 _setValue(offset, value);
39 _checkCapacity();
40 });
41 }
42
43 V operator [](K key) {
44 int offset = _get(key);
45 if (offset >= 0) return _value(offset);
46 return null;
47 }
48
49 void operator []=(K key, V value) {
50 int offset = _put(key);
51 _setValue(offset, value);
52 _checkCapacity();
53 }
54
55 V putIfAbsent(K key, V ifAbsent()) {
56 int offset = _probeForAdd(_hashCodeOf(key), key);
57 Object entry = _table[offset];
58 if (!_isFree(entry)) {
59 return _value(offset);
60 }
61 int modificationCount = _modificationCount;
62 V value = ifAbsent();
63 _checkModification(modificationCount);
64 _setKey(offset, key);
65 _setValue(offset, value);
66 if (entry == null) {
67 _entryCount++;
68 _checkCapacity();
69 } else {
70 assert(identical(entry, _TOMBSTONE));
71 _deletedCount--;
72 }
73 _recordModification();
74 return value;
75 }
76
77 V remove(K key) {
78 int offset = _remove(key);
79 if (offset < 0) return null;
80 V oldValue = _value(offset);
81 _setValue(offset, null);
82 _checkCapacity();
83 return oldValue;
84 }
85
86 void clear() {
87 _clear();
88 }
89
90 void forEach(void action (K key, V value)) {
91 int modificationCount = _modificationCount;
92 for (int offset = 0; offset < _table.length; offset += _entrySize) {
93 Object entry = _table[offset];
94 if (!_isFree(entry)) {
95 K key = entry;
96 V value = _value(offset);
97 action(key, value);
98 _checkModification(modificationCount);
99 }
100 }
101 }
102
103 Iterable<K> get keys => new _HashTableKeyIterable<K>(this);
104 Iterable<V> get values =>
105 new _HashTableValueIterable<V>(this, _VALUE_INDEX);
106
107 int get length => _elementCount;
108
109 bool get isEmpty => _elementCount == 0;
110
111 String toString() => Maps.mapToString(this);
112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698