OLD | NEW |
| (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 /** Unique marker object for the head of a linked list of entries. */ | |
8 class _LinkedHashTableHeadMarker { | |
9 const _LinkedHashTableHeadMarker(); | |
10 } | |
11 | |
12 const _LinkedHashTableHeadMarker _HEAD_MARKER = | |
13 const _LinkedHashTableHeadMarker(); | |
14 | |
15 class _LinkedHashTable<K> extends _HashTable<K> { | |
16 static const _NEXT_INDEX = 1; | |
17 static const _PREV_INDEX = 2; | |
18 static const _HEAD_OFFSET = 0; | |
19 | |
20 _LinkedHashTable(int initialCapacity) : super(initialCapacity); | |
21 | |
22 int get _entrySize => 3; | |
23 | |
24 List _createTable(int capacity) { | |
25 List result = new List.fixedLength(capacity * _entrySize); | |
26 result[_HEAD_OFFSET] = _HEAD_MARKER; | |
27 result[_HEAD_OFFSET + _NEXT_INDEX] = _HEAD_OFFSET; | |
28 result[_HEAD_OFFSET + _PREV_INDEX] = _HEAD_OFFSET; | |
29 return result; | |
30 } | |
31 | |
32 int _next(int offset) => _table[offset + _NEXT_INDEX]; | |
33 void _setNext(int offset, int to) { _table[offset + _NEXT_INDEX] = to; } | |
34 | |
35 int _prev(int offset) => _table[offset + _PREV_INDEX]; | |
36 void _setPrev(int offset, int to) { _table[offset + _PREV_INDEX] = to; } | |
37 | |
38 void _linkLast(int offset) { | |
39 // Add entry at offset at end of double-linked list. | |
40 int last = _prev(_HEAD_OFFSET); | |
41 _setNext(offset, _HEAD_OFFSET); | |
42 _setPrev(offset, last); | |
43 _setNext(last, offset); | |
44 _setPrev(_HEAD_OFFSET, offset); | |
45 } | |
46 | |
47 void _unlink(int offset) { | |
48 assert(offset != _HEAD_OFFSET); | |
49 int next = _next(offset); | |
50 int prev = _prev(offset); | |
51 _setNext(offset, null); | |
52 _setPrev(offset, null); | |
53 _setNext(prev, next); | |
54 _setPrev(next, prev); | |
55 } | |
56 | |
57 /** | |
58 * Copies all non-free entries from the old table to the new empty table. | |
59 */ | |
60 void _addAllEntries(List oldTable) { | |
61 int offset = oldTable[_HEAD_OFFSET + _NEXT_INDEX]; | |
62 while (offset != _HEAD_OFFSET) { | |
63 Object object = oldTable[offset]; | |
64 int nextOffset = oldTable[offset + _NEXT_INDEX]; | |
65 int toOffset = _put(object); | |
66 _copyEntry(oldTable, offset, toOffset); | |
67 offset = nextOffset; | |
68 } | |
69 } | |
70 | |
71 void _clear() { | |
72 if (_elementCount == 0) return; | |
73 _setNext(_HEAD_OFFSET, _HEAD_OFFSET); | |
74 _setPrev(_HEAD_OFFSET, _HEAD_OFFSET); | |
75 for (int i = _entrySize; i < _table.length; i++) { | |
76 _table[i] = null; | |
77 } | |
78 _entryCount = _deletedCount = 0; | |
79 _recordModification(); | |
80 } | |
81 | |
82 int _put(K key) { | |
83 int offset = _probeForAdd(_hashCodeOf(key), key); | |
84 Object oldEntry = _table[offset]; | |
85 if (identical(oldEntry, _TOMBSTONE)) { | |
86 _deletedCount--; | |
87 } else if (oldEntry == null) { | |
88 _entryCount++; | |
89 } else { | |
90 return offset; | |
91 } | |
92 _recordModification(); | |
93 _setKey(offset, key); | |
94 _linkLast(offset); | |
95 return offset; | |
96 } | |
97 | |
98 void _deleteEntry(int offset) { | |
99 _unlink(offset); | |
100 _setKey(offset, _TOMBSTONE); | |
101 _deletedCount++; | |
102 _recordModification(); | |
103 } | |
104 } | |
105 | |
106 class _LinkedHashTableKeyIterable<K> extends Iterable<K> { | |
107 final _LinkedHashTable<K> _table; | |
108 _LinkedHashTableKeyIterable(this._table); | |
109 Iterator<K> get iterator => new _LinkedHashTableKeyIterator<K>(_table); | |
110 | |
111 bool contains(Object value) => _table._get(value) >= 0; | |
112 | |
113 int get length => _table._elementCount; | |
114 } | |
115 | |
116 class _LinkedHashTableKeyIterator<K> extends _LinkedHashTableIterator<K> { | |
117 _LinkedHashTableKeyIterator(_LinkedHashTable<K> hashTable): super(hashTable); | |
118 | |
119 K _getCurrent(int offset) => _hashTable._key(offset); | |
120 } | |
121 | |
122 class _LinkedHashTableValueIterable<V> extends Iterable<V> { | |
123 final _LinkedHashTable _hashTable; | |
124 final int _valueIndex; | |
125 _LinkedHashTableValueIterable(this._hashTable, this._valueIndex); | |
126 Iterator<V> get iterator => | |
127 new _LinkedHashTableValueIterator<V>(_hashTable, _valueIndex); | |
128 int get length => _hashTable._elementCount; | |
129 } | |
130 | |
131 class _LinkedHashTableValueIterator<V> extends _LinkedHashTableIterator<V> { | |
132 final int _valueIndex; | |
133 | |
134 _LinkedHashTableValueIterator(_LinkedHashTable hashTable, this._valueIndex) | |
135 : super(hashTable); | |
136 | |
137 V _getCurrent(int offset) => _hashTable._table[offset + _valueIndex]; | |
138 } | |
139 | |
140 abstract class _LinkedHashTableIterator<T> implements Iterator<T> { | |
141 final _LinkedHashTable _hashTable; | |
142 final int _modificationCount; | |
143 int _offset; | |
144 T _current; | |
145 | |
146 _LinkedHashTableIterator(_LinkedHashTable table) | |
147 : _hashTable = table, | |
148 _modificationCount = table._modificationCount, | |
149 _offset = table._next(_LinkedHashTable._HEAD_OFFSET); | |
150 | |
151 bool moveNext() { | |
152 _hashTable._checkModification(_modificationCount); | |
153 if (_offset == _LinkedHashTable._HEAD_OFFSET) { | |
154 _current = null; | |
155 return false; | |
156 } | |
157 _current = _getCurrent(_offset); | |
158 _offset = _hashTable._next(_offset); | |
159 return true; | |
160 } | |
161 | |
162 T _getCurrent(int offset); | |
163 | |
164 T get current => _current; | |
165 } | |
OLD | NEW |