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

Side by Side Diff: sdk/lib/collection/hash_map.dart

Issue 12254006: Reapply "New implementation of {,Linked}Hash{Set,Map}." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make HashMap and LinkedHashMap not extend HashTable. 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of dart.collection;
6
7 class _HashMapTable<K, V> extends _HashTable<K> {
8 static const int _INITIAL_CAPACITY = 8;
9 static const int _VALUE_INDEX = 1;
10
11 _HashMapTable() : super(_INITIAL_CAPACITY);
12
13 int get _entrySize => 2;
14
15 V _value(int offset) => _table[offset + _VALUE_INDEX];
16 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; }
17
18 _copyEntry(List fromTable, int fromOffset, int toOffset) {
19 _table[toOffset + _VALUE_INDEX] = fromTable[fromOffset + _VALUE_INDEX];
20 }
21 }
22
23 class HashMap<K, V> implements Map<K, V> {
24 final _HashMapTable<K, V> _hashTable;
25
26 HashMap() : _hashTable = new _HashMapTable<K, V>() {
27 _hashTable._container = this;
28 }
29
30 factory HashMap.from(Map<K, V> other) {
31 return new HashMap<K, V>()..addAll(other);
32 }
33
34 bool containsKey(K key) {
35 return _hashTable._get(key) >= 0;
36 }
37
38 bool containsValue(V value) {
39 List table = _hashTable._table;
40 int entrySize = _hashTable._entrySize;
41 for (int offset = 0; offset < table.length; offset += entrySize) {
42 if (!_hashTable._isFree(table[offset]) &&
43 _hashTable._value(offset) == value) {
44 return true;
45 }
46 }
47 return false;
48 }
49
50 void addAll(Map<K, V> other) {
51 other.forEach((K key, V value) {
52 int offset = _hashTable._put(key);
53 _hashTable._setValue(offset, value);
54 _hashTable._checkCapacity();
55 });
56 }
57
58 V operator [](K key) {
59 int offset = _hashTable._get(key);
60 if (offset >= 0) return _hashTable._value(offset);
61 return null;
62 }
63
64 void operator []=(K key, V value) {
65 int offset = _hashTable._put(key);
66 _hashTable._setValue(offset, value);
67 _hashTable._checkCapacity();
68 }
69
70 V putIfAbsent(K key, V ifAbsent()) {
71 int offset = _hashTable._probeForAdd(_hashTable._hashCodeOf(key), key);
72 Object entry = _hashTable._table[offset];
73 if (!_hashTable._isFree(entry)) {
74 return _hashTable._value(offset);
75 }
76 int modificationCount = _hashTable._modificationCount;
77 V value = ifAbsent();
78 _hashTable._checkModification(modificationCount);
79 _hashTable._setKey(offset, key);
80 _hashTable._setValue(offset, value);
81 if (entry == null) {
82 _hashTable._entryCount++;
83 _hashTable._checkCapacity();
84 } else {
85 assert(identical(entry, _TOMBSTONE));
86 _hashTable._deletedCount--;
87 }
88 _hashTable._recordModification();
89 return value;
90 }
91
92 V remove(K key) {
93 int offset = _hashTable._remove(key);
94 if (offset < 0) return null;
95 V oldValue = _hashTable._value(offset);
96 _hashTable._setValue(offset, null);
97 _hashTable._checkCapacity();
98 return oldValue;
99 }
100
101 void clear() {
102 _hashTable._clear();
103 }
104
105 void forEach(void action (K key, V value)) {
106 int modificationCount = _hashTable._modificationCount;
107 List table = _hashTable._table;
108 int entrySize = _hashTable._entrySize;
109 for (int offset = 0; offset < table.length; offset += entrySize) {
110 Object entry = table[offset];
111 if (!_hashTable._isFree(entry)) {
112 K key = entry;
113 V value = _hashTable._value(offset);
114 action(key, value);
115 _hashTable._checkModification(modificationCount);
116 }
117 }
118 }
119
120 Iterable<K> get keys => new _HashTableKeyIterable<K>(_hashTable);
121 Iterable<V> get values =>
122 new _HashTableValueIterable<V>(_hashTable, _HashMapTable._VALUE_INDEX);
123
124 int get length => _hashTable._elementCount;
125
126 bool get isEmpty => _hashTable._elementCount == 0;
127
128 String toString() => Maps.mapToString(this);
129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698