Implement List.getRange.

Review URL: http://codereview.chromium.org//8276005

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@403 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
ngeoffray@google.com 2011-10-13 16:24:09 +00:00
parent 9e03f7c985
commit 85804080f3
6 changed files with 81 additions and 4 deletions

View file

@ -189,7 +189,9 @@ class ObjectArray<T> implements Array<T> native "Array" {
}
List<T> getRange(int start, int length) {
throw const NotImplementedException();
if (length == 0) return [];
Arrays.rangeCheck(this, start, length);
return new List<T>.fromList(this, start, start + length);
}
int indexOf(T element, int startIndex) {

View file

@ -63,4 +63,16 @@ class Arrays {
}
return -1;
}
static void rangeCheck(Array a, int start, int length) {
if (length < 0) {
throw new IllegalArgumentException("negative length $length");
}
if (start < 0 || start > a.length) {
throw new IndexOutOfRangeException(start);
}
if (start + length > a.length) {
throw new IndexOutOfRangeException(start + length);
}
}
}

View file

@ -109,7 +109,9 @@ class ObjectArray<T> implements Array<T> {
}
List<T> getRange(int start, int length) {
throw const NotImplementedException();
if (length == 0) return [];
Arrays.rangeCheck(this, start, length);
return new List<T>.fromList(this, start, start + length);
}
/**
@ -225,7 +227,9 @@ class ImmutableArray<T> implements Array<T> {
}
List<T> getRange(int start, int length) {
throw const NotImplementedException();
if (length == 0) return [];
Arrays.rangeCheck(this, start, length);
return new List<T>.fromList(this, start, start + length);
}
/**

View file

@ -86,5 +86,17 @@ class Arrays {
}
return -1;
}
static void rangeCheck(Array a, int start, int length) {
if (length < 0) {
throw new IllegalArgumentException("negative length $length");
}
if (start < 0 || start > a.length) {
throw new IndexOutOfRangeException(start);
}
if (start + length > a.length) {
throw new IndexOutOfRangeException(start + length);
}
}
}

View file

@ -42,7 +42,9 @@ class GrowableObjectArray<T> implements Array<T> {
}
List<T> getRange(int start, int length) {
throw const NotImplementedException();
if (length == 0) return [];
Arrays.rangeCheck(this, start, length);
return new List<T>.fromList(this, start, start + length);
}
// The length of this growable array. It is always less than the

View file

@ -0,0 +1,45 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
main() {
Expect.listEquals([], [].getRange(0, 0));
Expect.listEquals([], const [].getRange(0, 0));
Expect.listEquals([], [].getRange(-1, 0));
Expect.listEquals([], const [].getRange(-1, 0));
Expect.listEquals([1, 2], [1, 2].getRange(0, 2));
Expect.listEquals([1, 2], const [1, 2].getRange(0, 2));
Expect.listEquals([1], [1, 2].getRange(0, 1));
Expect.listEquals([1], const [1, 2].getRange(0, 1));
Expect.listEquals([2], [1, 2].getRange(1, 1));
Expect.listEquals([2], const [1, 2].getRange(1, 1));
Expect.listEquals([], [1, 2].getRange(0, 0));
Expect.listEquals([], const [1, 2].getRange(0, 0));
Expect.listEquals([2, 3], [1, 2, 3, 4].getRange(1, 2));
Expect.listEquals([2, 3], const [1, 2, 3, 4].getRange(1, 2));
Expect.listEquals([2, 3], [1, 2, 3, 4].getRange(1, 2));
Expect.listEquals([2, 3], const [1, 2, 3, 4].getRange(1, 2));
expectIAE(() => [].getRange(0, -1));
expectIAE(() => const [].getRange(-1, -1));
expectIOORE(() => [].getRange(-1, 1));
expectIOORE(() => [].getRange(1, 1));
expectIOORE(() => [1].getRange(0, 2));
expectIOORE(() => [1].getRange(1, 1));
}
void expectIOORE(Function f) {
Expect.throws(f, (e) => e is IndexOutOfRangeException);
}
void expectIAE(Function f) {
Expect.throws(f, (e) => e is IllegalArgumentException);
}