OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart2js.util; | |
6 | |
7 class Link<T> { | |
8 T get head => throw new StateError("no elements"); | |
9 Link<T> get tail => null; | |
10 | |
11 const Link(); | |
12 | |
13 Link<T> prepend(T element) { | |
14 return new LinkEntry<T>(element, this); | |
15 } | |
16 | |
17 Iterator<T> get iterator => new LinkIterator<T>(this); | |
18 | |
19 void printOn(StringBuffer buffer, [separatedBy]) { | |
20 } | |
21 | |
22 List<T> toList({ bool growable: true }) { | |
23 List<T> result; | |
24 if (!growable) { | |
25 result = new List<T>(slowLength()); | |
26 } else { | |
27 result = new List<T>(); | |
28 result.length = slowLength(); | |
29 } | |
30 int i = 0; | |
31 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | |
32 result[i++] = link.head; | |
33 } | |
34 return result; | |
35 } | |
36 | |
37 /// Lazily maps over this linked list, returning an [Iterable]. | |
38 Iterable map(dynamic fn(T item)) { | |
39 return new MappedLinkIterable<T,dynamic>(this, fn); | |
40 } | |
41 | |
42 /// Invokes `fn` for every item in the linked list and returns the results | |
43 /// in a [List]. | |
44 List mapToList(dynamic fn(T item), { bool growable: true }) { | |
45 List result; | |
46 if (!growable) { | |
47 result = new List(slowLength()); | |
48 } else { | |
49 result = new List(); | |
50 result.length = slowLength(); | |
51 } | |
52 int i = 0; | |
53 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | |
54 result[i++] = fn(link.head); | |
55 } | |
56 return result; | |
57 } | |
58 | |
59 /// Invokes `fn` for every item in the linked list and returns the results | |
60 /// in a [Set]. | |
61 Set mapToSet(dynamic fn(T item)) { | |
62 Set result = new Set(); | |
63 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | |
64 result.add(fn(link.head)); | |
65 } | |
66 return result; | |
67 } | |
68 | |
69 bool get isEmpty => true; | |
70 | |
71 Link<T> reverse() => this; | |
72 | |
73 Link<T> reversePrependAll(Link<T> from) { | |
74 if (from.isEmpty) return this; | |
75 return this.prepend(from.head).reversePrependAll(from.tail); | |
76 } | |
77 | |
78 Link<T> skip(int n) { | |
79 if (n == 0) return this; | |
80 throw new RangeError('Index $n out of range'); | |
81 } | |
82 | |
83 void forEach(void f(T element)) {} | |
84 | |
85 bool operator ==(other) { | |
86 if (other is !Link<T>) return false; | |
87 return other.isEmpty; | |
88 } | |
89 | |
90 int get hashCode => throw new UnsupportedError('Link.hashCode'); | |
91 | |
92 String toString() => "[]"; | |
93 | |
94 get length { | |
95 throw new UnsupportedError('get:length'); | |
96 } | |
97 | |
98 int slowLength() => 0; | |
99 | |
100 // TODO(ahe): Remove this method? | |
101 bool contains(T element) { | |
102 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | |
103 if (link.head == element) return true; | |
104 } | |
105 return false; | |
106 } | |
107 | |
108 // TODO(ahe): Remove this method? | |
109 T get single { | |
110 if (isEmpty) throw new StateError('No elements'); | |
111 if (!tail.isEmpty) throw new StateError('More than one element'); | |
112 return head; | |
113 } | |
114 | |
115 // TODO(ahe): Remove this method? | |
116 T get first { | |
117 if (isEmpty) throw new StateError('No elements'); | |
118 return head; | |
119 } | |
120 | |
121 /// Returns true if f returns true for all elements of this list. | |
122 /// | |
123 /// Returns true for the empty list. | |
124 bool every(bool f(T)) { | |
125 for (Link<T> link = this; !link.isEmpty; link = link.tail){ | |
126 if (!f(link.head)) return false; | |
127 } | |
128 return true; | |
129 } | |
130 } | |
131 | |
132 abstract class LinkBuilder<T> { | |
133 factory LinkBuilder() = LinkBuilderImplementation; | |
134 | |
135 /** | |
136 * Prepends all elements added to the builder to [tail]. The resulting list is | |
137 * returned and the builder is cleared. | |
138 */ | |
139 Link<T> toLink([Link<T> tail = const Link()]); | |
140 | |
141 List<T> toList(); | |
142 | |
143 void addLast(T t); | |
144 | |
145 final int length; | |
146 final bool isEmpty; | |
147 } | |
OLD | NEW |