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..628788b9772381df1760a9936cd3bcf30af9e22c |
--- /dev/null |
+++ b/sdk/lib/collection/linked_hash_map.dart |
@@ -0,0 +1,114 @@ |
+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; |
+ // ALias for easy access. |
+ static const int _HEAD_OFFSET = _LinkedHashTable._HEAD_OFFSET; |
+ |
+ 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) { |
+ _table[toOffset + _VALUE_INDEX] = oldTable[fromOffset + _VALUE_INDEX]; |
+ } |
+ |
+ bool containsKey(K key) { |
+ return _get(key) >= 0; |
+ } |
+ |
+ bool containsValue(V value) { |
+ int modificationCount = _modificationCount; |
+ for (int offset = _next(_HEAD_OFFSET); |
+ offset != _HEAD_OFFSET; |
+ offset = _next(offset)) { |
+ if (_value(offset) == value) { |
+ return true; |
+ } |
+ // The == call may modify the table. |
+ _checkModification(modificationCount); |
+ } |
+ return false; |
+ } |
+ |
+ void addAll(Map<K, V> other) { |
+ int length = other.length; |
+ _ensureCapacity(length); // Is this really wise if there is an overlap? |
+ other.forEach((K key, V value) { |
+ int offset = _put(key); |
+ _setValue(offset, value); |
+ }); |
+ } |
+ |
+ 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 entry = _table[offset]; |
+ if (entry == null) { |
+ _entryCount++; |
+ } else if (identical(entry, _TOMBSTONE)) { |
+ _deletedCount--; |
+ } else { |
+ return _value(offset); |
+ } |
+ V value = ifAbsent(); |
floitsch
2013/02/08 16:48:35
You already changed the _entryCount and _deletedCo
Lasse Reichstein Nielsen
2013/02/11 14:07:37
Done.
|
+ _setKey(offset, key); |
+ _setValue(offset, value); |
+ _linkLast(offset); |
+ _recordModification(); |
+ return value; |
+ } |
+ |
+ V remove(K key) { |
+ int offset = _remove(key); |
+ if (offset < 0) return null; |
+ Object oldValue = _value(offset); |
+ _setValue(offset, null); |
+ return oldValue; |
+ } |
+ |
+ void clear() { |
+ _clear(); |
+ } |
+ |
+ void forEach(void action (K key, V value)) { |
+ int modificationCount = _modificationCount; |
+ for (int offset = _next(_HEAD_OFFSET); |
+ offset != _HEAD_OFFSET; |
+ offset = _next(offset)) { |
+ action(_key(offset), _value(offset)); |
+ _checkModification(modificationCount); |
+ } |
+ } |
+ |
+ 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); |
+} |
+ |