| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, 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 // Efficient JavaScript based implementation of a linked hash map used as a |
| 6 // backing map for constant maps and the [LinkedHashMap] patch |
| 7 |
| 8 part of _js_helper; |
| 9 |
| 10 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap { |
| 11 int _length = 0; |
| 12 |
| 13 // The hash map contents are divided into three parts: one part for |
| 14 // string keys, one for numeric keys, and one for the rest. String |
| 15 // and numeric keys map directly to their linked cells, but the rest |
| 16 // of the entries are stored in bucket lists of the form: |
| 17 // |
| 18 // [cell-0, cell-1, ...] |
| 19 // |
| 20 // where all keys in the same bucket share the same hash code. |
| 21 var _strings; |
| 22 var _nums; |
| 23 var _rest; |
| 24 |
| 25 // The keys and values are stored in cells that are linked together |
| 26 // to form a double linked list. |
| 27 LinkedHashMapCell _first; |
| 28 LinkedHashMapCell _last; |
| 29 |
| 30 // We track the number of modifications done to the key set of the |
| 31 // hash map to be able to throw when the map is modified while being |
| 32 // iterated over. |
| 33 int _modifications = 0; |
| 34 |
| 35 JsLinkedHashMap(); |
| 36 |
| 37 |
| 38 int get length => _length; |
| 39 bool get isEmpty => _length == 0; |
| 40 bool get isNotEmpty => !isEmpty; |
| 41 |
| 42 Iterable<K> get keys { |
| 43 return new LinkedHashMapKeyIterable<K>(this); |
| 44 } |
| 45 |
| 46 Iterable<V> get values { |
| 47 return new MappedIterable<K, V>(keys, (each) => this[each]); |
| 48 } |
| 49 |
| 50 bool containsKey(Object key) { |
| 51 if (_isStringKey(key)) { |
| 52 var strings = _strings; |
| 53 if (strings == null) return false; |
| 54 LinkedHashMapCell cell = _getTableEntry(strings, key); |
| 55 return cell != null; |
| 56 } else if (_isNumericKey(key)) { |
| 57 var nums = _nums; |
| 58 if (nums == null) return false; |
| 59 LinkedHashMapCell cell = _getTableEntry(nums, key); |
| 60 return cell != null; |
| 61 } else { |
| 62 return internalContainsKey(key); |
| 63 } |
| 64 } |
| 65 |
| 66 bool internalContainsKey(Object key) { |
| 67 var rest = _rest; |
| 68 if (rest == null) return false; |
| 69 var bucket = _getBucket(rest, key); |
| 70 return internalFindBucketIndex(bucket, key) >= 0; |
| 71 } |
| 72 |
| 73 bool containsValue(Object value) { |
| 74 return keys.any((each) => this[each] == value); |
| 75 } |
| 76 |
| 77 void addAll(Map<K, V> other) { |
| 78 other.forEach((K key, V value) { |
| 79 this[key] = value; |
| 80 }); |
| 81 } |
| 82 |
| 83 V operator[](Object key) { |
| 84 if (_isStringKey(key)) { |
| 85 var strings = _strings; |
| 86 if (strings == null) return null; |
| 87 LinkedHashMapCell cell = _getTableEntry(strings, key); |
| 88 return (cell == null) ? null : cell.value; |
| 89 } else if (_isNumericKey(key)) { |
| 90 var nums = _nums; |
| 91 if (nums == null) return null; |
| 92 LinkedHashMapCell cell = _getTableEntry(nums, key); |
| 93 return (cell == null) ? null : cell.value; |
| 94 } else { |
| 95 return internalGet(key); |
| 96 } |
| 97 } |
| 98 |
| 99 V internalGet(Object key) { |
| 100 var rest = _rest; |
| 101 if (rest == null) return null; |
| 102 var bucket = _getBucket(rest, key); |
| 103 int index = internalFindBucketIndex(bucket, key); |
| 104 if (index < 0) return null; |
| 105 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); |
| 106 return cell.value; |
| 107 } |
| 108 |
| 109 void operator[]=(K key, V value) { |
| 110 if (_isStringKey(key)) { |
| 111 var strings = _strings; |
| 112 if (strings == null) _strings = strings = _newHashTable(); |
| 113 _addHashTableEntry(strings, key, value); |
| 114 } else if (_isNumericKey(key)) { |
| 115 var nums = _nums; |
| 116 if (nums == null) _nums = nums = _newHashTable(); |
| 117 _addHashTableEntry(nums, key, value); |
| 118 } else { |
| 119 internalSet(key, value); |
| 120 } |
| 121 } |
| 122 |
| 123 void internalSet(K key, V value) { |
| 124 var rest = _rest; |
| 125 if (rest == null) _rest = rest = _newHashTable(); |
| 126 var hash = internalComputeHashCode(key); |
| 127 var bucket = JS('var', '#[#]', rest, hash); |
| 128 if (bucket == null) { |
| 129 LinkedHashMapCell cell = _newLinkedCell(key, value); |
| 130 _setTableEntry(rest, hash, JS('var', '[#]', cell)); |
| 131 } else { |
| 132 int index = internalFindBucketIndex(bucket, key); |
| 133 if (index >= 0) { |
| 134 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); |
| 135 cell.value = value; |
| 136 } else { |
| 137 LinkedHashMapCell cell = _newLinkedCell(key, value); |
| 138 JS('void', '#.push(#)', bucket, cell); |
| 139 } |
| 140 } |
| 141 } |
| 142 |
| 143 V putIfAbsent(K key, V ifAbsent()) { |
| 144 if (containsKey(key)) return this[key]; |
| 145 V value = ifAbsent(); |
| 146 this[key] = value; |
| 147 return value; |
| 148 } |
| 149 |
| 150 V remove(Object key) { |
| 151 if (_isStringKey(key)) { |
| 152 return _removeHashTableEntry(_strings, key); |
| 153 } else if (_isNumericKey(key)) { |
| 154 return _removeHashTableEntry(_nums, key); |
| 155 } else { |
| 156 return internalRemove(key); |
| 157 } |
| 158 } |
| 159 |
| 160 V internalRemove(Object key) { |
| 161 var rest = _rest; |
| 162 if (rest == null) return null; |
| 163 var bucket = _getBucket(rest, key); |
| 164 int index = internalFindBucketIndex(bucket, key); |
| 165 if (index < 0) return null; |
| 166 // Use splice to remove the [cell] element at the index and |
| 167 // unlink the cell before returning its value. |
| 168 LinkedHashMapCell cell = JS('var', '#.splice(#, 1)[0]', bucket, index); |
| 169 _unlinkCell(cell); |
| 170 // TODO(kasperl): Consider getting rid of the bucket list when |
| 171 // the length reaches zero. |
| 172 return cell.value; |
| 173 } |
| 174 |
| 175 void clear() { |
| 176 if (_length > 0) { |
| 177 _strings = _nums = _rest = _first = _last = null; |
| 178 _length = 0; |
| 179 _modified(); |
| 180 } |
| 181 } |
| 182 |
| 183 void forEach(void action(K key, V value)) { |
| 184 LinkedHashMapCell cell = _first; |
| 185 int modifications = _modifications; |
| 186 while (cell != null) { |
| 187 action(cell.key, cell.value); |
| 188 if (modifications != _modifications) { |
| 189 throw new ConcurrentModificationError(this); |
| 190 } |
| 191 cell = cell._next; |
| 192 } |
| 193 } |
| 194 |
| 195 void _addHashTableEntry(var table, K key, V value) { |
| 196 LinkedHashMapCell cell = _getTableEntry(table, key); |
| 197 if (cell == null) { |
| 198 _setTableEntry(table, key, _newLinkedCell(key, value)); |
| 199 } else { |
| 200 cell.value = value; |
| 201 } |
| 202 } |
| 203 |
| 204 V _removeHashTableEntry(var table, Object key) { |
| 205 if (table == null) return null; |
| 206 LinkedHashMapCell cell = _getTableEntry(table, key); |
| 207 if (cell == null) return null; |
| 208 _unlinkCell(cell); |
| 209 _deleteTableEntry(table, key); |
| 210 return cell.value; |
| 211 } |
| 212 |
| 213 void _modified() { |
| 214 // Value cycles after 2^30 modifications. If you keep hold of an |
| 215 // iterator for that long, you might miss a modification |
| 216 // detection, and iteration can go sour. Don't do that. |
| 217 _modifications = (_modifications + 1) & 0x3ffffff; |
| 218 } |
| 219 |
| 220 // Create a new cell and link it in as the last one in the list. |
| 221 LinkedHashMapCell _newLinkedCell(K key, V value) { |
| 222 LinkedHashMapCell cell = new LinkedHashMapCell(key, value); |
| 223 if (_first == null) { |
| 224 _first = _last = cell; |
| 225 } else { |
| 226 LinkedHashMapCell last = _last; |
| 227 cell._previous = last; |
| 228 _last = last._next = cell; |
| 229 } |
| 230 _length++; |
| 231 _modified(); |
| 232 return cell; |
| 233 } |
| 234 |
| 235 // Unlink the given cell from the linked list of cells. |
| 236 void _unlinkCell(LinkedHashMapCell cell) { |
| 237 LinkedHashMapCell previous = cell._previous; |
| 238 LinkedHashMapCell next = cell._next; |
| 239 if (previous == null) { |
| 240 assert(cell == _first); |
| 241 _first = next; |
| 242 } else { |
| 243 previous._next = next; |
| 244 } |
| 245 if (next == null) { |
| 246 assert(cell == _last); |
| 247 _last = previous; |
| 248 } else { |
| 249 next._previous = previous; |
| 250 } |
| 251 _length--; |
| 252 _modified(); |
| 253 } |
| 254 |
| 255 static bool _isStringKey(var key) { |
| 256 return key is String && key != '__proto__'; |
| 257 } |
| 258 |
| 259 static bool _isNumericKey(var key) { |
| 260 // Only treat unsigned 30-bit integers as numeric keys. This way, |
| 261 // we avoid converting them to strings when we use them as keys in |
| 262 // the JavaScript hash table object. |
| 263 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key); |
| 264 } |
| 265 |
| 266 int internalComputeHashCode(var key) { |
| 267 // We force the hash codes to be unsigned 30-bit integers to avoid |
| 268 // issues with problematic keys like '__proto__'. Another option |
| 269 // would be to throw an exception if the hash code isn't a number. |
| 270 return JS('int', '# & 0x3ffffff', key.hashCode); |
| 271 } |
| 272 |
| 273 static _getTableEntry(var table, var key) { |
| 274 return JS('var', '#[#]', table, key); |
| 275 } |
| 276 |
| 277 static void _setTableEntry(var table, var key, var value) { |
| 278 assert(value != null); |
| 279 JS('void', '#[#] = #', table, key, value); |
| 280 } |
| 281 |
| 282 static void _deleteTableEntry(var table, var key) { |
| 283 JS('void', 'delete #[#]', table, key); |
| 284 } |
| 285 |
| 286 List _getBucket(var table, var key) { |
| 287 var hash = internalComputeHashCode(key); |
| 288 return JS('var', '#[#]', table, hash); |
| 289 } |
| 290 |
| 291 int internalFindBucketIndex(var bucket, var key) { |
| 292 if (bucket == null) return -1; |
| 293 int length = JS('int', '#.length', bucket); |
| 294 for (int i = 0; i < length; i++) { |
| 295 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); |
| 296 if (cell.key == key) return i; |
| 297 } |
| 298 return -1; |
| 299 } |
| 300 |
| 301 static _newHashTable() { |
| 302 // Create a new JavaScript object to be used as a hash table. Use |
| 303 // Object.create to avoid the properties on Object.prototype |
| 304 // showing up as entries. |
| 305 var table = JS('var', 'Object.create(null)'); |
| 306 // Attempt to force the hash table into 'dictionary' mode by |
| 307 // adding a property to it and deleting it again. |
| 308 var temporaryKey = '<non-identifier-key>'; |
| 309 _setTableEntry(table, temporaryKey, table); |
| 310 _deleteTableEntry(table, temporaryKey); |
| 311 return table; |
| 312 } |
| 313 |
| 314 String toString() => Maps.mapToString(this); |
| 315 } |
| 316 |
| 317 class LinkedHashMapCell { |
| 318 final key; |
| 319 var value; |
| 320 |
| 321 LinkedHashMapCell _next; |
| 322 LinkedHashMapCell _previous; |
| 323 |
| 324 LinkedHashMapCell(this.key, this.value); |
| 325 } |
| 326 |
| 327 class LinkedHashMapKeyIterable<E> extends IterableBase<E> |
| 328 implements EfficientLength { |
| 329 final _map; |
| 330 LinkedHashMapKeyIterable(this._map); |
| 331 |
| 332 int get length => _map._length; |
| 333 bool get isEmpty => _map._length == 0; |
| 334 |
| 335 Iterator<E> get iterator { |
| 336 return new LinkedHashMapKeyIterator<E>(_map, _map._modifications); |
| 337 } |
| 338 |
| 339 bool contains(Object element) { |
| 340 return _map.containsKey(element); |
| 341 } |
| 342 |
| 343 void forEach(void f(E element)) { |
| 344 LinkedHashMapCell cell = _map._first; |
| 345 int modifications = _map._modifications; |
| 346 while (cell != null) { |
| 347 f(cell.key); |
| 348 if (modifications != _map._modifications) { |
| 349 throw new ConcurrentModificationError(_map); |
| 350 } |
| 351 cell = cell._next; |
| 352 } |
| 353 } |
| 354 } |
| 355 |
| 356 class LinkedHashMapKeyIterator<E> implements Iterator<E> { |
| 357 final _map; |
| 358 final int _modifications; |
| 359 LinkedHashMapCell _cell; |
| 360 E _current; |
| 361 |
| 362 LinkedHashMapKeyIterator(this._map, this._modifications) { |
| 363 _cell = _map._first; |
| 364 } |
| 365 |
| 366 E get current => _current; |
| 367 |
| 368 bool moveNext() { |
| 369 if (_modifications != _map._modifications) { |
| 370 throw new ConcurrentModificationError(_map); |
| 371 } else if (_cell == null) { |
| 372 _current = null; |
| 373 return false; |
| 374 } else { |
| 375 _current = _cell.key; |
| 376 _cell = _cell._next; |
| 377 return true; |
| 378 } |
| 379 } |
| 380 } |
| OLD | NEW |