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

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: Fixed copyright statments. 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
« no previous file with comments | « sdk/lib/collection/collection_sources.gypi ('k') | sdk/lib/collection/hash_set.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..6d2886e4e80f3016bc48085ce35a0cfbe322d1f3
--- /dev/null
+++ b/sdk/lib/collection/hash_map.dart
@@ -0,0 +1,116 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+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) {
+ other.forEach((K key, V value) {
+ int offset = _put(key);
+ _setValue(offset, value);
+ _checkCapacity();
+ });
+ }
+
+ V operator [](K key) {
+ int offset = _get(key);
+ if (offset >= 0) return _value(offset);
+ return null;
+ }
+
+ void operator []=(K key, V value) {
+ int offset = _put(key);
+ _setValue(offset, value);
+ _checkCapacity();
+ }
+
+ V putIfAbsent(K key, V ifAbsent()) {
+ int offset = _probeForAdd(_hashCodeOf(key), key);
+ Object entry = _table[offset];
+ if (!_isFree(entry)) {
+ return _value(offset);
+ }
+ int modificationCount = _modificationCount;
+ V value = ifAbsent();
+ _checkModification(modificationCount);
+ _setKey(offset, key);
+ _setValue(offset, value);
+ if (entry == null) {
+ _entryCount++;
+ _checkCapacity();
+ } else {
+ assert(identical(entry, _TOMBSTONE));
+ _deletedCount--;
+ }
+ _recordModification();
+ return value;
+ }
+
+ V remove(K key) {
+ int offset = _remove(key);
+ if (offset < 0) return null;
+ V oldValue = _value(offset);
+ _setValue(offset, null);
+ _checkCapacity();
+ 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);
+}
« no previous file with comments | « sdk/lib/collection/collection_sources.gypi ('k') | sdk/lib/collection/hash_set.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698