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