| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 library dart2js.util.setlet; | 5 library dart2js.util.setlet; |
| 6 | 6 |
| 7 import 'dart:collection' show IterableBase; | 7 import 'dart:collection' show IterableBase; |
| 8 | 8 |
| 9 class Setlet<E> extends IterableBase<E> implements Set<E> { | 9 class Setlet<E> extends IterableBase<E> implements Set<E> { |
| 10 static const _MARKER = const _SetletMarker(); | 10 static const _MARKER = const _SetletMarker(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 _contents = element; | 79 _contents = element; |
| 80 return true; | 80 return true; |
| 81 } else if (_contents == element) { | 81 } else if (_contents == element) { |
| 82 // Do nothing. | 82 // Do nothing. |
| 83 return false; | 83 return false; |
| 84 } else { | 84 } else { |
| 85 List list = new List(CAPACITY); | 85 List list = new List(CAPACITY); |
| 86 list[0] = _contents; | 86 list[0] = _contents; |
| 87 list[1] = element; | 87 list[1] = element; |
| 88 _contents = list; | 88 _contents = list; |
| 89 _extra = 2; // Two elements. | 89 _extra = 2; // Two elements. |
| 90 return true; | 90 return true; |
| 91 } | 91 } |
| 92 } else if (_MARKER == _extra) { | 92 } else if (_MARKER == _extra) { |
| 93 return _contents.add(element); | 93 return _contents.add(element); |
| 94 } else { | 94 } else { |
| 95 int remaining = _extra; | 95 int remaining = _extra; |
| 96 int index = 0; | 96 int index = 0; |
| 97 int copyTo, copyFrom; | 97 int copyTo, copyFrom; |
| 98 while (remaining > 0 && index < CAPACITY) { | 98 while (remaining > 0 && index < CAPACITY) { |
| 99 var candidate = _contents[index++]; | 99 var candidate = _contents[index++]; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 123 while (copyFrom < CAPACITY) { | 123 while (copyFrom < CAPACITY) { |
| 124 _contents[copyTo++] = _contents[copyFrom++]; | 124 _contents[copyTo++] = _contents[copyFrom++]; |
| 125 } | 125 } |
| 126 // Insert the new element as the last element. | 126 // Insert the new element as the last element. |
| 127 _contents[copyTo++] = element; | 127 _contents[copyTo++] = element; |
| 128 _extra++; | 128 _extra++; |
| 129 // Clear all elements after the new last elements to | 129 // Clear all elements after the new last elements to |
| 130 // make sure we don't keep extra stuff alive. | 130 // make sure we don't keep extra stuff alive. |
| 131 while (copyTo < CAPACITY) _contents[copyTo++] = null; | 131 while (copyTo < CAPACITY) _contents[copyTo++] = null; |
| 132 } else { | 132 } else { |
| 133 _contents = new Set<E>()..addAll(_contents)..add(element); | 133 _contents = new Set<E>() |
| 134 ..addAll(_contents) |
| 135 ..add(element); |
| 134 _extra = _MARKER; | 136 _extra = _MARKER; |
| 135 } | 137 } |
| 136 return true; | 138 return true; |
| 137 } | 139 } |
| 138 } | 140 } |
| 139 | 141 |
| 140 void addAll(Iterable<E> elements) { | 142 void addAll(Iterable<E> elements) { |
| 141 elements.forEach((each) => add(each)); | 143 elements.forEach((each) => add(each)); |
| 142 } | 144 } |
| 143 | 145 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 if (_MARKER == element) continue; | 228 if (_MARKER == element) continue; |
| 227 action(element); | 229 action(element); |
| 228 remaining--; | 230 remaining--; |
| 229 } | 231 } |
| 230 } | 232 } |
| 231 } | 233 } |
| 232 | 234 |
| 233 bool containsAll(Iterable<E> other) { | 235 bool containsAll(Iterable<E> other) { |
| 234 for (E e in other) { | 236 for (E e in other) { |
| 235 if (!this.contains(e)) return false; | 237 if (!this.contains(e)) return false; |
| 236 }; | 238 } |
| 239 ; |
| 237 return true; | 240 return true; |
| 238 } | 241 } |
| 239 | 242 |
| 240 clear() { | 243 clear() { |
| 241 _contents = _MARKER; | 244 _contents = _MARKER; |
| 242 _extra = null; | 245 _extra = null; |
| 243 } | 246 } |
| 244 | 247 |
| 245 Set<E> union(Set<E> other) => new Set<E>.from(this)..addAll(other); | 248 Set<E> union(Set<E> other) => new Set<E>.from(this)..addAll(other); |
| 246 | 249 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 if (Setlet._MARKER != candidate) { | 306 if (Setlet._MARKER != candidate) { |
| 304 _current = candidate; | 307 _current = candidate; |
| 305 _remaining--; | 308 _remaining--; |
| 306 return true; | 309 return true; |
| 307 } | 310 } |
| 308 } | 311 } |
| 309 _current = null; | 312 _current = null; |
| 310 return false; | 313 return false; |
| 311 } | 314 } |
| 312 } | 315 } |
| OLD | NEW |