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

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: Address review comments, fix bugs. 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 _ensureCapacity(other.length);
37 other.forEach((K key, V value) {
38 int offset = _put(key);
39 _setValue(offset, value);
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 _ensureCapacity(1);
51 int offset = _put(key);
52 _setValue(offset, value);
53 }
54
55 V putIfAbsent(K key, V ifAbsent()) {
56 _ensureCapacity(1);
57 int offset = _probeForAdd(_hashCodeOf(key), key);
58 Object entry = _table[offset];
59 if (_isFree(entry)) {
60 V value = ifAbsent();
61 _setKey(offset, key);
62 _setValue(offset, value);
63 if (entry == null) {
64 _entryCount++;
65 } else {
66 assert(identical(entry, _TOMBSTONE));
67 _deletedCount--;
68 }
69 return value;
70 }
71 return _value(offset);
72 }
73
74 V remove(K key) {
75 int offset = _remove(key);
76 if (offset < 0) return null;
77 V oldValue = _value(offset);
78 _setValue(offset, null);
79 return oldValue;
80 }
81
82 void clear() {
83 _clear();
84 }
85
86 void forEach(void action (K key, V value)) {
87 int modificationCount = _modificationCount;
88 for (int offset = 0; offset < _table.length; offset += _entrySize) {
89 Object entry = _table[offset];
90 if (!_isFree(entry)) {
91 K key = entry;
92 V value = _value(offset);
93 action(key, value);
94 _checkModification(modificationCount);
95 }
96 }
97 }
98
99 Iterable<K> get keys => new _HashTableKeyIterable<K>(this);
100 Iterable<V> get values =>
101 new _HashTableValueIterable<V>(this, _VALUE_INDEX);
102
103 int get length => _elementCount;
104
105 bool get isEmpty => _elementCount == 0;
106
107 String toString() => Maps.mapToString(this);
108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698