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

Unified Diff: runtime/lib/collection_patch.dart

Issue 13599005: Add JS implementation of LinkedHashMap. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix checked mode. Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/collection_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/collection_patch.dart
diff --git a/runtime/lib/collection_patch.dart b/runtime/lib/collection_patch.dart
index 2d4881ed7303163134cfdeb8932e9996538b2a85..e7856c5b073908d04abb9d8eb02863e7cc394cef 100644
--- a/runtime/lib/collection_patch.dart
+++ b/runtime/lib/collection_patch.dart
@@ -189,3 +189,132 @@ patch class HashSet<E> {
_table._clear();
}
}
+
+class _LinkedHashMapTable<K, V> extends _LinkedHashTable<K> {
+ static const int _INITIAL_CAPACITY = 8;
+ static const int _VALUE_INDEX = 3;
+
+ int get _entrySize => 4;
+
+ _LinkedHashMapTable() : super(_INITIAL_CAPACITY);
+
+ 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];
+ }
+}
+
+/**
+ * A hash-based map that iterates keys and values in key insertion order.
+ */
+patch class LinkedHashMap<K, V> {
+ final _LinkedHashMapTable _hashTable;
+
+ /* patch */ LinkedHashMap() : _hashTable = new _LinkedHashMapTable<K, V>() {
+ _hashTable._container = this;
+ }
+
+ /* patch */ bool containsKey(K key) {
+ return _hashTable._get(key) >= 0;
+ }
+
+ /* patch */ bool containsValue(V value) {
+ int modificationCount = _hashTable._modificationCount;
+ for (int offset = _hashTable._next(_LinkedHashTable._HEAD_OFFSET);
+ offset != _LinkedHashTable._HEAD_OFFSET;
+ offset = _hashTable._next(offset)) {
+ if (_hashTable._value(offset) == value) {
+ return true;
+ }
+ // The == call may modify the table.
+ _hashTable._checkModification(modificationCount);
+ }
+ return false;
+ }
+
+ /* patch */ void addAll(Map<K, V> other) {
+ other.forEach((K key, V value) {
+ int offset = _hashTable._put(key);
+ _hashTable._setValue(offset, value);
+ _hashTable._checkCapacity();
+ });
+ }
+
+ /* patch */ V operator [](K key) {
+ int offset = _hashTable._get(key);
+ if (offset >= 0) return _hashTable._value(offset);
+ return null;
+ }
+
+ /* patch */ void operator []=(K key, V value) {
+ int offset = _hashTable._put(key);
+ _hashTable._setValue(offset, value);
+ _hashTable._checkCapacity();
+ }
+
+ /* patch */ V putIfAbsent(K key, V ifAbsent()) {
+ int offset = _hashTable._probeForAdd(_hashTable._hashCodeOf(key), key);
+ Object entry = _hashTable._table[offset];
+ if (!_hashTable._isFree(entry)) {
+ return _hashTable._value(offset);
+ }
+ int modificationCount = _hashTable._modificationCount;
+ V value = ifAbsent();
+ if (modificationCount == _hashTable._modificationCount) {
+ _hashTable._setKey(offset, key);
+ _hashTable._setValue(offset, value);
+ _hashTable._linkLast(offset);
+ if (entry == null) {
+ _hashTable._entryCount++;
+ _hashTable._checkCapacity();
+ } else {
+ assert(identical(entry, _TOMBSTONE));
+ _hashTable._deletedCount--;
+ }
+ _hashTable._recordModification();
+ } else {
+ // The table might have changed, so we can't trust [offset] any more.
+ // Do another lookup before setting the value.
+ offset = _hashTable._put(key);
+ _hashTable._setValue(offset, value);
+ _hashTable._checkCapacity();
+ }
+ return value;
+ }
+
+ /* patch */ V remove(K key) {
+ int offset = _hashTable._remove(key);
+ if (offset < 0) return null;
+ Object oldValue = _hashTable._value(offset);
+ _hashTable._setValue(offset, null);
+ _hashTable._checkCapacity();
+ return oldValue;
+ }
+
+ /* patch */ void clear() {
+ _hashTable._clear();
+ }
+
+ /* patch */ void forEach(void action (K key, V value)) {
+ int modificationCount = _hashTable._modificationCount;
+ for (int offset = _hashTable._next(_LinkedHashTable._HEAD_OFFSET);
+ offset != _LinkedHashTable._HEAD_OFFSET;
+ offset = _hashTable._next(offset)) {
+ action(_hashTable._key(offset), _hashTable._value(offset));
+ _hashTable._checkModification(modificationCount);
+ }
+ }
+
+ /* patch */ Iterable<K> get keys =>
+ new _LinkedHashTableKeyIterable<K>(_hashTable);
+
+ /* patch */ Iterable<V> get values =>
+ new _LinkedHashTableValueIterable<V>(_hashTable,
+ _LinkedHashMapTable._VALUE_INDEX);
+
+ /* patch */ int get length => _hashTable._elementCount;
+
+ /* patch */ bool get isEmpty => _hashTable._elementCount == 0;
+}
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/collection_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698