Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class LinkIterator<T> implements Iterator<T> { | 5 class LinkIterator<T> implements Iterator<T> { |
| 6 Link<T> current; | 6 Link<T> current; |
| 7 LinkIterator(Link<T> this.current); | 7 LinkIterator(Link<T> this.current); |
| 8 bool hasNext() => !current.isEmpty(); | 8 bool hasNext() => !current.isEmpty(); |
| 9 T next() { | 9 T next() { |
| 10 T result = current.head; | 10 T result = current.head; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 Link<T> reversePrependAll(Link<T> from) { | 63 Link<T> reversePrependAll(Link<T> from) { |
| 64 if (from.isEmpty()) return this; | 64 if (from.isEmpty()) return this; |
| 65 return this.prepend(from.head).reversePrependAll(from.tail); | 65 return this.prepend(from.head).reversePrependAll(from.tail); |
| 66 } | 66 } |
| 67 | 67 |
| 68 List toList() => const []; | 68 List toList() => const []; |
| 69 | 69 |
| 70 bool isEmpty() => true; | 70 bool isEmpty() => true; |
| 71 | 71 |
| 72 void forEach(void f(T element)) {} | 72 void forEach(void f(T element)) {} |
| 73 | |
| 74 bool equals(other) { | |
| 75 if (other is !Link<T>) return false; | |
| 76 return other.isEmpty(); | |
| 77 } | |
| 73 } | 78 } |
| 74 | 79 |
| 75 class LinkEntry<T> implements Link<T> { | 80 class LinkEntry<T> implements Link<T> { |
| 76 final T head; | 81 final T head; |
| 77 Link<T> tail; | 82 Link<T> tail; |
| 78 | 83 |
| 79 LinkEntry(T this.head, Link<T> this.tail); | 84 LinkEntry(T this.head, Link<T> this.tail); |
| 80 | 85 |
| 81 Link<T> prepend(T element) { | 86 Link<T> prepend(T element) { |
| 82 // TODO(ahe): Use new Link<T>, but this cost 8% performance on VM. | 87 // TODO(ahe): Use new Link<T>, but this cost 8% performance on VM. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 list.addLast(link.head); | 132 list.addLast(link.head); |
| 128 } | 133 } |
| 129 return list; | 134 return list; |
| 130 } | 135 } |
| 131 | 136 |
| 132 void forEach(void f(T element)) { | 137 void forEach(void f(T element)) { |
| 133 for (Link<T> link = this; !link.isEmpty(); link = link.tail) { | 138 for (Link<T> link = this; !link.isEmpty(); link = link.tail) { |
| 134 f(link.head); | 139 f(link.head); |
| 135 } | 140 } |
| 136 } | 141 } |
| 142 | |
| 143 bool equals(other) { | |
| 144 if (other is !Link<T>) return false; | |
|
ngeoffray
2012/09/05 12:50:40
Making it recursive looks easier to understand. Bu
karlklose
2012/09/06 11:04:02
I will keep it as it is.
| |
| 145 Link<T> myElements = this; | |
| 146 while (!myElements.isEmpty() && !other.isEmpty()) { | |
| 147 if (myElements.head != other.head) { | |
| 148 return false; | |
| 149 } | |
| 150 myElements = myElements.tail; | |
| 151 other = other.tail; | |
| 152 } | |
| 153 return myElements.isEmpty() && other.isEmpty(); | |
| 154 } | |
| 137 } | 155 } |
| 138 | 156 |
| 139 class LinkBuilderImplementation<T> implements LinkBuilder<T> { | 157 class LinkBuilderImplementation<T> implements LinkBuilder<T> { |
| 140 LinkEntry<T> head = null; | 158 LinkEntry<T> head = null; |
| 141 LinkEntry<T> lastLink = null; | 159 LinkEntry<T> lastLink = null; |
| 142 int length = 0; | 160 int length = 0; |
| 143 | 161 |
| 144 LinkBuilderImplementation(); | 162 LinkBuilderImplementation(); |
| 145 | 163 |
| 146 Link<T> toLink() { | 164 Link<T> toLink() { |
| 147 if (head === null) return const LinkTail(); | 165 if (head === null) return const LinkTail(); |
| 148 lastLink.tail = const LinkTail(); | 166 lastLink.tail = const LinkTail(); |
| 149 Link<T> link = head; | 167 Link<T> link = head; |
| 150 lastLink = null; | 168 lastLink = null; |
| 151 head = null; | 169 head = null; |
| 152 return link; | 170 return link; |
| 153 } | 171 } |
| 154 | 172 |
| 155 void addLast(T t) { | 173 void addLast(T t) { |
| 156 length++; | 174 length++; |
| 157 LinkEntry<T> entry = new LinkEntry<T>(t, null); | 175 LinkEntry<T> entry = new LinkEntry<T>(t, null); |
| 158 if (head === null) { | 176 if (head === null) { |
| 159 head = entry; | 177 head = entry; |
| 160 } else { | 178 } else { |
| 161 lastLink.tail = entry; | 179 lastLink.tail = entry; |
| 162 } | 180 } |
| 163 lastLink = entry; | 181 lastLink = entry; |
| 164 } | 182 } |
| 165 } | 183 } |
| OLD | NEW |