Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: sdk/lib/core/list.dart

Issue 23908003: Added examples to list.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changes based on Mem's comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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 dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * An indexable collection of objects with a length. 8 * An indexable collection of objects with a length.
9 * 9 *
10 * Subclasses of this class implement different kinds of lists. 10 * Subclasses of this class implement different kinds of lists.
11 * The most common kinds of lists are: 11 * The most common kinds of lists are:
12 * 12 *
13 * * Fixed-length list. 13 * * Fixed-length list.
14 * An error occurs when attempting to use operations 14 * An error occurs when attempting to use operations
15 * that can change the length of the list. 15 * that can change the length of the list.
16 * 16 *
17 * * Growable list. Full implementation of the API defined in this class. 17 * * Growable list. Full implementation of the API defined in this class.
18 * 18 *
19 * The following code illustrates that some List implementations support 19 * The following code illustrates that some List implementations support
20 * only a subset of the API. 20 * only a subset of the API.
21 * 21 *
22 * var fixedLengthList = new List(5); 22 * List<int> fixedLengthList = new List(5);
23 * fixedLengthList.length = 0; // Error. 23 * fixedLengthList.length = 0; // Error
24 * fixedLengthList.add(499); // Error. 24 * fixedLengthList.add(499); // Error
25 * fixedLengthList[0] = 87; 25 * fixedLengthList[0] = 87;
26 * 26 end
floitsch 2013/09/23 13:41:23 spurious line.
27 * var growableList = [1, 2]; 27 * List<int> growableList = [1, 2];
28 * growableList.length = 0; 28 * growableList.length = 0;
29 * growableList.add(499); 29 * growableList.add(499);
30 * growableList[0] = 87; 30 * growableList[0] = 87;
31 * 31 *
32 * Lists are [Iterable]. 32 * Lists are [Iterable]. Iteration occurs over values in index order. Changing
33 * Iteration occurs over values in index order. 33 * the values does not affect iteration, but changing the valid
34 * Changing the values does not affect iteration, 34 * indices&mdash;that is, changing the list's length&mdash;between iteration
floitsch 2013/09/23 13:41:23 &mdash; is not valid markdown (afaik). (twice on t
35 * but changing the valid indices&mdash;that is, 35 * steps causes a [ConcurrentModificationError]. This means that only growable
36 * changing the list's length&mdash;between 36 * lists can throw ConcurrentModificationError. If the length changes
floitsch 2013/09/23 13:41:23 [ConcurrentModificationError] or `ConcurrentModifi
Kathy Walrath 2013/09/25 18:26:25 Why not just ConcurrentModificationError or Concur
floitsch 2013/09/27 09:13:38 But it also means that renaming the class will aut
37 * iteration steps 37 * temporarily and is restored before continuing the iteration, the iterator
38 * causes a [ConcurrentModificationError]. 38 * does not detect it.
39 * This means that only growable lists can throw ConcurrentModificationError.
40 * If the length changes temporarily
41 * and is restored before continuing the iteration,
42 * the iterator does not detect it.
43 */ 39 */
44 abstract class List<E> implements Iterable<E> { 40 abstract class List<E> implements Iterable<E> {
45 /** 41 /**
46 * Creates a list of the given _length_. 42 * Creates a list of the given _length_.
floitsch 2013/09/23 13:41:23 [length]
Kathy Walrath 2013/09/25 18:26:25 How about just "of the given length." (I think we
floitsch 2013/09/27 09:13:38 fine.
47 * 43 *
48 * The created list is fixed-length if _length_ is provided. 44 * The created list is fixed-length if _length_ is provided.
floitsch 2013/09/23 13:41:23 [length]
45 *
46 * List fixedLengthList = new List(3);
47 * fixedLengthList.length; // 3
48 fixedLengthList.length = 1; // Error
49 *
50 *
49 * The list has length 0 and is growable if _length_ is omitted. 51 * The list has length 0 and is growable if _length_ is omitted.
floitsch 2013/09/23 13:41:23 [length]
50 * 52 *
53 * List growableList = new List();
54 * growableList.length; // 0;
55 * growableList.length = 3;
56 *
51 * An error occurs if _length_ is negative. 57 * An error occurs if _length_ is negative.
floitsch 2013/09/23 13:41:23 I would prefer: The argument [length] must not be
52 */ 58 */
53 external factory List([int length]); 59 external factory List([int length]);
54 60
55 /** 61 /**
56 * Creates a fixed-length list of the given _length_ 62 * Creates a fixed-length list of the given _length_
floitsch 2013/09/23 13:41:23 [length]
57 * and initializes the value at each position with [fill]. 63 * and initializes the value at each position with [fill]:
64 *
65 * new List<int>.filled(3, 0); // [0, 0, 0]
58 */ 66 */
59 external factory List.filled(int length, E fill); 67 external factory List.filled(int length, E fill);
60 68
61 /** 69 /**
62 * Creates a list and initializes it using the contents of [other]. 70 * Creates a list and initializes it using the contents of [other].
63 * 71 *
64 * The [Iterator] of [other] provides the order of the objects. 72 * The [Iterator] of [other] provides the order of the objects.
65 * 73 *
66 * This constructor returns a growable list if [growable] is true; 74 * This constructor returns a growable list if [growable] is true;
67 * otherwise, it returns a fixed-length list. 75 * otherwise, it returns a fixed-length list.
(...skipping 13 matching lines...) Expand all
81 } 89 }
82 90
83 /** 91 /**
84 * Generates a list of values. 92 * Generates a list of values.
85 * 93 *
86 * Creates a list with _length_ positions 94 * Creates a list with _length_ positions
87 * and fills it with values created by calling [generator] 95 * and fills it with values created by calling [generator]
88 * for each index in the range `0` .. `length - 1` 96 * for each index in the range `0` .. `length - 1`
89 * in increasing order. 97 * in increasing order.
90 * 98 *
99 * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4]
100 *
91 * The created list is fixed-length unless [growable] is true. 101 * The created list is fixed-length unless [growable] is true.
92 */ 102 */
93 factory List.generate(int length, E generator(int index), 103 factory List.generate(int length, E generator(int index),
94 { bool growable: true }) { 104 { bool growable: true }) {
95 List<E> result; 105 List<E> result;
96 if (growable) { 106 if (growable) {
97 result = <E>[]..length = length; 107 result = <E>[]..length = length;
98 } else { 108 } else {
99 result = new List<E>(length); 109 result = new List<E>(length);
100 } 110 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 161
152 /** 162 /**
153 * Returns an [Iterable] of the objects in this list in reverse order. 163 * Returns an [Iterable] of the objects in this list in reverse order.
154 */ 164 */
155 Iterable<E> get reversed; 165 Iterable<E> get reversed;
156 166
157 /** 167 /**
158 * Sorts this list according to the order specified by the [compare] function. 168 * Sorts this list according to the order specified by the [compare] function.
159 * 169 *
160 * The [compare] function must act as a [Comparator]. 170 * The [compare] function must act as a [Comparator].
171
172 * List<String> numbers = ['one', 'two', 'three', 'four'];
173 * // Sort from shortest to longest.
174 * numbers.sort((x, y) => x.length.compareTo(y.length));
floitsch 2013/09/23 13:41:23 you could also show that short-cutting is ok: numb
175 * numbers.join(', '); // 'one, two, four, three'
161 * 176 *
162 * The default List implementations use [Comparable.compare] if 177 * The default List implementations use [Comparable.compare] if
163 * [compare] is omitted. 178 * [compare] is omitted.
179 *
180 * List<int> nums = [13, 2, -11];
181 * nums.sort();
182 nums.join(', '); // '-11, 2, 13'
164 */ 183 */
floitsch 2013/09/23 13:41:23 Since you add an example that sorts strings, pleas
165 void sort([int compare(E a, E b)]); 184 void sort([int compare(E a, E b)]);
166 185
167 /** 186 /**
168 * Returns the first index of [element] in this list. 187 * Returns the first index of [element] in this list.
169 * 188 *
170 * Searches the list from index [start] to the length of the list. 189 * Searches the list from index [start] to the end of the list.
171 * The first time an object [:o:] is encountered so that [:o == element:], 190 * The first time an object [:o:] is encountered so that [:o == element:],
172 * the index of [:o:] is returned. 191 * the index of [:o:] is returned.
192 *
193 * List<String> notes = ['do', 're', 'mi', 're'];
194 * notes.indexOf('re'); // 1
195 * notes.indexOf('re', 2); // 3
196 *
173 * Returns -1 if [element] is not found. 197 * Returns -1 if [element] is not found.
198 *
199 * notes.indexOf('fa'); // -1
floitsch 2013/09/23 13:41:23 either align with above, or put it closer to the s
174 */ 200 */
175 int indexOf(E element, [int start = 0]); 201 int indexOf(E element, [int start = 0]);
176 202
177 /** 203 /**
178 * Returns the last index of [element] in this list. 204 * Returns the last index of [element] in this list.
179 * 205 *
180 * Searches the list backwards from index [start] to 0. 206 * Searches the list backwards from index [start] to 0.
181 * 207 *
182 * The first time an object [:o:] is encountered so that [:o == element:], 208 * The first time an object [:o:] is encountered so that [:o == element:],
183 * the index of [:o:] is returned. 209 * the index of [:o:] is returned.
184 * 210 *
185 * If [start] is not provided, it defaults to [:this.length - 1:]. 211 * List<String> notes = ['do', 're', 'mi', 're'];
212 * notes.lastIndexOf('re', 2); // 1
213 *
214 * If [start] is not provided, this method searches from the end of the
215 * list./Returns
floitsch 2013/09/23 13:41:23 Spurious text.
216 *
217 * notes.lastIndexOf('re'); // 3
186 * 218 *
187 * Returns -1 if [element] is not found. 219 * Returns -1 if [element] is not found.
220 *
221 * notes.lastIndexOf('fa'); // -1
188 */ 222 */
189 int lastIndexOf(E element, [int start]); 223 int lastIndexOf(E element, [int start]);
190 224
191 /** 225 /**
192 * Removes all objects from this list; 226 * Removes all objects from this list;
193 * the length of the list becomes zero. 227 * the length of the list becomes zero.
194 * 228 *
195 * Throws an [UnsupportedError], and retains all objects, if this 229 * Throws an [UnsupportedError], and retains all objects, if this
196 * is a fixed-length list. 230 * is a fixed-length list.
197 */ 231 */
(...skipping 18 matching lines...) Expand all
216 * 250 *
217 * An error occurs if the [index] is less than 0 or greater than length. 251 * An error occurs if the [index] is less than 0 or greater than length.
218 * An [UnsupportedError] occurs if the list is fixed-length. 252 * An [UnsupportedError] occurs if the list is fixed-length.
219 */ 253 */
220 void insertAll(int index, Iterable<E> iterable); 254 void insertAll(int index, Iterable<E> iterable);
221 255
222 /** 256 /**
223 * Overwrites objects of `this` with the objects of [iterable], starting 257 * Overwrites objects of `this` with the objects of [iterable], starting
224 * at position [index] in this list. 258 * at position [index] in this list.
225 * 259 *
260 * List<String> list = ['a', 'b', 'c'];
261 * list.setAll(1, ['bee', 'sea']);
262 * list.join(', '); // 'a, bee, sea'
263 *
226 * This operation does not increase the length of `this`. 264 * This operation does not increase the length of `this`.
227 * 265 *
228 * An error occurs if the [index] is less than 0 or greater than length. 266 * An error occurs if the [index] is less than 0 or greater than length.
229 * An error occurs if the [iterable] is longer than [length] - [index]. 267 * An error occurs if the [iterable] is longer than [length] - [index].
230 */ 268 */
231 void setAll(int index, Iterable<E> iterable); 269 void setAll(int index, Iterable<E> iterable);
232 270
233 /** 271 /**
234 * Removes the first occurence of [value] from this list. 272 * Removes the first occurence of [value] from this list.
235 * 273 *
236 * Returns true if [value] was in the list. 274 * Returns true if [value] was in the list, false otherwise.
237 * Returns false otherwise. 275 *
276 * List<String> parts = ['head', 'shoulders', 'knees', 'toes'];
277 * parts.remove('head'); // true
278 * parts.join(', '); // 'shoulders, knees, toes'
238 * 279 *
239 * The method has no effect if [value] was not in the list. 280 * The method has no effect if [value] was not in the list.
240 * 281 *
282 * // Note: 'head' has already been removed.
283 * parts.remove('head'); // false
284 * parts.join(', '); // 'shoulders, knees, toes'
285 *
241 * An [UnsupportedError] occurs if the list is fixed-length. 286 * An [UnsupportedError] occurs if the list is fixed-length.
floitsch 2013/09/23 13:41:23 Below we write "Throws an [UnsupportedError] if th
242 */ 287 */
243 bool remove(Object value); 288 bool remove(Object value);
244 289
245 /** 290 /**
246 * Removes the object at position [index] from this list. 291 * Removes the object at position [index] from this list.
247 * 292 *
248 * This method reduces the length of `this` by one and moves all later objects 293 * This method reduces the length of `this` by one and moves all later objects
249 * down by one position. 294 * down by one position.
250 * 295 *
251 * Returns the removed object. 296 * Returns the removed object.
(...skipping 10 matching lines...) Expand all
262 * 307 *
263 * Throws an [UnsupportedError] if this is a fixed-length list. 308 * Throws an [UnsupportedError] if this is a fixed-length list.
264 */ 309 */
265 E removeLast(); 310 E removeLast();
266 311
267 /** 312 /**
268 * Removes all objects from this list that satisfy [test]. 313 * Removes all objects from this list that satisfy [test].
269 * 314 *
270 * An object [:o:] satisfies [test] if [:test(o):] is true. 315 * An object [:o:] satisfies [test] if [:test(o):] is true.
271 * 316 *
317 * List<String> numbers = ['one', 'two', 'three', 'four'];
318 * numbers.removeWhere((item) => item.length == 3);
319 * numbers.join(', '); // 'three, four'
320 *
272 * Throws an [UnsupportedError] if this is a fixed-length list. 321 * Throws an [UnsupportedError] if this is a fixed-length list.
273 */ 322 */
274 void removeWhere(bool test(E element)); 323 void removeWhere(bool test(E element));
275 324
276 /** 325 /**
277 * Removes all objects from this list that fail to satisfy [test]. 326 * Removes all objects from this list that fail to satisfy [test].
278 * 327 *
279 * An object [:o:] satisfies [test] if [:test(o):] is true. 328 * An object [:o:] satisfies [test] if [:test(o):] is true.
280 * 329 *
330 * List<String> numbers = ['one', 'two', 'three', 'four'];
331 * numbers.retainWhere((item) => item.length == 3);
332 * numbers.join(', '); // 'one, two'
333 *
281 * Throws an [UnsupportedError] if this is a fixed-length list. 334 * Throws an [UnsupportedError] if this is a fixed-length list.
282 */ 335 */
283 void retainWhere(bool test(E element)); 336 void retainWhere(bool test(E element));
284 337
285 /** 338 /**
286 * Returns a new list containing the objects 339 * Returns a new list containing the objects from [start] inclusive to [end]
287 * from [start] inclusive to [end] exclusive. 340 * exclusive.
341 *
342 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
343 * colors.sublist(1, 3); // ['green', 'blue']
288 * 344 *
289 * If [end] is omitted, the [length] of `this` is used. 345 * If [end] is omitted, the [length] of `this` is used.
290 * 346 *
347 * colors.sublist(1); // ['green', 'blue', 'orange', 'pink']
348 *
291 * An error occurs if [start] is outside the range `0` .. `length` or if 349 * An error occurs if [start] is outside the range `0` .. `length` or if
292 * [end] is outside the range `start` .. `length`. 350 * [end] is outside the range `start` .. `length`.
293 */ 351 */
294 List<E> sublist(int start, [int end]); 352 List<E> sublist(int start, [int end]);
295 353
296 /** 354 /**
297 * Returns an [Iterable] that iterates over the objects in the range 355 * Returns an [Iterable] that iterates over the objects in the range
298 * [start] inclusive to [end] exclusive. 356 * [start] inclusive to [end] exclusive.
299 * 357 *
300 * An error occurs if [end] is before [start]. 358 * An error occurs if [end] is before [start].
301 * 359 *
302 * An error occurs if the [start] and [end] are not valid ranges at the time 360 * An error occurs if the [start] and [end] are not valid ranges at the time
303 * of the call to this method. The returned [Iterable] behaves like 361 * of the call to this method. The returned [Iterable] behaves like
304 * `skip(start).take(end - start)`. That is, it does not throw exceptions 362 * `skip(start).take(end - start)`. That is, it does not throw exceptions
305 * if `this` changes size. 363 * if `this` changes size.
306 * 364 *
307 * Example: 365 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
308 * 366 * Iterable<String> range = colors.getRange(1, 4);
309 * var list = [1, 2, 3, 4, 5]; 367 * range.join(', '); // 'green, blue, orange'
310 * var range = list.getRange(1, 4); 368 * colors.length = 3;
311 * print(range.join(', ')); // => 2, 3, 4 369 * range.join(', '); // 'green, blue'
312 * list.length = 3;
313 * print(range.join(', ')); // => 2, 3
314 */ 370 */
315 Iterable<E> getRange(int start, int end); 371 Iterable<E> getRange(int start, int end);
316 372
317 /** 373 /**
318 * Copies the objects of [iterable], skipping [skipCount] objects first, 374 * Copies the objects of [iterable], skipping [skipCount] objects first,
319 * into the range [start] inclusive to [end] exclusive of `this`. 375 * into the range [start] inclusive to [end] exclusive of `this`.
320 * 376 *
377 * List<int> list1 = [1, 2, 3, 4];
378 * List<int> list2 = [5, 6, 7, 8, 9];
379 * // Copies the 4th and 5th items in list2 as the 2nd and 3rd items
380 * // of list1.
381 * list1.setRange(1, 3, list2, 3);
382 * list1.join(', '); // '1, 8, 9, 4'
383 *
321 * If [start] equals [end] and [start]..[end] represents a legal range, this 384 * If [start] equals [end] and [start]..[end] represents a legal range, this
322 * method has no effect. 385 * method has no effect.
323 * 386 *
324 * An error occurs if [start]..[end] is not a valid range for `this`. 387 * An error occurs if [start]..[end] is not a valid range for `this`.
325 * An error occurs if the [iterable] does not have enough objects after 388 * An error occurs if the [iterable] does not have enough objects after
326 * skipping [skipCount] objects. 389 * skipping [skipCount] objects.
327 * 390 *
328 * Example:
329 *
330 * var list = [1, 2, 3, 4];
331 * var list2 = [5, 6, 7, 8, 9];
332 * list.setRange(1, 3, list2, 3);
333 * print(list); // => [1, 8, 9, 4]
334 */ 391 */
335 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]); 392 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]);
336 393
337 /** 394 /**
338 * Removes the objects in the range [start] inclusive to [end] exclusive. 395 * Removes the objects in the range [start] inclusive to [end] exclusive.
339 * 396 *
340 * An error occurs if [start]..[end] is not a valid range for `this`. 397 * An error occurs if [start]..[end] is not a valid range for `this`.
341 * Throws an [UnsupportedError] if this is a fixed-length list. 398 * Throws an [UnsupportedError] if this is a fixed-length list.
342 */ 399 */
343 void removeRange(int start, int end); 400 void removeRange(int start, int end);
344 401
345 /** 402 /**
346 * Sets the objects in the range [start] inclusive to [end] exclusive 403 * Sets the objects in the range [start] inclusive to [end] exclusive
347 * to the given [fillValue]. 404 * to the given [fillValue].
348 * 405 *
349 * An error occurs if [start]..[end] is not a valid range for `this`. 406 * An error occurs if [start]..[end] is not a valid range for `this`.
350 */ 407 */
351 void fillRange(int start, int end, [E fillValue]); 408 void fillRange(int start, int end, [E fillValue]);
352 409
353 /** 410 /**
354 * Removes the objects in the range [start] inclusive to [end] exclusive 411 * Removes the objects in the range [start] inclusive to [end] exclusive
355 * and replaces them with the contents of the [iterable]. 412 * and replaces them with the contents of the [iterable].
356 * 413 *
414 * List<int> list = [1, 2, 3, 4];
415 * list.replaceRange(1, 3, [6, 7]);
416 * list.join(', '); // '1, 6, 7, 4'
417 *
357 * An error occurs if [start]..[end] is not a valid range for `this`. 418 * An error occurs if [start]..[end] is not a valid range for `this`.
358 *
359 * Example:
360 *
361 * var list = [1, 2, 3, 4, 5];
362 * list.replaceRange(1, 3, [6, 7, 8, 9]);
363 * print(list); // [1, 6, 7, 8, 9, 4, 5]
364 */ 419 */
365 void replaceRange(int start, int end, Iterable<E> iterable); 420 void replaceRange(int start, int end, Iterable<E> iterable);
366 421
367 /** 422 /**
368 * Returns an unmodifiable [Map] view of `this`. 423 * Returns an unmodifiable [Map] view of `this`.
369 * 424 *
370 * The map uses the indices of this list as keys and the corresponding objects 425 * The map uses the indices of this list as keys and the corresponding objects
371 * as values. The `Map.keys` [Iterable] iterates the indices of this list 426 * as values. The `Map.keys` [Iterable] iterates the indices of this list
372 * in numerical order. 427 * in numerical order.
428 *
429 * List<String> words = ['fee', 'fi', 'fo', 'fum'];
430 * Map<int, String> map = words.asMap();
431 * map[0] + map[1]; // 'feefi';
432 * map.keys.toList(); // [0, 1, 2, 3]
373 */ 433 */
374 Map<int, E> asMap(); 434 Map<int, E> asMap();
375 } 435 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698