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 library fasta.util.link; | 5 library fasta.util.link; |
6 | 6 |
7 import 'link_implementation.dart' show | 7 import 'link_implementation.dart' show |
8 LinkBuilderImplementation, | 8 LinkBuilderImplementation, |
9 LinkEntry, | 9 LinkEntry, |
10 LinkIterator, | 10 LinkIterator, |
(...skipping 22 matching lines...) Expand all Loading... |
33 result.length = slowLength(); | 33 result.length = slowLength(); |
34 } | 34 } |
35 int i = 0; | 35 int i = 0; |
36 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | 36 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
37 result[i++] = link.head; | 37 result[i++] = link.head; |
38 } | 38 } |
39 return result; | 39 return result; |
40 } | 40 } |
41 | 41 |
42 /// Lazily maps over this linked list, returning an [Iterable]. | 42 /// Lazily maps over this linked list, returning an [Iterable]. |
43 Iterable map(dynamic fn(T item)) { | 43 Iterable<K> map<K>(K fn(T item)) { |
44 return new MappedLinkIterable<T, dynamic>(this, fn); | 44 return new MappedLinkIterable<T, K>(this, fn); |
45 } | 45 } |
46 | 46 |
47 /// Invokes `fn` for every item in the linked list and returns the results | 47 /// Invokes `fn` for every item in the linked list and returns the results |
48 /// in a [List]. | 48 /// in a [List]. |
49 List mapToList(dynamic fn(T item), {bool growable: true}) { | 49 List mapToList(dynamic fn(T item), {bool growable: true}) { |
50 List result; | 50 List result; |
51 if (!growable) { | 51 if (!growable) { |
52 result = new List(slowLength()); | 52 result = new List(slowLength()); |
53 } else { | 53 } else { |
54 result = new List(); | 54 result = new List(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 87 |
88 String toString() => "[]"; | 88 String toString() => "[]"; |
89 | 89 |
90 get length { | 90 get length { |
91 throw new UnsupportedError('get:length'); | 91 throw new UnsupportedError('get:length'); |
92 } | 92 } |
93 | 93 |
94 int slowLength() => 0; | 94 int slowLength() => 0; |
95 | 95 |
96 // TODO(ahe): Remove this method? | 96 // TODO(ahe): Remove this method? |
97 bool contains(T element) { | 97 bool contains(Object element) { |
98 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | 98 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
99 if (link.head == element) return true; | 99 if (link.head == element) return true; |
100 } | 100 } |
101 return false; | 101 return false; |
102 } | 102 } |
103 | 103 |
104 // TODO(ahe): Remove this method? | 104 // TODO(ahe): Remove this method? |
105 T get single { | 105 T get single { |
106 if (isEmpty) throw new StateError('No elements'); | 106 if (isEmpty) throw new StateError('No elements'); |
107 if (!tail.isEmpty) throw new StateError('More than one element'); | 107 if (!tail.isEmpty) throw new StateError('More than one element'); |
(...skipping 16 matching lines...) Expand all Loading... |
124 return true; | 124 return true; |
125 } | 125 } |
126 | 126 |
127 Link copyWithout(e) => this; | 127 Link copyWithout(e) => this; |
128 | 128 |
129 // | 129 // |
130 // Unsupported Iterable<T> methods. | 130 // Unsupported Iterable<T> methods. |
131 // | 131 // |
132 bool any(bool f(T e)) => _unsupported('any'); | 132 bool any(bool f(T e)) => _unsupported('any'); |
133 T elementAt(int i) => _unsupported('elementAt'); | 133 T elementAt(int i) => _unsupported('elementAt'); |
134 Iterable expand(Iterable f(T e)) => _unsupported('expand'); | 134 Iterable<K> expand<K>(Iterable<K> f(T e)) => _unsupported('expand'); |
135 T firstWhere(bool f(T e), {T orElse()}) => _unsupported('firstWhere'); | 135 T firstWhere(bool f(T e), {T orElse()}) => _unsupported('firstWhere'); |
136 fold(initialValue, combine(value, T element)) => _unsupported('fold'); | 136 K fold<K>(K initialValue, K combine(K value, T element)) => _unsupported('fold
'); |
137 T get last => _unsupported('get:last'); | 137 T get last => _unsupported('get:last'); |
138 T lastWhere(bool f(T e), {T orElse()}) => _unsupported('lastWhere'); | 138 T lastWhere(bool f(T e), {T orElse()}) => _unsupported('lastWhere'); |
139 String join([separator = '']) => _unsupported('join'); | 139 String join([separator = '']) => _unsupported('join'); |
140 T reduce(T combine(T a, T b)) => _unsupported('reduce'); | 140 T reduce(T combine(T a, T b)) => _unsupported('reduce'); |
141 T singleWhere(bool f(T e)) => _unsupported('singleWhere'); | 141 T singleWhere(bool f(T e)) => _unsupported('singleWhere'); |
142 Iterable<T> skipWhile(bool f(T e)) => _unsupported('skipWhile'); | 142 Iterable<T> skipWhile(bool f(T e)) => _unsupported('skipWhile'); |
143 Iterable<T> take(int n) => _unsupported('take'); | 143 Iterable<T> take(int n) => _unsupported('take'); |
144 Iterable<T> takeWhile(bool f(T e)) => _unsupported('takeWhile'); | 144 Iterable<T> takeWhile(bool f(T e)) => _unsupported('takeWhile'); |
145 Set<T> toSet() => _unsupported('toSet'); | 145 Set<T> toSet() => _unsupported('toSet'); |
146 Iterable<T> where(bool f(T e)) => _unsupported('where'); | 146 Iterable<T> where(bool f(T e)) => _unsupported('where'); |
147 | 147 |
148 _unsupported(String method) => throw new UnsupportedError(method); | 148 _unsupported(String method) => throw new UnsupportedError(method); |
149 } | 149 } |
150 | 150 |
151 /// Builder object for creating linked lists using [Link] or fixed-length [List] | 151 /// Builder object for creating linked lists using [Link] or fixed-length [List] |
152 /// objects. | 152 /// objects. |
153 abstract class LinkBuilder<T> { | 153 abstract class LinkBuilder<T> { |
154 factory LinkBuilder() = LinkBuilderImplementation; | 154 factory LinkBuilder() = LinkBuilderImplementation<T>; |
155 | 155 |
156 /// Prepends all elements added to the builder to [tail]. The resulting list | 156 /// Prepends all elements added to the builder to [tail]. The resulting list |
157 /// is returned and the builder is cleared. | 157 /// is returned and the builder is cleared. |
158 Link<T> toLink([Link<T> tail = const Link()]); | 158 Link<T> toLink([Link<T> tail = const Link()]); |
159 | 159 |
160 /// Creates a new fixed length containing all added elements. The | 160 /// Creates a new fixed length containing all added elements. The |
161 /// resulting list is returned and the builder is cleared. | 161 /// resulting list is returned and the builder is cleared. |
162 List<T> toList(); | 162 List<T> toList(); |
163 | 163 |
164 /// Adds the element [t] to the end of the list being built. | 164 /// Adds the element [t] to the end of the list being built. |
165 Link<T> addLast(T t); | 165 Link<T> addLast(T t); |
166 | 166 |
167 /// Returns the first element in the list being built. | 167 /// Returns the first element in the list being built. |
168 T get first; | 168 T get first; |
169 | 169 |
170 /// Returns the number of elements in the list being built. | 170 /// Returns the number of elements in the list being built. |
171 final int length; | 171 final int length; |
172 | 172 |
173 /// Returns `true` if the list being built is empty. | 173 /// Returns `true` if the list being built is empty. |
174 final bool isEmpty; | 174 final bool isEmpty; |
175 | 175 |
176 /// Removes all added elements and resets the builder. | 176 /// Removes all added elements and resets the builder. |
177 void clear(); | 177 void clear(); |
178 } | 178 } |
OLD | NEW |