OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, 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 /** | |
6 * An indexed sequence of elements of the same type. | |
7 * | |
8 * This is a primitive interface that any finite integer-indexable | |
9 * sequences can implement. | |
floitsch
2012/10/16 14:03:19
sequence (-s)
Lasse Reichstein Nielsen
2012/10/17 11:06:45
Done.
| |
10 */ | |
11 abstract class Sequence<T> { | |
12 int get length; | |
13 T operator[](int index); | |
14 } | |
15 | |
16 /** | |
17 * A skeleton class for a [Collection] that is also a [Sequence]. | |
sra1
2012/10/16 18:52:52
The assumption is that length is efficient, otherw
Lasse Reichstein Nielsen
2012/10/17 11:06:45
True. I'm adding this as comments on the methods o
| |
18 */ | |
19 abstract class SequenceCollection<E> implements Collection<E>, Sequence<E> { | |
20 // The class is intended for use as a mixin as well. | |
21 | |
22 Iterator<E> iterator() => new SequenceIterator(sequence); | |
23 | |
24 void forEach(f(E element)) { | |
25 for (int i = 0; i < this.length; i++) f(this[i]); | |
26 } | |
27 | |
28 Collection map(f(E element)) { | |
29 List result = new List(this.length); | |
floitsch
2012/10/16 14:03:19
This violates the specification of 'map' which spe
sra1
2012/10/16 18:52:52
Dont make the returned value fixed length, since i
Lasse Reichstein Nielsen
2012/10/17 11:06:45
That specification can't be followed exactly. Ther
floitsch
2012/10/17 13:19:32
That was not my point: from what I can see this cl
| |
30 for (int i = 0; i < this.length; i++) { | |
31 result[i] = f(this[i]); | |
32 } | |
33 return result; | |
34 } | |
35 | |
36 bool contains(E value) { | |
37 for (int i = 0; i < sequence.length; i++) { | |
floitsch
2012/10/16 14:03:19
I would store the length in a variable, but that's
Lasse Reichstein Nielsen
2012/10/17 11:06:45
I try to avoid that when I don't know whether the
| |
38 if (sequence[i] == value) return true; | |
39 } | |
40 return false; | |
41 } | |
42 | |
43 reduce(initialValue, combine(previousValue, E element)) { | |
44 var value = initialValue; | |
45 for (int i = 0; i < this.length; i++) { | |
46 value = combine(value, this[i]); | |
47 } | |
48 return value; | |
49 } | |
50 | |
51 Collection<E> filter(bool f(E element)) { | |
52 List<E> result = <E>[]; | |
floitsch
2012/10/16 14:03:19
Ditto: violates the contract.
Lasse Reichstein Nielsen
2012/10/17 11:06:45
Arguably.
| |
53 for (int i = 0; i < this.length; i++) { | |
54 E element = this[i]; | |
55 if (f(element)) result.add(element); | |
56 } | |
57 return result; | |
58 } | |
59 | |
60 bool every(bool f(E element)) { | |
61 for (int i = 0; i < this.length; i++) { | |
62 if (!f(this[i])) return false; | |
63 } | |
64 return true; | |
65 } | |
66 | |
67 bool some(bool f(E element)) { | |
68 for (int i = 0; i < this.length; i++) { | |
69 if (f(this[i])) return true; | |
70 } | |
71 return false; | |
72 } | |
73 | |
74 bool isEmpty() { | |
75 return this.length == 0; | |
76 } | |
77 } | |
78 | |
79 | |
80 /** | |
81 * An unmodifiable [List] backed by a [Sequence]. | |
82 */ | |
83 class SequenceList<E> extends SequenceCollection<E> implements List<E> { | |
84 Sequence<E> sequence; | |
85 SequenceList(this.sequence); | |
86 | |
87 int get length => sequence.length; | |
88 | |
89 T operator[](int index) => sequence[index]; | |
90 | |
91 int indexOf(E value, [int start = 0]) { | |
92 for (int i = start; i < sequence.length; i++) { | |
93 if (sequence[i] == value) return i; | |
94 } | |
95 return -1; | |
96 } | |
97 | |
98 int lastIndexOf(E value, [int start]) { | |
99 if (start == null) start = sequence.length - 1; | |
100 for (int i = start; i >= 0; i--) { | |
101 if (sequence[i] == value) return i; | |
102 } | |
103 return -1; | |
104 } | |
105 | |
106 E last() => sequence[sequence.length - 1]; | |
107 | |
108 List<E> getRange(int start, int length) { | |
109 List<E> result = <E>[]; | |
110 for (int i = 0; i < length; i++) { | |
111 result.add(sequence[start + i]); | |
112 } | |
113 return result; | |
114 } | |
115 | |
116 void operator []=(int index, E value) { | |
117 throw new IllegalAccessException(); | |
118 } | |
119 | |
120 void set length(int newLength) { | |
121 throw new IllegalAccessException(); | |
122 } | |
123 | |
124 void add(E value) { | |
125 throw new IllegalAccessException(); | |
126 } | |
127 | |
128 void addLast(E value) { | |
129 throw new IllegalAccessException(); | |
130 } | |
131 | |
132 void addAll(Collection<E> collection) { | |
133 throw new IllegalAccessException(); | |
134 } | |
135 | |
136 void sort(Comparator<E> compare) { | |
137 throw new IllegalAccessException(); | |
ngeoffray
2012/10/16 15:08:18
Access? Should it be UnsupportedOperationException
Lasse Reichstein Nielsen
2012/10/17 11:06:45
You are right - the VM's implementation mostly use
| |
138 } | |
139 | |
140 void clear() { | |
141 throw new IllegalAccessException(); | |
142 } | |
143 | |
144 E removeAt(int index) { | |
145 throw new IllegalAccessException(); | |
146 } | |
147 | |
148 E removeLast() { | |
149 throw new IllegalAccessException(); | |
150 } | |
151 | |
152 void setRange(int start, int length, List<E> from, [int startFrom]) { | |
153 throw new IllegalAccessException(); | |
154 } | |
155 | |
156 void removeRange(int start, int length) { | |
157 throw new IllegalAccessException(); | |
158 } | |
159 | |
160 void insertRange(int start, int length, [E initialValue]) { | |
161 throw new IllegalAccessException(); | |
162 } | |
163 } | |
164 | |
165 /** | |
166 * Iterates over a [Sequence] in growing index order. | |
167 */ | |
168 class SequenceIterator<E> implements Iterator<E> { | |
169 Sequence<E> _sequence; | |
170 int _position; | |
171 SequenceIterator(this._sequence) : _position = 0; | |
172 bool hasNext() => _position < _sequence.length; | |
173 E next() { | |
174 if (hasNext()) return _sequence[_position++]; | |
175 throw new NoMoreElementsException(); | |
176 } | |
177 } | |
178 | |
OLD | NEW |