Chromium Code Reviews| 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 // Patch file for dart:collection classes. | |
| 6 import 'dart:_foreign_helper' show JS; | |
| 7 | |
| 8 patch class HashMap<K, V> { | |
| 9 int _length = 0; | |
| 10 | |
| 11 // The hash map contents is divided into three parts: one part for | |
| 12 // string keys, one for numeric keys, and one for the rest. String | |
| 13 // and numeric keys map directly to their values, but the rest of | |
| 14 // the entries are stored in bucket lists of the form: | |
| 15 // | |
| 16 // [key-0, value-0, key-1, value-1, ...] | |
| 17 // | |
| 18 // where all keys in the same bucket share the same hash code. | |
| 19 var _strings; | |
| 20 var _nums; | |
| 21 var _rest; | |
| 22 | |
| 23 // When iterating over the hash map, it is very convenient to have a | |
| 24 // list of all the keys. We cache that on the instance and clear the | |
| 25 // the cache whenever the key set changes. This is also used to | |
| 26 // guard against concurrent modifications. | |
| 27 List _keys; | |
| 28 | |
| 29 patch HashMap(); | |
| 30 | |
| 31 patch int get length => _length; | |
| 32 patch bool get isEmpty => _length == 0; | |
| 33 | |
| 34 patch Iterable<K> get keys { | |
| 35 return new HashMapKeyIterable<K>(this); | |
| 36 } | |
| 37 | |
| 38 patch Iterable<V> get values { | |
| 39 return keys.map((each) => this[each]); | |
| 40 } | |
| 41 | |
| 42 patch bool containsKey(K key) { | |
| 43 if (_isStringKey(key)) { | |
| 44 var strings = _strings; | |
| 45 return (strings == null) ? false : _hasTableEntry(strings, key); | |
| 46 } else if (_isNumericKey(key)) { | |
| 47 var nums = _nums; | |
| 48 return (nums == null) ? false : _hasTableEntry(nums, key); | |
| 49 } else { | |
| 50 var rest = _rest; | |
| 51 if (rest == null) return false; | |
| 52 var bucket = _getBucket(rest, key); | |
| 53 return _findBucketIndex(bucket, key) >= 0; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 patch bool containsValue(V value) { | |
| 58 return _computeKeys().any((each) => this[each] == value); | |
| 59 } | |
| 60 | |
| 61 patch void addAll(Map<K, V> other) { | |
| 62 other.forEach((K key, V value) { | |
| 63 this[key] = value; | |
| 64 }); | |
| 65 } | |
| 66 | |
| 67 patch V operator[](K key) { | |
| 68 if (_isStringKey(key)) { | |
| 69 var strings = _strings; | |
| 70 return (strings == null) ? null : _getTableEntry(strings, key); | |
| 71 } else if (_isNumericKey(key)) { | |
| 72 var nums = _nums; | |
| 73 return (nums == null) ? null : _getTableEntry(nums, key); | |
| 74 } else { | |
| 75 var rest = _rest; | |
| 76 if (rest == null) return null; | |
| 77 var bucket = _getBucket(rest, key); | |
| 78 int index = _findBucketIndex(bucket, key); | |
| 79 return (index < 0) ? null : JS('var', '#[#]', bucket, index + 1); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 patch void operator[]=(K key, V value) { | |
| 84 if (_isStringKey(key)) { | |
| 85 var strings = _strings; | |
| 86 if (strings == null) _strings = strings = _newHashTable(); | |
| 87 _addHashTableEntry(strings, key, value); | |
| 88 } else if (_isNumericKey(key)) { | |
| 89 var nums = _nums; | |
| 90 if (nums == null) _nums = nums = _newHashTable(); | |
| 91 _addHashTableEntry(nums, key, value); | |
| 92 } else { | |
| 93 var rest = _rest; | |
| 94 if (rest == null) _rest = rest = _newHashTable(); | |
| 95 var hash = _computeHashCode(key); | |
| 96 var bucket = JS('var', '#[#]', rest, hash); | |
| 97 if (bucket == null) { | |
| 98 _setTableEntry(rest, hash, JS('var', '[#, #]', key, value)); | |
| 99 _length++; | |
| 100 _keys = null; | |
| 101 } else { | |
| 102 int index = _findBucketIndex(bucket, key); | |
| 103 if (index >= 0) { | |
| 104 JS('void', '#[#] = #', bucket, index + 1, value); | |
| 105 } else { | |
| 106 JS('void', '#.push(#, #)', bucket, key, value); | |
| 107 _length++; | |
| 108 _keys = null; | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 patch V putIfAbsent(K key, V ifAbsent()) { | |
| 115 if (containsKey(key)) return this[key]; | |
| 116 V value = ifAbsent(); | |
| 117 this[key] = value; | |
| 118 return value; | |
| 119 } | |
| 120 | |
| 121 patch V remove(K key) { | |
| 122 if (_isStringKey(key)) { | |
| 123 return _removeHashTableEntry(_strings, key); | |
| 124 } else if (_isNumericKey(key)) { | |
| 125 return _removeHashTableEntry(_nums, key); | |
| 126 } else { | |
| 127 var rest = _rest; | |
| 128 if (rest == null) return null; | |
| 129 var bucket = _getBucket(rest, key); | |
| 130 int index = _findBucketIndex(bucket, key); | |
| 131 if (index < 0) return null; | |
| 132 _length--; | |
| 133 _keys = null; | |
| 134 // Use splice to remove the two [key, value] elements at the | |
| 135 // index and return the value. | |
| 136 return JS('var', '#.splice(#, 2)[1]', bucket, index); | |
|
erikcorry
2013/04/02 11:20:09
Should you be checking for zero length array here?
kasperl
2013/04/02 11:42:07
Added TODO.
| |
| 137 } | |
| 138 } | |
| 139 | |
| 140 patch void clear() { | |
| 141 if (_length > 0) { | |
| 142 _strings = _nums = _rest = _keys = null; | |
| 143 _length = 0; | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 patch void forEach(void action(K key, V value)) { | |
| 148 List keys = _computeKeys(); | |
| 149 for (int i = 0, length = keys.length; i < length; i++) { | |
| 150 var key = JS('var', '#[#]', keys, i); | |
| 151 action(key, this[key]); | |
| 152 if (JS('bool', '# !== #', keys, _keys)) { | |
| 153 throw new ConcurrentModificationError(this); | |
| 154 } | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 List _computeKeys() { | |
| 159 if (_keys != null) return _keys; | |
| 160 List result = new List(_length); | |
| 161 int index = 0; | |
|
erikcorry
2013/04/02 11:20:09
I wonder if this could be profitably (ie smaller c
kasperl
2013/04/02 11:42:07
I'll work on this in another CL.
| |
| 162 | |
| 163 // Add all string keys to the list. | |
| 164 var strings = _strings; | |
| 165 if (strings != null) { | |
| 166 var names = JS('var', 'Object.getOwnPropertyNames(#)', strings); | |
| 167 int entries = JS('int', '#.length', names); | |
| 168 for (int i = 0; i < entries; i++) { | |
| 169 String key = JS('String', '#[#]', names, i); | |
| 170 result[index++] = key; | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 // Add all numeric keys to the list. | |
| 175 var nums = _nums; | |
| 176 if (nums != null) { | |
| 177 var names = JS('var', 'Object.getOwnPropertyNames(#)', nums); | |
| 178 int entries = JS('int', '#.length', names); | |
| 179 for (int i = 0; i < entries; i++) { | |
| 180 // Object.getOwnPropertyNames returns a list of strings, so we | |
| 181 // have to convert the keys back to numbers (+). | |
| 182 num key = JS('num', '+#[#]', names, i); | |
| 183 result[index++] = key; | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 // Add all the remaining keys to the list. | |
| 188 var rest = _rest; | |
| 189 if (rest != null) { | |
| 190 var names = JS('var', 'Object.getOwnPropertyNames(#)', rest); | |
| 191 int entries = JS('int', '#.length', names); | |
| 192 for (int i = 0; i < entries; i++) { | |
| 193 var key = JS('String', '#[#]', names, i); | |
| 194 var bucket = JS('var', '#[#]', rest, key); | |
| 195 int length = JS('int', '#.length', bucket); | |
| 196 for (int i = 0; i < length; i += 2) { | |
| 197 var key = JS('var', '#[#]', bucket, i); | |
| 198 result[index++] = key; | |
| 199 } | |
| 200 } | |
| 201 } | |
| 202 assert(index == _length); | |
| 203 return _keys = result; | |
| 204 } | |
| 205 | |
| 206 _newHashTable() { | |
| 207 // Create a new JavaScript object to be used as a hash table. Use | |
| 208 // Object.create to avoid the properties on Object.prototype | |
| 209 // showing up as entries. | |
| 210 var table = JS('var', 'Object.create(null)'); | |
| 211 // Attempt to force the hash table into 'dictionary' mode by | |
| 212 // adding a property to it and deleting it again. | |
| 213 var temporaryKey = '<non-identifier-key>'; | |
| 214 _setTableEntry(table, temporaryKey, table); | |
| 215 _deleteTableEntry(table, temporaryKey); | |
| 216 return table; | |
| 217 } | |
| 218 | |
| 219 void _addHashTableEntry(var table, K key, V value) { | |
| 220 if (!_hasTableEntry(table, key)) { | |
| 221 _length++; | |
| 222 _keys = null; | |
| 223 } | |
| 224 _setTableEntry(table, key, value); | |
| 225 } | |
| 226 | |
| 227 V _removeHashTableEntry(var table, K key) { | |
| 228 if (table != null && _hasTableEntry(table, key)) { | |
| 229 V value = _getTableEntry(table, key); | |
| 230 _deleteTableEntry(table, key); | |
| 231 _length--; | |
| 232 _keys = null; | |
| 233 return value; | |
| 234 } else { | |
| 235 return null; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 static bool _isStringKey(var key) { | |
| 240 return key is String && key != '__proto__'; | |
| 241 } | |
| 242 | |
| 243 static bool _isNumericKey(var key) { | |
| 244 return key is num && JS('bool', '# === #', key, key); | |
|
erikcorry
2013/04/02 11:20:09
This line deserves a comment.
kasperl
2013/04/02 11:42:07
Done.
| |
| 245 } | |
| 246 | |
| 247 static int _computeHashCode(var key) { | |
| 248 // We force the hash codes to be integers to avoid issues with | |
| 249 // problematic keys like '__proto__'. Another option would be to | |
| 250 // throw an exception if the key isn't a number. | |
| 251 return JS('int', '# | 0', key.hashCode); | |
| 252 } | |
| 253 | |
| 254 static bool _hasTableEntry(var table, var key) { | |
| 255 var entry = JS('var', '#[#]', table, key); | |
| 256 // We take care to only store non-null entries in the table, so we | |
| 257 // can check if the table has an entry for the given key with a | |
| 258 // simple null check. | |
| 259 return JS('bool', '# != null', entry); | |
|
erikcorry
2013/04/02 11:20:09
Surprised this needs JS.
kasperl
2013/04/02 11:42:07
You're right. Not needed.
| |
| 260 } | |
| 261 | |
| 262 static _getTableEntry(var table, var key) { | |
| 263 var entry = JS('var', '#[#]', table, key); | |
| 264 // We store the table itself as the entry to signal that it really | |
| 265 // is a null value, so we have to map back to null here. | |
| 266 return JS('bool', '# === #', entry, table) ? null : entry; | |
| 267 } | |
| 268 | |
| 269 static void _setTableEntry(var table, var key, var value) { | |
| 270 // We only store non-null entries in the table, so we have to | |
| 271 // change null values to refer to the table itself. Such values | |
| 272 // will be recognized and mapped back to null on access. | |
| 273 if (value == null) value = table; | |
| 274 JS('void', '#[#] = #', table, key, value); | |
| 275 } | |
| 276 | |
| 277 static void _deleteTableEntry(var table, var key) { | |
| 278 JS('void', 'delete #[#]', table, key); | |
| 279 } | |
| 280 | |
| 281 static _getBucket(var table, var key) { | |
| 282 var hash = _computeHashCode(key); | |
| 283 return JS('var', '#[#]', table, hash); | |
| 284 } | |
| 285 | |
| 286 static int _findBucketIndex(var bucket, var key) { | |
| 287 if (bucket == null) return -1; | |
| 288 int length = JS('int', '#.length', bucket); | |
| 289 for (int i = 0; i < length; i += 2) { | |
| 290 if (JS('var', '#[#]', bucket, i) == key) return i; | |
| 291 } | |
| 292 return -1; | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 class HashMapKeyIterable<E> extends Iterable<E> { | |
| 297 final HashMap _map; | |
| 298 HashMapKeyIterable(this._map); | |
| 299 | |
| 300 int get length => _map._length; | |
| 301 bool get isEmpty => _map._length == 0; | |
| 302 | |
| 303 Iterator<E> get iterator { | |
| 304 return new HashMapKeyIterator<E>(_map, _map._computeKeys()); | |
| 305 } | |
| 306 | |
| 307 bool contains(E element) { | |
| 308 return _map.containsKey(element); | |
| 309 } | |
| 310 | |
| 311 void forEach(void f(E element)) { | |
| 312 List keys = _map._computeKeys(); | |
| 313 for (int i = 0, length = JS('int', '#.length', keys); i < length; i++) { | |
| 314 f(JS('var', '#[#]', keys, i)); | |
| 315 if (JS('bool', '# !== #', keys, _map._keys)) { | |
| 316 throw new ConcurrentModificationError(_map); | |
| 317 } | |
| 318 } | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 class HashMapKeyIterator<E> implements Iterator<E> { | |
| 323 final HashMap _map; | |
| 324 final List _keys; | |
| 325 int _offset = 0; | |
| 326 E _current; | |
| 327 | |
| 328 HashMapKeyIterator(this._map, this._keys); | |
| 329 | |
| 330 E get current => _current; | |
| 331 | |
| 332 bool moveNext() { | |
| 333 var keys = _keys; | |
| 334 int offset = _offset; | |
| 335 if (JS('bool', '# !== #', keys, _map._keys)) { | |
| 336 throw new ConcurrentModificationError(_map); | |
| 337 } else if (offset >= JS('int', '#.length', keys)) { | |
| 338 _current = null; | |
| 339 return false; | |
| 340 } else { | |
| 341 _current = JS('var', '#[#]', keys, offset); | |
| 342 // TODO(kasperl): For now, we have to tell the type inferrer to | |
| 343 // treat the result of doing offset + 1 as an int. Otherwise, we | |
| 344 // get unnecessary bailout code. | |
| 345 _offset = JS('int', '#', offset + 1); | |
| 346 return true; | |
| 347 } | |
| 348 } | |
| 349 } | |
| OLD | NEW |