OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.collection; | 5 part of dart.collection; |
6 | 6 |
7 class _HashMapTable<K, V> extends _HashTable<K> { | 7 class _HashMapTable<K, V> extends _HashTable<K> { |
8 static const int _INITIAL_CAPACITY = 8; | 8 static const int _INITIAL_CAPACITY = 8; |
9 static const int _VALUE_INDEX = 1; | 9 static const int _VALUE_INDEX = 1; |
10 | 10 |
11 _HashMapTable() : super(_INITIAL_CAPACITY); | 11 _HashMapTable() : super(_INITIAL_CAPACITY); |
12 | 12 |
13 int get _entrySize => 2; | 13 int get _entrySize => 2; |
14 | 14 |
15 V _value(int offset) => _table[offset + _VALUE_INDEX]; | 15 V _value(int offset) => _table[offset + _VALUE_INDEX]; |
16 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; } | 16 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; } |
17 | 17 |
18 _copyEntry(List fromTable, int fromOffset, int toOffset) { | 18 _copyEntry(List fromTable, int fromOffset, int toOffset) { |
19 _table[toOffset + _VALUE_INDEX] = fromTable[fromOffset + _VALUE_INDEX]; | 19 _table[toOffset + _VALUE_INDEX] = fromTable[fromOffset + _VALUE_INDEX]; |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 class HashMap<K, V> implements Map<K, V> { | 23 class HashMap<K, V> implements Map<K, V> { |
24 final _HashMapTable<K, V> _hashTable; | 24 final _HashMapTable<K, V> _hashTable = new _HashMapTable<K, V>(); |
25 | 25 |
26 HashMap() : _hashTable = new _HashMapTable<K, V>() { | 26 external factory HashMap(); |
Ivan Posva
2013/03/22 16:32:47
Why stop here? There are plenty of other opportuni
kasperl
2013/03/22 16:36:01
I need to have this _internal hashTable object eve
| |
27 _hashTable._container = this; | |
28 } | |
29 | 27 |
30 factory HashMap.from(Map<K, V> other) { | 28 factory HashMap.from(Map<K, V> other) { |
31 return new HashMap<K, V>()..addAll(other); | 29 return new HashMap<K, V>()..addAll(other); |
32 } | 30 } |
33 | 31 |
32 HashMap._internal() { | |
33 _hashTable._container = this; | |
34 } | |
35 | |
34 bool containsKey(K key) { | 36 bool containsKey(K key) { |
35 return _hashTable._get(key) >= 0; | 37 return _hashTable._get(key) >= 0; |
36 } | 38 } |
37 | 39 |
38 bool containsValue(V value) { | 40 bool containsValue(V value) { |
39 List table = _hashTable._table; | 41 List table = _hashTable._table; |
40 int entrySize = _hashTable._entrySize; | 42 int entrySize = _hashTable._entrySize; |
41 for (int offset = 0; offset < table.length; offset += entrySize) { | 43 for (int offset = 0; offset < table.length; offset += entrySize) { |
42 if (!_hashTable._isFree(table[offset]) && | 44 if (!_hashTable._isFree(table[offset]) && |
43 _hashTable._value(offset) == value) { | 45 _hashTable._value(offset) == value) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 Iterable<K> get keys => new _HashTableKeyIterable<K>(_hashTable); | 129 Iterable<K> get keys => new _HashTableKeyIterable<K>(_hashTable); |
128 Iterable<V> get values => | 130 Iterable<V> get values => |
129 new _HashTableValueIterable<V>(_hashTable, _HashMapTable._VALUE_INDEX); | 131 new _HashTableValueIterable<V>(_hashTable, _HashMapTable._VALUE_INDEX); |
130 | 132 |
131 int get length => _hashTable._elementCount; | 133 int get length => _hashTable._elementCount; |
132 | 134 |
133 bool get isEmpty => _hashTable._elementCount == 0; | 135 bool get isEmpty => _hashTable._elementCount == 0; |
134 | 136 |
135 String toString() => Maps.mapToString(this); | 137 String toString() => Maps.mapToString(this); |
136 } | 138 } |
OLD | NEW |