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 // TODO(jacobr): move into a core library or at least merge with the copy |
| 6 // in client/dom/src |
| 7 class Lists { |
| 8 |
| 9 /** |
| 10 * Returns the index in the array [a] of the given [element], starting |
| 11 * the search at index [startIndex] to [endIndex] (exclusive). |
| 12 * Returns -1 if [element] is not found. |
| 13 */ |
| 14 static int indexOf(List a, |
| 15 Object element, |
| 16 int startIndex, |
| 17 int endIndex) { |
| 18 if (startIndex >= a.length) { |
| 19 return -1; |
| 20 } |
| 21 if (startIndex < 0) { |
| 22 startIndex = 0; |
| 23 } |
| 24 for (int i = startIndex; i < endIndex; i++) { |
| 25 if (a[i] == element) { |
| 26 return i; |
| 27 } |
| 28 } |
| 29 return -1; |
| 30 } |
| 31 |
| 32 /** |
| 33 * Returns the last index in the array [a] of the given [element], starting |
| 34 * the search at index [startIndex] to 0. |
| 35 * Returns -1 if [element] is not found. |
| 36 */ |
| 37 static int lastIndexOf(List a, Object element, int startIndex) { |
| 38 if (startIndex < 0) { |
| 39 return -1; |
| 40 } |
| 41 if (startIndex >= a.length) { |
| 42 startIndex = a.length - 1; |
| 43 } |
| 44 for (int i = startIndex; i >= 0; i--) { |
| 45 if (a[i] == element) { |
| 46 return i; |
| 47 } |
| 48 } |
| 49 return -1; |
| 50 } |
| 51 |
| 52 static void setRange(List to, int start, int length, List from, |
| 53 int startFrom) { |
| 54 // TODO(nweiz): remove these IndexOutOfRange checks once Frog has bounds |
| 55 // checking for List indexing |
| 56 if (start < 0) { |
| 57 throw new IndexOutOfRangeException(start); |
| 58 } else if (startFrom < 0) { |
| 59 throw new IndexOutOfRangeException(startFrom); |
| 60 } else if (length < 0) { |
| 61 throw new IllegalArgumentException("negative length $length"); |
| 62 } else if (start + length > to.length) { |
| 63 throw new IndexOutOfRangeException(Math.min(to.length, start)); |
| 64 } else if (startFrom + length > from.length) { |
| 65 throw new IndexOutOfRangeException(Math.min(from.length, startFrom)); |
| 66 } |
| 67 |
| 68 for (var i = 0; i < length; i++) { |
| 69 to[start + i] = from[startFrom + i]; |
| 70 } |
| 71 } |
| 72 |
| 73 static void removeRange(List a, int start, int length, |
| 74 void removeOne(int index)) { |
| 75 // TODO(nweiz): remove these IndexOutOfRange checks once Frog has bounds |
| 76 // checking for List indexing |
| 77 if (start < 0) { |
| 78 throw new IndexOutOfRangeException(start); |
| 79 } else if (length < 0) { |
| 80 throw new IllegalArgumentException("negative length $length"); |
| 81 } else if (start + length > a.length) { |
| 82 throw new IndexOutOfRangeException(Math.min(a.length, start)); |
| 83 } |
| 84 |
| 85 for (var i = 0; i < length; i++) { |
| 86 removeOne(start); |
| 87 } |
| 88 } |
| 89 |
| 90 static List getRange(List a, int start, int length) { |
| 91 // TODO(nweiz): remove these IndexOutOfRange checks once Frog has bounds |
| 92 // checking for List indexing |
| 93 if (start < 0) { |
| 94 throw new IndexOutOfRangeException(start); |
| 95 } else if (length < 0) { |
| 96 throw new IllegalArgumentException("negative length $length"); |
| 97 } else if (start + length > a.length) { |
| 98 throw new IndexOutOfRangeException(Math.min(a.length, start)); |
| 99 } |
| 100 |
| 101 var result = []; |
| 102 for (var i = 0; i < length; i++) { |
| 103 result.add(a[start + i]); |
| 104 } |
| 105 return result; |
| 106 } |
| 107 } |
OLD | NEW |