| 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 get hasNext => !current.isEmpty(); | 8 bool get hasNext => !current.isEmpty; |
| 9 T next() { | 9 T next() { |
| 10 T result = current.head; | 10 T result = current.head; |
| 11 current = current.tail; | 11 current = current.tail; |
| 12 return result; | 12 return result; |
| 13 } | 13 } |
| 14 } | 14 } |
| 15 | 15 |
| 16 class LinkEntry<T> extends Link<T> { | 16 class LinkEntry<T> extends Link<T> { |
| 17 final T head; | 17 final T head; |
| 18 Link<T> tail; | 18 Link<T> tail; |
| 19 | 19 |
| 20 LinkEntry(T this.head, [Link<T> tail]) | 20 LinkEntry(T this.head, [Link<T> tail]) |
| 21 : this.tail = ((tail == null) ? new Link<T>() : tail); | 21 : this.tail = ((tail == null) ? new Link<T>() : tail); |
| 22 | 22 |
| 23 Link<T> prepend(T element) { | 23 Link<T> prepend(T element) { |
| 24 // TODO(ahe): Use new Link<T>, but this cost 8% performance on VM. | 24 // TODO(ahe): Use new Link<T>, but this cost 8% performance on VM. |
| 25 return new LinkEntry<T>(element, this); | 25 return new LinkEntry<T>(element, this); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void printOn(StringBuffer buffer, [separatedBy]) { | 28 void printOn(StringBuffer buffer, [separatedBy]) { |
| 29 buffer.add(head); | 29 buffer.add(head); |
| 30 if (separatedBy == null) separatedBy = ''; | 30 if (separatedBy == null) separatedBy = ''; |
| 31 for (Link link = tail; !link.isEmpty(); link = link.tail) { | 31 for (Link link = tail; !link.isEmpty; link = link.tail) { |
| 32 buffer.add(separatedBy); | 32 buffer.add(separatedBy); |
| 33 buffer.add(link.head); | 33 buffer.add(link.head); |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 String toString() { | 37 String toString() { |
| 38 StringBuffer buffer = new StringBuffer(); | 38 StringBuffer buffer = new StringBuffer(); |
| 39 buffer.add('[ '); | 39 buffer.add('[ '); |
| 40 printOn(buffer, ', '); | 40 printOn(buffer, ', '); |
| 41 buffer.add(' ]'); | 41 buffer.add(' ]'); |
| 42 return buffer.toString(); | 42 return buffer.toString(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 Link<T> reverse() { | 45 Link<T> reverse() { |
| 46 Link<T> result = const Link(); | 46 Link<T> result = const Link(); |
| 47 for (Link<T> link = this; !link.isEmpty(); link = link.tail) { | 47 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
| 48 result = result.prepend(link.head); | 48 result = result.prepend(link.head); |
| 49 } | 49 } |
| 50 return result; | 50 return result; |
| 51 } | 51 } |
| 52 | 52 |
| 53 Link<T> reversePrependAll(Link<T> from) { | 53 Link<T> reversePrependAll(Link<T> from) { |
| 54 Link<T> result; | 54 Link<T> result; |
| 55 for (result = this; !from.isEmpty(); from = from.tail) { | 55 for (result = this; !from.isEmpty; from = from.tail) { |
| 56 result = result.prepend(from.head); | 56 result = result.prepend(from.head); |
| 57 } | 57 } |
| 58 return result; | 58 return result; |
| 59 } | 59 } |
| 60 | 60 |
| 61 | 61 |
| 62 bool isEmpty() => false; | 62 bool get isEmpty => false; |
| 63 | 63 |
| 64 List<T> toList() { | 64 List<T> toList() { |
| 65 List<T> list = new List<T>(); | 65 List<T> list = new List<T>(); |
| 66 for (Link<T> link = this; !link.isEmpty(); link = link.tail) { | 66 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
| 67 list.addLast(link.head); | 67 list.addLast(link.head); |
| 68 } | 68 } |
| 69 return list; | 69 return list; |
| 70 } | 70 } |
| 71 | 71 |
| 72 void forEach(void f(T element)) { | 72 void forEach(void f(T element)) { |
| 73 for (Link<T> link = this; !link.isEmpty(); link = link.tail) { | 73 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
| 74 f(link.head); | 74 f(link.head); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool operator ==(other) { | 78 bool operator ==(other) { |
| 79 if (other is !Link<T>) return false; | 79 if (other is !Link<T>) return false; |
| 80 Link<T> myElements = this; | 80 Link<T> myElements = this; |
| 81 while (!myElements.isEmpty() && !other.isEmpty()) { | 81 while (!myElements.isEmpty && !other.isEmpty) { |
| 82 if (myElements.head != other.head) { | 82 if (myElements.head != other.head) { |
| 83 return false; | 83 return false; |
| 84 } | 84 } |
| 85 myElements = myElements.tail; | 85 myElements = myElements.tail; |
| 86 other = other.tail; | 86 other = other.tail; |
| 87 } | 87 } |
| 88 return myElements.isEmpty() && other.isEmpty(); | 88 return myElements.isEmpty && other.isEmpty; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 class LinkBuilderImplementation<T> implements LinkBuilder<T> { | 92 class LinkBuilderImplementation<T> implements LinkBuilder<T> { |
| 93 LinkEntry<T> head = null; | 93 LinkEntry<T> head = null; |
| 94 LinkEntry<T> lastLink = null; | 94 LinkEntry<T> lastLink = null; |
| 95 int length = 0; | 95 int length = 0; |
| 96 | 96 |
| 97 LinkBuilderImplementation(); | 97 LinkBuilderImplementation(); |
| 98 | 98 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 109 length++; | 109 length++; |
| 110 LinkEntry<T> entry = new LinkEntry<T>(t, null); | 110 LinkEntry<T> entry = new LinkEntry<T>(t, null); |
| 111 if (head == null) { | 111 if (head == null) { |
| 112 head = entry; | 112 head = entry; |
| 113 } else { | 113 } else { |
| 114 lastLink.tail = entry; | 114 lastLink.tail = entry; |
| 115 } | 115 } |
| 116 lastLink = entry; | 116 lastLink = entry; |
| 117 } | 117 } |
| 118 } | 118 } |
| OLD | NEW |