Index: sdk/lib/collection/hash_map.dart |
diff --git a/sdk/lib/collection/hash_map.dart b/sdk/lib/collection/hash_map.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0f610f5fde04e230c3e52c8eb8fab949be9f7c3f |
--- /dev/null |
+++ b/sdk/lib/collection/hash_map.dart |
@@ -0,0 +1,112 @@ |
+part of dart.collection; |
+ |
+class HashMap<K, V> extends _HashTable<K> implements Map<K, V> { |
+ static const int _INITIAL_CAPACITY = 8; |
+ static const int _VALUE_INDEX = 1; |
+ |
+ HashMap() : super(_INITIAL_CAPACITY); |
+ |
+ factory HashMap.from(Map<K, V> other) { |
+ return new HashMap<K, V>()..addAll(other); |
+ } |
+ |
+ int get _entrySize => 2; |
+ |
+ V _value(int offset) => _table[offset + _VALUE_INDEX]; |
+ void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; } |
+ |
+ _copyEntry(List fromTable, int fromOffset, int toOffset) { |
floitsch
2013/02/06 10:43:58
Add comment that this is just to be more efficient
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Base function has been reduced to not copying anyt
|
+ _setValue(toOffset, fromTable[fromOffset + _VALUE_INDEX]); |
floitsch
2013/02/06 10:43:58
Don't use '_setValue' it makes it confusing.
_tabl
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done.
|
+ } |
+ |
+ bool containsKey(K key) { |
+ return _get(key) >= 0; |
+ } |
+ |
+ bool containsValue(V value) { |
+ for (int offset = 0; offset < _table.length; offset += _entrySize) { |
+ if (!_isFree(_table[offset]) && _value(offset) == value) { |
+ return true; |
+ } |
+ } |
+ return false; |
+ } |
+ |
+ void addAll(Map<K, V> other) { |
+ _ensureCapacity(other.length); |
+ other.forEach((K key, V value) { |
+ int offset = _put(key); |
+ _setValue(offset, value); |
+ _modificationCount++; |
erikcorry
2013/02/05 16:25:05
You probably want to mask this with something so i
Lasse Reichstein Nielsen
2013/02/05 19:47:40
That could be wrong, if you keep an iterator alive
floitsch
2013/02/06 10:43:58
It's 30 bits (1 for tagging, 1 for the sign). That
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done.
|
+ }); |
+ } |
+ |
+ V operator [](K key) { |
+ int offset = _get(key); |
+ if (offset >= 0) return _value(offset); |
+ return null; |
+ } |
+ |
+ void operator []=(K key, V value) { |
+ _ensureCapacity(1); |
+ int offset = _put(key); |
+ _setValue(offset, value); |
erikcorry
2013/02/05 16:25:05
Seems inconsistent that you don't increment the mo
Lasse Reichstein Nielsen
2013/02/05 19:47:40
The modification count is only for modifications t
erikcorry
2013/02/06 10:57:56
You already called _ensureCapacity, which may have
floitsch
2013/02/06 11:45:46
I would not make it dependent on the actual outcom
Lasse Reichstein Nielsen
2013/02/08 13:53:01
I prefer to consider only operations that change t
erikcorry
2013/02/11 10:18:29
If that is the contract with the user, then you ne
Lasse Reichstein Nielsen
2013/02/11 14:07:37
Gah!
Ok. I think I can do something for non-linke
erikcorry
2013/02/11 14:42:05
So if you update the values for preexisting keys w
Lasse Reichstein Nielsen
2013/02/12 10:34:47
Yes, That's acceptable. You always get the element
|
+ } |
+ |
+ V putIfAbsent(K key, V ifAbsent()) { |
+ _ensureCapacity(1); |
+ int offset = _probeForAdd(_hashCodeOf(key), key); |
+ Object prevKey = _key(offset); |
+ if (_isFree(prevKey)) { |
+ V value = ifAbsent(); |
+ _setKey(offset, key); |
+ _setValue(offset, value); |
+ if (prevKey == null) { |
+ _entryCount++; |
+ } else { |
+ assert(identical(prevKey, _TOMBSTONE)); |
+ _deletedCount--; |
+ } |
+ _modificationCount++; |
+ return value; |
+ } |
+ return _value(offset); |
+ } |
+ |
+ V remove(K key) { |
+ int offset = _remove(key); |
+ if (offset < 0) return null; |
+ V oldValue = _value(offset); |
+ _setValue(offset, null); |
+ return oldValue; |
+ } |
+ |
+ void clear() { |
+ _clear(); |
+ } |
+ |
+ void forEach(void action (K key, V value)) { |
+ int modCount = _modificationCount; |
+ for (int offset = 0; offset < _table.length; offset += 2) { |
erikcorry
2013/02/05 16:25:05
+= _entrySize?
Lasse Reichstein Nielsen
2013/02/05 19:47:40
Yes, it's probably more readable, even if we know
|
+ if (modCount != _modificationCount) { |
+ throw new ConcurrentModificationError(this); |
+ } |
+ Object entry = _key(offset); |
+ if (!_isFree(entry)) { |
+ K key = entry; |
+ V value = _value(offset); |
+ action(key, value); |
+ } |
+ } |
+ } |
+ |
+ Iterable<K> get keys => new _HashTableKeyIterable<K>(this); |
+ Iterable<V> get values => |
+ new _HashTableValueIterable<V>(this, _VALUE_INDEX); |
+ |
+ int get length => _elementCount; |
+ |
+ bool get isEmpty => _elementCount == 0; |
+ |
+ String toString() => Maps.mapToString(this); |
+} |