| 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 // TODO(ngeoffray): Rename to Lists. |
| 5 class Arrays { | 6 class Arrays { |
| 6 | 7 |
| 7 static String asString(Array array) { | 8 static String asString(List list) { |
| 8 String result = "["; | 9 String result = "["; |
| 9 int len = array.length; | 10 int len = list.length; |
| 10 for (int i = 0; i < len; i++) { | 11 for (int i = 0; i < len; i++) { |
| 11 // TODO(4466785): Deal with recursion and formatting. | 12 // TODO(4466785): Deal with recursion and formatting. |
| 12 result += array[i].toString() + ", "; | 13 result += list[i].toString() + ", "; |
| 13 } | 14 } |
| 14 result += "]"; | 15 result += "]"; |
| 15 return result; | 16 return result; |
| 16 } | 17 } |
| 17 | 18 |
| 18 static void copy(Array src, int srcStart, | 19 static void copy(List src, int srcStart, |
| 19 Array dst, int dstStart, int count) { | 20 List dst, int dstStart, int count) { |
| 20 if (srcStart === null) srcStart = 0; | 21 if (srcStart === null) srcStart = 0; |
| 21 if (dstStart === null) dstStart = 0; | 22 if (dstStart === null) dstStart = 0; |
| 22 | 23 |
| 23 if (srcStart < dstStart) { | 24 if (srcStart < dstStart) { |
| 24 for (int i = srcStart + count - 1, j = dstStart + count - 1; | 25 for (int i = srcStart + count - 1, j = dstStart + count - 1; |
| 25 i >= srcStart; i--, j--) { | 26 i >= srcStart; i--, j--) { |
| 26 dst[j] = src[i]; | 27 dst[j] = src[i]; |
| 27 } | 28 } |
| 28 } else { | 29 } else { |
| 29 for (int i = srcStart, j = dstStart; i < srcStart + count; i++, j++) { | 30 for (int i = srcStart, j = dstStart; i < srcStart + count; i++, j++) { |
| 30 dst[j] = src[i]; | 31 dst[j] = src[i]; |
| 31 } | 32 } |
| 32 } | 33 } |
| 33 } | 34 } |
| 34 | 35 |
| 35 static bool areEqual(Array a, Object b) { | 36 static bool areEqual(List a, Object b) { |
| 36 if (a === b) return true; | 37 if (a === b) return true; |
| 37 if (!(b is Array)) return false; | 38 if (!(b is List)) return false; |
| 38 int length = a.length; | 39 int length = a.length; |
| 39 if (length != b.length) return false; | 40 if (length != b.length) return false; |
| 40 | 41 |
| 41 for (int i = 0; i < length; i++) { | 42 for (int i = 0; i < length; i++) { |
| 42 if (a[i] !== b[i]) return false; | 43 if (a[i] !== b[i]) return false; |
| 43 } | 44 } |
| 44 return true; | 45 return true; |
| 45 } | 46 } |
| 46 | 47 |
| 47 /** | 48 /** |
| 48 * Returns the index in the array [a] of the given [element], starting | 49 * Returns the index in the list [a] of the given [element], starting |
| 49 * the search at index [startIndex] to [endIndex] (exclusive). | 50 * the search at index [startIndex] to [endIndex] (exclusive). |
| 50 * Returns -1 if [element] is not found. | 51 * Returns -1 if [element] is not found. |
| 51 */ | 52 */ |
| 52 static int indexOf(Array a, | 53 static int indexOf(List a, |
| 53 Object element, | 54 Object element, |
| 54 int startIndex, | 55 int startIndex, |
| 55 int endIndex) { | 56 int endIndex) { |
| 56 if (startIndex >= a.length) { | 57 if (startIndex >= a.length) { |
| 57 return -1; | 58 return -1; |
| 58 } | 59 } |
| 59 if (startIndex < 0) { | 60 if (startIndex < 0) { |
| 60 startIndex = 0; | 61 startIndex = 0; |
| 61 } | 62 } |
| 62 for (int i = startIndex; i < endIndex; i++) { | 63 for (int i = startIndex; i < endIndex; i++) { |
| 63 if (a[i] == element) { | 64 if (a[i] == element) { |
| 64 return i; | 65 return i; |
| 65 } | 66 } |
| 66 } | 67 } |
| 67 return -1; | 68 return -1; |
| 68 } | 69 } |
| 69 | 70 |
| 70 /** | 71 /** |
| 71 * Returns the last index in the array [a] of the given [element], starting | 72 * Returns the last index in the list [a] of the given [element], starting |
| 72 * the search at index [startIndex] to 0. | 73 * the search at index [startIndex] to 0. |
| 73 * Returns -1 if [element] is not found. | 74 * Returns -1 if [element] is not found. |
| 74 */ | 75 */ |
| 75 static int lastIndexOf(Array a, Object element, int startIndex) { | 76 static int lastIndexOf(List a, Object element, int startIndex) { |
| 76 if (startIndex < 0) { | 77 if (startIndex < 0) { |
| 77 return -1; | 78 return -1; |
| 78 } | 79 } |
| 79 if (startIndex >= a.length) { | 80 if (startIndex >= a.length) { |
| 80 startIndex = a.length - 1; | 81 startIndex = a.length - 1; |
| 81 } | 82 } |
| 82 for (int i = startIndex; i >= 0; i--) { | 83 for (int i = startIndex; i >= 0; i--) { |
| 83 if (a[i] == element) { | 84 if (a[i] == element) { |
| 84 return i; | 85 return i; |
| 85 } | 86 } |
| 86 } | 87 } |
| 87 return -1; | 88 return -1; |
| 88 } | 89 } |
| 89 | 90 |
| 90 static void rangeCheck(Array a, int start, int length) { | 91 static void rangeCheck(List a, int start, int length) { |
| 91 if (length < 0) { | 92 if (length < 0) { |
| 92 throw new IllegalArgumentException("negative length $length"); | 93 throw new IllegalArgumentException("negative length $length"); |
| 93 } | 94 } |
| 94 if (start < 0 || start >= a.length) { | 95 if (start < 0 || start >= a.length) { |
| 95 throw new IndexOutOfRangeException(start); | 96 throw new IndexOutOfRangeException(start); |
| 96 } | 97 } |
| 97 if (start + length > a.length) { | 98 if (start + length > a.length) { |
| 98 throw new IndexOutOfRangeException(start + length); | 99 throw new IndexOutOfRangeException(start + length); |
| 99 } | 100 } |
| 100 } | 101 } |
| 101 } | 102 } |
| 102 | 103 |
| OLD | NEW |