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 part of dart2js.util; | 5 part of dart2js.util; |
6 | 6 |
7 class Link<T> implements Iterable<T> { | 7 class Link<T> implements Iterable<T> { |
8 T get head => throw new StateError("no elements"); | 8 T get head => throw new StateError("no elements"); |
9 Link<T> get tail => null; | 9 Link<T> get tail => null; |
10 | 10 |
11 const Link(); | 11 const Link(); |
12 | 12 |
13 Link<T> prepend(T element) { | 13 Link<T> prepend(T element) { |
14 return new LinkEntry<T>(element, this); | 14 return new LinkEntry<T>(element, this); |
15 } | 15 } |
16 | 16 |
17 Iterator<T> get iterator => new LinkIterator<T>(this); | 17 Iterator<T> get iterator => new LinkIterator<T>(this); |
18 | 18 |
19 void printOn(StringBuffer buffer, [separatedBy]) { | 19 void printOn(StringBuffer buffer, [separatedBy]) {} |
20 } | |
21 | 20 |
22 List<T> toList({ bool growable: true }) { | 21 List<T> toList({bool growable: true}) { |
23 List<T> result; | 22 List<T> result; |
24 if (!growable) { | 23 if (!growable) { |
25 result = new List<T>(slowLength()); | 24 result = new List<T>(slowLength()); |
26 } else { | 25 } else { |
27 result = new List<T>(); | 26 result = new List<T>(); |
28 result.length = slowLength(); | 27 result.length = slowLength(); |
29 } | 28 } |
30 int i = 0; | 29 int i = 0; |
31 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | 30 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
32 result[i++] = link.head; | 31 result[i++] = link.head; |
33 } | 32 } |
34 return result; | 33 return result; |
35 } | 34 } |
36 | 35 |
37 /// Lazily maps over this linked list, returning an [Iterable]. | 36 /// Lazily maps over this linked list, returning an [Iterable]. |
38 Iterable map(dynamic fn(T item)) { | 37 Iterable map(dynamic fn(T item)) { |
39 return new MappedLinkIterable<T,dynamic>(this, fn); | 38 return new MappedLinkIterable<T, dynamic>(this, fn); |
40 } | 39 } |
41 | 40 |
42 /// Invokes `fn` for every item in the linked list and returns the results | 41 /// Invokes `fn` for every item in the linked list and returns the results |
43 /// in a [List]. | 42 /// in a [List]. |
44 List mapToList(dynamic fn(T item), { bool growable: true }) { | 43 List mapToList(dynamic fn(T item), {bool growable: true}) { |
45 List result; | 44 List result; |
46 if (!growable) { | 45 if (!growable) { |
47 result = new List(slowLength()); | 46 result = new List(slowLength()); |
48 } else { | 47 } else { |
49 result = new List(); | 48 result = new List(); |
50 result.length = slowLength(); | 49 result.length = slowLength(); |
51 } | 50 } |
52 int i = 0; | 51 int i = 0; |
53 for (Link<T> link = this; !link.isEmpty; link = link.tail) { | 52 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
54 result[i++] = fn(link.head); | 53 result[i++] = fn(link.head); |
(...skipping 12 matching lines...) Expand all Loading... |
67 } | 66 } |
68 | 67 |
69 Link<T> skip(int n) { | 68 Link<T> skip(int n) { |
70 if (n == 0) return this; | 69 if (n == 0) return this; |
71 throw new RangeError('Index $n out of range'); | 70 throw new RangeError('Index $n out of range'); |
72 } | 71 } |
73 | 72 |
74 void forEach(void f(T element)) {} | 73 void forEach(void f(T element)) {} |
75 | 74 |
76 bool operator ==(other) { | 75 bool operator ==(other) { |
77 if (other is !Link<T>) return false; | 76 if (other is! Link<T>) return false; |
78 return other.isEmpty; | 77 return other.isEmpty; |
79 } | 78 } |
80 | 79 |
81 int get hashCode => throw new UnsupportedError('Link.hashCode'); | 80 int get hashCode => throw new UnsupportedError('Link.hashCode'); |
82 | 81 |
83 String toString() => "[]"; | 82 String toString() => "[]"; |
84 | 83 |
85 get length { | 84 get length { |
86 throw new UnsupportedError('get:length'); | 85 throw new UnsupportedError('get:length'); |
87 } | 86 } |
(...skipping 18 matching lines...) Expand all Loading... |
106 // TODO(ahe): Remove this method? | 105 // TODO(ahe): Remove this method? |
107 T get first { | 106 T get first { |
108 if (isEmpty) throw new StateError('No elements'); | 107 if (isEmpty) throw new StateError('No elements'); |
109 return head; | 108 return head; |
110 } | 109 } |
111 | 110 |
112 /// Returns true if f returns true for all elements of this list. | 111 /// Returns true if f returns true for all elements of this list. |
113 /// | 112 /// |
114 /// Returns true for the empty list. | 113 /// Returns true for the empty list. |
115 bool every(bool f(T)) { | 114 bool every(bool f(T)) { |
116 for (Link<T> link = this; !link.isEmpty; link = link.tail){ | 115 for (Link<T> link = this; !link.isEmpty; link = link.tail) { |
117 if (!f(link.head)) return false; | 116 if (!f(link.head)) return false; |
118 } | 117 } |
119 return true; | 118 return true; |
120 } | 119 } |
121 | 120 |
122 Link copyWithout(e) => this; | 121 Link copyWithout(e) => this; |
123 | 122 |
124 // | 123 // |
125 // Unsupported Iterable<T> methods. | 124 // Unsupported Iterable<T> methods. |
126 // | 125 // |
127 bool any(bool f(T e)) => _unsupported('any'); | 126 bool any(bool f(T e)) => _unsupported('any'); |
128 T elementAt(int i) => _unsupported('elementAt'); | 127 T elementAt(int i) => _unsupported('elementAt'); |
129 Iterable expand(Iterable f(T e)) => _unsupported('expand'); | 128 Iterable expand(Iterable f(T e)) => _unsupported('expand'); |
130 T firstWhere(bool f(T e), {T orElse()}) => _unsupported('firstWhere'); | 129 T firstWhere(bool f(T e), {T orElse()}) => _unsupported('firstWhere'); |
131 fold(initialValue, combine(value, T element)) => _unsupported('fold'); | 130 fold(initialValue, combine(value, T element)) => _unsupported('fold'); |
132 T get last => _unsupported('get:last'); | 131 T get last => _unsupported('get:last'); |
133 T lastWhere(bool f(T e), {T orElse()}) => _unsupported('lastWhere'); | 132 T lastWhere(bool f(T e), {T orElse()}) => _unsupported('lastWhere'); |
134 String join([separator = '']) => _unsupported('join'); | 133 String join([separator = '']) => _unsupported('join'); |
135 T reduce(T combine(T a, T b)) => _unsupported('reduce'); | 134 T reduce(T combine(T a, T b)) => _unsupported('reduce'); |
136 T singleWhere(bool f(T e)) => _unsupported('singleWhere'); | 135 T singleWhere(bool f(T e)) => _unsupported('singleWhere'); |
137 Iterable<T> skipWhile(bool f(T e)) => _unsupported('skipWhile'); | 136 Iterable<T> skipWhile(bool f(T e)) => _unsupported('skipWhile'); |
138 Iterable<T> take(int n) => _unsupported('take'); | 137 Iterable<T> take(int n) => _unsupported('take'); |
(...skipping 25 matching lines...) Expand all Loading... |
164 | 163 |
165 /// Returns the number of elements in the list being built. | 164 /// Returns the number of elements in the list being built. |
166 final int length; | 165 final int length; |
167 | 166 |
168 /// Returns `true` if the list being built is empty. | 167 /// Returns `true` if the list being built is empty. |
169 final bool isEmpty; | 168 final bool isEmpty; |
170 | 169 |
171 /// Removes all added elements and resets the builder. | 170 /// Removes all added elements and resets the builder. |
172 void clear(); | 171 void clear(); |
173 } | 172 } |
OLD | NEW |