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

Unified 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 side-by-side diff with in-line comments
Download patch
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..fafe1c301bd6e29437592b9b00fafe0197404650
--- /dev/null
+++ b/sdk/lib/collection/hash_map.dart
@@ -0,0 +1,108 @@
+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) {
+ _table[toOffset + _VALUE_INDEX] = fromTable[fromOffset + _VALUE_INDEX];
+ }
+
+ 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);
+ });
+ }
+
+ 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 (_isFree(entry)) {
+ V value = ifAbsent();
+ _setKey(offset, key);
+ _setValue(offset, value);
+ if (entry == null) {
+ _entryCount++;
+ } else {
+ assert(identical(entry, _TOMBSTONE));
+ _deletedCount--;
+ }
+ 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 modificationCount = _modificationCount;
+ for (int offset = 0; offset < _table.length; offset += _entrySize) {
+ Object entry = _table[offset];
+ if (!_isFree(entry)) {
+ K key = entry;
+ V value = _value(offset);
+ action(key, value);
+ _checkModification(modificationCount);
+ }
+ }
+ }
+
+ 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);
+}

Powered by Google App Engine
This is Rietveld 408576698