| 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 LinkFactory { | 5 // TODO(ahe): This class should not be generic. |
| 6 static Link createLink(head, [Link tail]) { | 6 class LinkFactory<T> { |
| 7 factory Link(head, [Link tail]) { |
| 7 return new LinkEntry(head, (tail === null) ? const LinkTail() : tail); | 8 return new LinkEntry(head, (tail === null) ? const LinkTail() : tail); |
| 8 } | 9 } |
| 9 | 10 |
| 10 static Link createFromList(List list) { | 11 factory Link.fromList(List list) { |
| 11 switch (list.length) { | 12 switch (list.length) { |
| 12 case 0: | 13 case 0: |
| 13 return const LinkTail(); | 14 return const LinkTail(); |
| 14 case 1: | 15 case 1: |
| 15 return createLink(list[0]); | 16 return new Link(list[0]); |
| 16 case 2: | 17 case 2: |
| 17 return createLink(list[0], createLink(list[1])); | 18 return new Link(list[0], new Link(list[1])); |
| 18 case 3: | 19 case 3: |
| 19 return createLink(list[0], createLink(list[1], createLink(list[2]))); | 20 return new Link(list[0], new Link(list[1], new Link(list[2]))); |
| 20 } | 21 } |
| 21 Link link = createLink(list.last()); | 22 Link link = new Link(list.last()); |
| 22 for (int i = list.length - 1; i > 0; i--) { | 23 for (int i = list.length - 1; i > 0; i--) { |
| 23 link = link.prepend(list[i - 1]); | 24 link = link.prepend(list[i - 1]); |
| 24 } | 25 } |
| 25 return link; | 26 return link; |
| 26 } | 27 } |
| 27 } | 28 } |
| 28 | 29 |
| 29 class AbstractLink<T> implements Link<T> { | 30 class AbstractLink<T> implements Link<T> { |
| 30 T get head() { throw "bug"; } // TODO(ahe): Work around VM bug. | 31 T get head() { throw "bug"; } // TODO(ahe): Work around VM bug. |
| 31 T get tail() { throw "bug"; } // TODO(ahe): Work around VM bug. | 32 T get tail() { throw "bug"; } // TODO(ahe): Work around VM bug. |
| 32 abstract List<T> toList(); // TODO(ahe): Work around Frog bug #318. | 33 abstract List<T> toList(); // TODO(ahe): Work around Frog bug #318. |
| 33 abstract bool isEmpty(); // TODO(ahe): Work around Frog bug #318. | 34 abstract bool isEmpty(); // TODO(ahe): Work around Frog bug #318. |
| 34 | 35 |
| 35 const AbstractLink(); | 36 const AbstractLink(); |
| 36 | 37 |
| 37 Link<T> prepend(T element) { | 38 Link<T> prepend(T element) { |
| 38 return LinkFactory.createLink(element, this); | 39 return new Link<T>(element, this); |
| 39 } | 40 } |
| 40 | 41 |
| 41 Iterator<T> iterator() => toList().iterator(); | 42 Iterator<T> iterator() => toList().iterator(); |
| 42 | 43 |
| 43 void printOn(StringBuffer buffer, [separatedBy]) { | 44 void printOn(StringBuffer buffer, [separatedBy]) { |
| 44 if (isEmpty()) return; | 45 if (isEmpty()) return; |
| 45 // TODO(ngeofray): Work around Frog bug | 46 // TODO(ngeofray): Work around Frog bug |
| 46 buffer.add(head === null ? 'null' : head); | 47 buffer.add(head === null ? 'null' : head); |
| 47 if (separatedBy === null) separatedBy = ''; | 48 if (separatedBy === null) separatedBy = ''; |
| 48 for (Link link = tail; !link.isEmpty(); link = link.tail) { | 49 for (Link link = tail; !link.isEmpty(); link = link.tail) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 void addLast(T t) { | 116 void addLast(T t) { |
| 116 LinkEntry<T> entry = new LinkEntry<T>(t, null); | 117 LinkEntry<T> entry = new LinkEntry<T>(t, null); |
| 117 if (head === null) { | 118 if (head === null) { |
| 118 head = entry; | 119 head = entry; |
| 119 } else { | 120 } else { |
| 120 lastLink.realTail = entry; | 121 lastLink.realTail = entry; |
| 121 } | 122 } |
| 122 lastLink = entry; | 123 lastLink = entry; |
| 123 } | 124 } |
| 124 } | 125 } |
| OLD | NEW |