Index: sdk/lib/collection/linked_hash_map.dart |
diff --git a/sdk/lib/collection/linked_hash_map.dart b/sdk/lib/collection/linked_hash_map.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1070c227868a63b7954fac0c804522e1ae174454 |
--- /dev/null |
+++ b/sdk/lib/collection/linked_hash_map.dart |
@@ -0,0 +1,113 @@ |
+part of dart.collection; |
+ |
+class LinkedHashMap<K, V> extends _LinkedHashTable<K> implements HashMap<K, V> { |
+ static const int _INITIAL_CAPACITY = 8; |
+ static const int _VALUE_INDEX = 3; |
+ |
+ int get _entrySize => 4; |
+ |
+ LinkedHashMap() : super(_INITIAL_CAPACITY); |
+ |
+ factory LinkedHashMap.from(Map<K, V> other) { |
+ return new LinkedHashMap<K, V>()..addAll(other); |
+ } |
+ |
+ V _value(int offset) => _table[offset + _VALUE_INDEX]; |
+ void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; } |
+ |
+ _copyEntry(List oldTable, int fromOffset, int toOffset) { |
+ _setValue(toOffset, oldTable[fromOffset + _VALUE_INDEX]); |
floitsch
2013/02/06 10:43:58
don't use "_setValue".
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 = _next(_LinkedHashTable._HEAD_OFFSET); |
+ offset != _LinkedHashTable._HEAD_OFFSET; |
+ offset = _next(offset)) { |
+ if (_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++; |
+ }); |
+ } |
+ |
+ 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); |
+ } |
+ |
+ V putIfAbsent(K key, V ifAbsent()) { |
+ _ensureCapacity(1); |
+ int offset = _probeForAdd(_hashCodeOf(key), key); |
+ Object prevKey = _table[offset]; |
+ if (_isFree(prevKey)) { |
+ V value = ifAbsent(); |
+ _table[offset] = key; |
+ _setValue(offset, value); |
+ _linkLast(offset); |
+ 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; |
+ Object oldValue = _value(offset); |
floitsch
2013/02/06 10:43:58
modification counter.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done by _remove now.
|
+ _setValue(offset, null); |
+ return oldValue; |
+ } |
+ |
+ void clear() { |
+ _clear(); |
+ } |
+ |
+ void forEach(void action (K key, V value)) { |
+ int modCount = _modificationCount; |
+ for (int offset = _next(0); offset != 0; offset = _next(offset)) { |
+ if (modCount != _modificationCount) { |
+ throw new ConcurrentModificationError(this); |
+ } |
+ Object key = _table[offset]; |
+ assert(!_isFree(key)); |
+ action(key, _value(offset)); |
+ } |
+ } |
+ |
+ Iterable<K> get keys => new _LinkedHashTableKeyIterable<K>(this); |
+ Iterable<V> get values => |
+ new _LinkedHashTableValueIterable<V>(this, _VALUE_INDEX); |
+ |
+ int get length => _elementCount; |
+ |
+ bool get isEmpty => _elementCount == 0; |
+ |
+ String toString() => Maps.mapToString(this); |
+} |
+ |