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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/linked_hash_map.dart

Issue 1032783003: dart2js: use Es6 maps when available. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Move code around Created 5 years, 9 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
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/collection_patch.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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 // Efficient JavaScript based implementation of a linked hash map used as a 5 // Efficient JavaScript based implementation of a linked hash map used as a
6 // backing map for constant maps and the [LinkedHashMap] patch 6 // backing map for constant maps and the [LinkedHashMap] patch
7 7
8 part of _js_helper; 8 part of _js_helper;
9 9
10 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap { 10 class JsLinkedHashMap<K, V> implements LinkedHashMap<K, V>, InternalMap {
(...skipping 16 matching lines...) Expand all
27 LinkedHashMapCell _first; 27 LinkedHashMapCell _first;
28 LinkedHashMapCell _last; 28 LinkedHashMapCell _last;
29 29
30 // We track the number of modifications done to the key set of the 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 31 // hash map to be able to throw when the map is modified while being
32 // iterated over. 32 // iterated over.
33 int _modifications = 0; 33 int _modifications = 0;
34 34
35 JsLinkedHashMap(); 35 JsLinkedHashMap();
36 36
37
38 int get length => _length; 37 int get length => _length;
39 bool get isEmpty => _length == 0; 38 bool get isEmpty => _length == 0;
40 bool get isNotEmpty => !isEmpty; 39 bool get isNotEmpty => !isEmpty;
41 40
42 Iterable<K> get keys { 41 Iterable<K> get keys {
43 return new LinkedHashMapKeyIterable<K>(this); 42 return new LinkedHashMapKeyIterable<K>(this);
44 } 43 }
45 44
46 Iterable<V> get values { 45 Iterable<V> get values {
47 return new MappedIterable<K, V>(keys, (each) => this[each]); 46 return new MappedIterable<K, V>(keys, (each) => this[each]);
48 } 47 }
49 48
50 bool containsKey(Object key) { 49 bool containsKey(Object key) {
51 if (_isStringKey(key)) { 50 if (_isStringKey(key)) {
52 var strings = _strings; 51 var strings = _strings;
53 if (strings == null) return false; 52 if (strings == null) return false;
54 LinkedHashMapCell cell = _getTableEntry(strings, key); 53 return _containsTableEntry(strings, key);
55 return cell != null;
56 } else if (_isNumericKey(key)) { 54 } else if (_isNumericKey(key)) {
57 var nums = _nums; 55 var nums = _nums;
58 if (nums == null) return false; 56 if (nums == null) return false;
59 LinkedHashMapCell cell = _getTableEntry(nums, key); 57 return _containsTableEntry(nums, key);
60 return cell != null;
61 } else { 58 } else {
62 return internalContainsKey(key); 59 return internalContainsKey(key);
63 } 60 }
64 } 61 }
65 62
66 bool internalContainsKey(Object key) { 63 bool internalContainsKey(Object key) {
67 var rest = _rest; 64 var rest = _rest;
68 if (rest == null) return false; 65 if (rest == null) return false;
69 var bucket = _getBucket(rest, key); 66 var bucket = _getBucket(rest, key);
70 return internalFindBucketIndex(bucket, key) >= 0; 67 return internalFindBucketIndex(bucket, key) >= 0;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 _addHashTableEntry(nums, key, value); 114 _addHashTableEntry(nums, key, value);
118 } else { 115 } else {
119 internalSet(key, value); 116 internalSet(key, value);
120 } 117 }
121 } 118 }
122 119
123 void internalSet(K key, V value) { 120 void internalSet(K key, V value) {
124 var rest = _rest; 121 var rest = _rest;
125 if (rest == null) _rest = rest = _newHashTable(); 122 if (rest == null) _rest = rest = _newHashTable();
126 var hash = internalComputeHashCode(key); 123 var hash = internalComputeHashCode(key);
127 var bucket = JS('var', '#[#]', rest, hash); 124 var bucket = JS('var', '#[#]', rest, hash);
Vyacheslav Egorov (Google) 2015/03/25 08:43:52 should use _getBucketHere
floitsch 2015/03/25 23:58:15 applied your patch.
128 if (bucket == null) { 125 if (bucket == null) {
129 LinkedHashMapCell cell = _newLinkedCell(key, value); 126 LinkedHashMapCell cell = _newLinkedCell(key, value);
130 _setTableEntry(rest, hash, JS('var', '[#]', cell)); 127 _setTableEntry(rest, hash, JS('var', '[#]', cell));
131 } else { 128 } else {
132 int index = internalFindBucketIndex(bucket, key); 129 int index = internalFindBucketIndex(bucket, key);
133 if (index >= 0) { 130 if (index >= 0) {
134 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index); 131 LinkedHashMapCell cell = JS('var', '#[#]', bucket, index);
135 cell.hashMapCellValue = value; 132 cell.hashMapCellValue = value;
136 } else { 133 } else {
137 LinkedHashMapCell cell = _newLinkedCell(key, value); 134 LinkedHashMapCell cell = _newLinkedCell(key, value);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 assert(cell == _last); 243 assert(cell == _last);
247 _last = previous; 244 _last = previous;
248 } else { 245 } else {
249 next._previous = previous; 246 next._previous = previous;
250 } 247 }
251 _length--; 248 _length--;
252 _modified(); 249 _modified();
253 } 250 }
254 251
255 static bool _isStringKey(var key) { 252 static bool _isStringKey(var key) {
256 return key is String && key != '__proto__'; 253 return key is String;
257 } 254 }
258 255
259 static bool _isNumericKey(var key) { 256 static bool _isNumericKey(var key) {
260 // Only treat unsigned 30-bit integers as numeric keys. This way, 257 // 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 258 // we avoid converting them to strings when we use them as keys in
262 // the JavaScript hash table object. 259 // the JavaScript hash table object.
263 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key); 260 return key is num && JS('bool', '(# & 0x3ffffff) === #', key, key);
264 } 261 }
265 262
266 int internalComputeHashCode(var key) { 263 int internalComputeHashCode(var key) {
267 // We force the hash codes to be unsigned 30-bit integers to avoid 264 // We force the hash codes to be unsigned 30-bit integers to avoid
268 // issues with problematic keys like '__proto__'. Another option 265 // issues with problematic keys like '__proto__'. Another option
269 // would be to throw an exception if the hash code isn't a number. 266 // would be to throw an exception if the hash code isn't a number.
270 return JS('int', '# & 0x3ffffff', key.hashCode); 267 return JS('int', '# & 0x3ffffff', key.hashCode);
271 } 268 }
272 269
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) { 270 List _getBucket(var table, var key) {
287 var hash = internalComputeHashCode(key); 271 var hash = internalComputeHashCode(key);
288 return JS('var', '#[#]', table, hash); 272 return JS('var', '#[#]', table, hash);
289 } 273 }
290 274
291 int internalFindBucketIndex(var bucket, var key) { 275 int internalFindBucketIndex(var bucket, var key) {
292 if (bucket == null) return -1; 276 if (bucket == null) return -1;
293 int length = JS('int', '#.length', bucket); 277 int length = JS('int', '#.length', bucket);
294 for (int i = 0; i < length; i++) { 278 for (int i = 0; i < length; i++) {
295 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i); 279 LinkedHashMapCell cell = JS('var', '#[#]', bucket, i);
296 if (cell.hashMapCellKey == key) return i; 280 if (cell.hashMapCellKey == key) return i;
297 } 281 }
298 return -1; 282 return -1;
299 } 283 }
300 284
301 static _newHashTable() { 285 String toString() => Maps.mapToString(this);
286
287 static bool get supportsEs6Maps {
288 return JS('returns:bool;depends:none;effects:none;',
289 'typeof Map != "undefined"');
290 }
291
292 _getTableEntry(var table, var key) {
293 return JS('var', '#[#]', table, key);
294 }
295
296 void _setTableEntry(var table, var key, var value) {
297 assert(value != null);
298 JS('void', '#[#] = #', table, key, value);
299 }
300
301 void _deleteTableEntry(var table, var key) {
302 JS('void', 'delete #[#]', table, key);
303 }
304
305 bool _containsTableEntry(var table, var key) {
306 LinkedHashMapCell cell = _getTableEntry(table, key);
307 return cell != null;
308 }
309
310 _newHashTable() {
302 // Create a new JavaScript object to be used as a hash table. Use 311 // Create a new JavaScript object to be used as a hash table. Use
303 // Object.create to avoid the properties on Object.prototype 312 // Object.create to avoid the properties on Object.prototype
304 // showing up as entries. 313 // showing up as entries.
305 var table = JS('var', 'Object.create(null)'); 314 var table = JS('var', 'Object.create(null)');
306 // Attempt to force the hash table into 'dictionary' mode by 315 // Attempt to force the hash table into 'dictionary' mode by
307 // adding a property to it and deleting it again. 316 // adding a property to it and deleting it again.
308 var temporaryKey = '<non-identifier-key>'; 317 var temporaryKey = '<non-identifier-key>';
309 _setTableEntry(table, temporaryKey, table); 318 _setTableEntry(table, temporaryKey, table);
310 _deleteTableEntry(table, temporaryKey); 319 _deleteTableEntry(table, temporaryKey);
311 return table; 320 return table;
312 } 321 }
322 }
313 323
314 String toString() => Maps.mapToString(this); 324 class Es6LinkedHashMap<K, V> extends JsLinkedHashMap<K, V> {
325
326 @override
327 _getTableEntry(var table, var key) {
328 return JS('var', '#.get(#)', table, key);
329 }
330
331 @override
332 void _setTableEntry(var table, var key, var value) {
333 JS('void', '#.set(#, #)', table, key, value);
334 }
335
336 @override
337 void _deleteTableEntry(var table, var key) {
338 JS('void', '#.delete(#)', table, key);
339 }
340
341 @override
342 bool _containsTableEntry(var table, var key) {
343 return JS('bool', '#.has(#)', table, key);
344 }
345
346 @override
347 _newHashTable() {
348 return JS('var', 'new Map()');
349 }
315 } 350 }
316 351
317 class LinkedHashMapCell { 352 class LinkedHashMapCell {
318 final hashMapCellKey; 353 final hashMapCellKey;
319 var hashMapCellValue; 354 var hashMapCellValue;
320 355
321 LinkedHashMapCell _next; 356 LinkedHashMapCell _next;
322 LinkedHashMapCell _previous; 357 LinkedHashMapCell _previous;
323 358
324 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue); 359 LinkedHashMapCell(this.hashMapCellKey, this.hashMapCellValue);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 } else if (_cell == null) { 406 } else if (_cell == null) {
372 _current = null; 407 _current = null;
373 return false; 408 return false;
374 } else { 409 } else {
375 _current = _cell.hashMapCellKey; 410 _current = _cell.hashMapCellKey;
376 _cell = _cell._next; 411 _cell = _cell._next;
377 return true; 412 return true;
378 } 413 }
379 } 414 }
380 } 415 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/collection_patch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698