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 class _Lists { | |
6 | |
7 /** | |
8 * Returns the index in the array [a] of the given [element], starting | |
9 * the search at index [startIndex] to [endIndex] (exclusive). | |
10 * Returns -1 if [element] is not found. | |
11 */ | |
12 static int indexOf(List a, | |
13 Object element, | |
14 int startIndex, | |
15 int endIndex) { | |
16 if (startIndex >= a.length) { | |
17 return -1; | |
18 } | |
19 if (startIndex < 0) { | |
20 startIndex = 0; | |
21 } | |
22 for (int i = startIndex; i < endIndex; i++) { | |
23 if (a[i] == element) { | |
24 return i; | |
25 } | |
26 } | |
27 return -1; | |
28 } | |
29 | |
30 /** | |
31 * Returns the last index in the array [a] of the given [element], starting | |
32 * the search at index [startIndex] to 0. | |
33 * Returns -1 if [element] is not found. | |
34 */ | |
35 static int lastIndexOf(List a, Object element, int startIndex) { | |
36 if (startIndex < 0) { | |
37 return -1; | |
38 } | |
39 if (startIndex >= a.length) { | |
40 startIndex = a.length - 1; | |
41 } | |
42 for (int i = startIndex; i >= 0; i--) { | |
43 if (a[i] == element) { | |
44 return i; | |
45 } | |
46 } | |
47 return -1; | |
48 } | |
49 | |
50 /** | |
51 * Returns a sub list copy of this list, from [start] to | |
52 * [:start + length:]. | |
53 * Returns an empty list if [length] is 0. | |
54 * Throws an [IllegalArgumentException] if [length] is negative. | |
55 * Throws an [IndexOutOfRangeException] if [start] or | |
56 * [:start + length:] are out of range. | |
57 */ | |
58 static List getRange(List a, int start, int length, List accumulator) { | |
59 if (length < 0) throw new IllegalArgumentException('length'); | |
60 if (start < 0) throw new IndexOutOfRangeException(start); | |
61 int end = start + length; | |
62 if (end > a.length) throw new IndexOutOfRangeException(end); | |
63 for (int i = start; i < end; i++) { | |
64 accumulator.add(a[i]); | |
65 } | |
66 return accumulator; | |
67 } | |
68 } | |
OLD | NEW |