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

Side by Side Diff: runtime/lib/compact_hash.dart

Issue 971833002: Correct typing of LinkedHashSet.add. Add unit test. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: 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 | « no previous file | tests/corelib/hash_set_type_check_test.dart » ('j') | 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 import 'dart:typed_data'; 5 import 'dart:typed_data';
6 6
7 // Hash table with open addressing that separates the index from keys/values. 7 // Hash table with open addressing that separates the index from keys/values.
8 abstract class _HashBase { 8 abstract class _HashBase {
9 // Each occupied entry in _index is a fixed-size integer that encodes a pair: 9 // Each occupied entry in _index is a fixed-size integer that encodes a pair:
10 // [ hash pattern for key | index of entry in _data ] 10 // [ hash pattern for key | index of entry in _data ]
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 } else { 330 } else {
331 current = null; 331 current = null;
332 return false; 332 return false;
333 } 333 }
334 } 334 }
335 } 335 }
336 336
337 // Set implementation, analogous to _CompactLinkedHashMap. 337 // Set implementation, analogous to _CompactLinkedHashMap.
338 class _CompactLinkedHashSet<K> 338 class _CompactLinkedHashSet<K>
339 extends SetBase<K> with _HashBase 339 extends SetBase<K> with _HashBase
340 implements HashSet<K>, LinkedHashSet<K> { 340 implements LinkedHashSet<K> {
341 341
342 _CompactLinkedHashSet() { 342 _CompactLinkedHashSet() {
343 assert(_HashBase._UNUSED_PAIR == 0); 343 assert(_HashBase._UNUSED_PAIR == 0);
344 _index = new Uint32List(_HashBase._INITIAL_INDEX_SIZE); 344 _index = new Uint32List(_HashBase._INITIAL_INDEX_SIZE);
345 _data = new List(_HashBase._INITIAL_INDEX_SIZE >> 1); 345 _data = new List(_HashBase._INITIAL_INDEX_SIZE >> 1);
346 } 346 }
347 347
348 int get length => _usedData - _deletedKeys; 348 int get length => _usedData - _deletedKeys;
349 349
350 void _rehash() { 350 void _rehash() {
(...skipping 19 matching lines...) Expand all
370 if (oldData != null) { 370 if (oldData != null) {
371 for (int i = 0; i < oldUsed; i += 1) { 371 for (int i = 0; i < oldUsed; i += 1) {
372 var key = oldData[i]; 372 var key = oldData[i];
373 if (!_HashBase._isDeleted(oldData, key)) { 373 if (!_HashBase._isDeleted(oldData, key)) {
374 add(key); 374 add(key);
375 } 375 }
376 } 376 }
377 } 377 }
378 } 378 }
379 379
380 bool add(Object key) { 380 bool add(K key) {
381 final int size = _index.length; 381 final int size = _index.length;
382 final int sizeMask = size - 1; 382 final int sizeMask = size - 1;
383 final int maxEntries = size >> 1; 383 final int maxEntries = size >> 1;
384 final int fullHash = key.hashCode; 384 final int fullHash = key.hashCode;
385 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size); 385 final int hashPattern = _HashBase._hashPattern(fullHash, _hashMask, size);
386 int i = _HashBase._firstProbe(fullHash, sizeMask); 386 int i = _HashBase._firstProbe(fullHash, sizeMask);
387 int firstDeleted = -1; 387 int firstDeleted = -1;
388 int pair = _index[i]; 388 int pair = _index[i];
389 while (pair != _HashBase._UNUSED_PAIR) { 389 while (pair != _HashBase._UNUSED_PAIR) {
390 if (pair == _HashBase._DELETED_PAIR) { 390 if (pair == _HashBase._DELETED_PAIR) {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 pair = _index[i]; 464 pair = _index[i];
465 } 465 }
466 return false; 466 return false;
467 } 467 }
468 468
469 Iterator<K> get iterator => 469 Iterator<K> get iterator =>
470 new _CompactIterator<K>(this, _data, _usedData, -1, 1); 470 new _CompactIterator<K>(this, _data, _usedData, -1, 1);
471 471
472 Set<K> toSet() => new Set<K>()..addAll(this); 472 Set<K> toSet() => new Set<K>()..addAll(this);
473 } 473 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/hash_set_type_check_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698