Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1336)

Side by Side Diff: runtime/lib/byte_array.dart

Issue 10391056: - Typed arrays should implement all methods from Collection and List interfaces. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 interface ByteArray { 5 interface ByteArray {
6 int lengthInBytes(); 6 int lengthInBytes();
7 7
8 ByteArray subByteArray([int start, int length]); 8 ByteArray subByteArray([int start, int length]);
9 9
10 int getInt8(int byteOffset); 10 int getInt8(int byteOffset);
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 return new _Float64Array(length); 233 return new _Float64Array(length);
234 } 234 }
235 235
236 factory Float64List.view(ByteArray array, [int start, int length]) { 236 factory Float64List.view(ByteArray array, [int start, int length]) {
237 return new _Float64ArrayView(array, start, length); 237 return new _Float64ArrayView(array, start, length);
238 } 238 }
239 } 239 }
240 240
241 241
242 abstract class _ByteArrayBase { 242 abstract class _ByteArrayBase {
243
244 // Methods implementing the Collection interface.
245
246 void forEach(void f(element)) {
247 var len = this.length;
248 for (var i = 0; i < len; i++) {
249 f(this[i]);
250 }
251 }
252
253 Collection map(f(element)) {
254 return Collections.map(this,
255 new GrowableObjectArray.withCapacity(length),
Anders Johnsen 2012/05/11 08:38:54 This could be end up slowing down some users code.
256 f);
257 }
258
259 Collection filter(bool f(element)) {
260 return Collections.filter(this, new GrowableObjectArray(), f);
Anders Johnsen 2012/05/11 08:38:54 Ditto.
261 }
262
263 bool every(bool f(element)) {
264 return Collections.every(this, f);
265 }
266
267 bool some(bool f(element)) {
268 return Collections.some(this, f);;
269 }
270
271 bool isEmpty() {
272 return this.length === 0;
273 }
274
275 int get length() {
276 return _length();
277 }
278
279 // Methods implementing the List interface.
280
281 set length(newLength) {
282 throw const UnsupportedOperationException(
283 "Cannot resize a non-extendable array");
284 }
285
243 void add(value) { 286 void add(value) {
244 throw const UnsupportedOperationException( 287 throw const UnsupportedOperationException(
245 "Cannot add to a non-extendable array"); 288 "Cannot add to a non-extendable array");
246 } 289 }
247 290
248 void addLast(value) { 291 void addLast(value) {
249 throw const UnsupportedOperationException( 292 throw const UnsupportedOperationException(
250 "Cannot add to a non-extendable array"); 293 "Cannot add to a non-extendable array");
251 } 294 }
252 295
253 void addAll(Collection value) { 296 void addAll(Collection value) {
254 throw const UnsupportedOperationException( 297 throw const UnsupportedOperationException(
255 "Cannot add to a non-extendable array"); 298 "Cannot add to a non-extendable array");
256 } 299 }
257 300
301 void sort(int compare(a, b)) {
302 DualPivotQuicksort.sort(this, compare);
303 }
304
305 int indexOf(element, [int start = 0]) {
306 return Arrays.indexOf(this, element, start, this.length);
307 }
308
309 int lastIndexOf(element, [int start = null]) {
310 if (start === null) start = length - 1;
311 return Arrays.lastIndexOf(this, element, start);
312 }
313
258 void clear() { 314 void clear() {
259 throw const UnsupportedOperationException( 315 throw const UnsupportedOperationException(
260 "Cannot remove from a non-extendable array"); 316 "Cannot remove from a non-extendable array");
261 } 317 }
262 318
263 int indexOf(element, [int start = 0]) {
264 for (int i = start; i < length; i++) {
265 if (this[i] == element) return i;
266 }
267 return -1;
268 }
269
270 void insertRange(int start, int length, [initialValue]) {
271 throw const UnsupportedOperationException(
272 "Cannot add to a non-extendable array");
273 }
274
275 int get length() {
276 return _length();
277 }
278
279 set length(newLength) {
280 throw const UnsupportedOperationException(
281 "Cannot resize a non-extendable array");
282 }
283
284 int removeLast() { 319 int removeLast() {
285 throw const UnsupportedOperationException( 320 throw const UnsupportedOperationException(
286 "Cannot remove from a non-extendable array"); 321 "Cannot remove from a non-extendable array");
287 } 322 }
288 323
324 last() {
325 return this[length - 1];
326 }
327
289 void removeRange(int start, int length) { 328 void removeRange(int start, int length) {
290 throw const UnsupportedOperationException( 329 throw const UnsupportedOperationException(
291 "Cannot remove from a non-extendable array"); 330 "Cannot remove from a non-extendable array");
292 } 331 }
293 332
333 void insertRange(int start, int length, [initialValue]) {
334 throw const UnsupportedOperationException(
335 "Cannot add to a non-extendable array");
336 }
337
294 ByteArray asByteArray([int start = 0, int length]) { 338 ByteArray asByteArray([int start = 0, int length]) {
295 if (length === null) { 339 if (length === null) {
296 length = this.lengthInBytes(); 340 length = this.lengthInBytes();
297 } 341 }
298 return new _ByteArrayView(this, start, length); 342 return new _ByteArrayView(this, start, length);
299 } 343 }
300 344
301 int _length() native "ByteArray_getLength"; 345 int _length() native "ByteArray_getLength";
302 346
303 void _setRange(int startInBytes, int lengthInBytes, 347 void _setRange(int startInBytes, int lengthInBytes,
(...skipping 1336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1640 _array._setFloat64(_offset + byteOffset, value); 1684 _array._setFloat64(_offset + byteOffset, value);
1641 } 1685 }
1642 1686
1643 final _ByteArrayBase _array; 1687 final _ByteArrayBase _array;
1644 final int _offset; 1688 final int _offset;
1645 final int _length; 1689 final int _length;
1646 } 1690 }
1647 1691
1648 1692
1649 class _ByteArrayViewBase { 1693 class _ByteArrayViewBase {
1694
1695 // Methods implementing the Collection interface.
1696
1697 void forEach(void f(element)) {
1698 var len = this.length;
1699 for (var i = 0; i < len; i++) {
1700 f(this[i]);
1701 }
1702 }
1703
1704 Collection map(f(element)) {
1705 return Collections.map(this,
1706 new GrowableObjectArray.withCapacity(length),
1707 f);
1708 }
1709
1710 Collection filter(bool f(element)) {
1711 return Collections.filter(this, new GrowableObjectArray(), f);
1712 }
1713
1714 bool every(bool f(element)) {
1715 return Collections.every(this, f);
1716 }
1717
1718 bool some(bool f(element)) {
1719 return Collections.some(this, f);;
1720 }
1721
1722 bool isEmpty() {
1723 return this.length === 0;
1724 }
1725
1726 int get length() {
1727 return _length();
1728 }
1729
1730 // Methods implementing the List interface.
1731
1732 set length(newLength) {
1733 throw const UnsupportedOperationException(
1734 "Cannot resize a non-extendable array");
1735 }
1736
1650 void add(value) { 1737 void add(value) {
1651 throw const UnsupportedOperationException( 1738 throw const UnsupportedOperationException(
1652 "Cannot add to a non-extendable array"); 1739 "Cannot add to a non-extendable array");
1653 } 1740 }
1654 1741
1655 void addLast(value) { 1742 void addLast(value) {
1656 throw const UnsupportedOperationException( 1743 throw const UnsupportedOperationException(
1657 "Cannot add to a non-extendable array"); 1744 "Cannot add to a non-extendable array");
1658 } 1745 }
1659 1746
1660 void addAll(Collection value) { 1747 void addAll(Collection value) {
1661 throw const UnsupportedOperationException( 1748 throw const UnsupportedOperationException(
1662 "Cannot add to a non-extendable array"); 1749 "Cannot add to a non-extendable array");
1663 } 1750 }
1664 1751
1752 void sort(int compare(a, b)) {
1753 DualPivotQuicksort.sort(this, compare);
1754 }
1755
1756 int indexOf(element, [int start = 0]) {
1757 return Arrays.indexOf(this, element, start, this.length);
1758 }
1759
1760 int lastIndexOf(element, [int start = null]) {
1761 if (start === null) start = length - 1;
1762 return Arrays.lastIndexOf(this, element, start);
1763 }
1764
1665 void clear() { 1765 void clear() {
1666 throw const UnsupportedOperationException( 1766 throw const UnsupportedOperationException(
1667 "Cannot remove from a non-extendable array"); 1767 "Cannot remove from a non-extendable array");
1668 } 1768 }
1669 1769
1670 void insertRange(int start, int length, [initialValue]) {
1671 throw const UnsupportedOperationException(
1672 "Cannot add to a non-extendable array");
1673 }
1674
1675 set length(int newLength) {
1676 throw const UnsupportedOperationException(
1677 "Cannot resize a non-extendable array");
1678 }
1679
1680 int removeLast() { 1770 int removeLast() {
1681 throw const UnsupportedOperationException( 1771 throw const UnsupportedOperationException(
1682 "Cannot remove from a non-extendable array"); 1772 "Cannot remove from a non-extendable array");
1683 } 1773 }
1684 1774
1775 last() {
1776 return this[length - 1];
1777 }
1778
1685 void removeRange(int start, int length) { 1779 void removeRange(int start, int length) {
1686 throw const UnsupportedOperationException( 1780 throw const UnsupportedOperationException(
1687 "Cannot remove from a non-extendable array"); 1781 "Cannot remove from a non-extendable array");
1688 } 1782 }
1783
1784 void insertRange(int start, int length, [initialValue]) {
1785 throw const UnsupportedOperationException(
1786 "Cannot add to a non-extendable array");
1787 }
1689 } 1788 }
1690 1789
1691 1790
1692 class _Int8ArrayView extends _ByteArrayViewBase implements Int8List { 1791 class _Int8ArrayView extends _ByteArrayViewBase implements Int8List {
1693 _Int8ArrayView(ByteArray array, [int offsetInBytes = 0, int length]) 1792 _Int8ArrayView(ByteArray array, [int offsetInBytes = 0, int length])
1694 : _array = array, 1793 : _array = array,
1695 _offset = offsetInBytes, 1794 _offset = offsetInBytes,
1696 _length = (length === null) ? (array.lengthInBytes() - offsetInBytes) 1795 _length = (length === null) ? (array.lengthInBytes() - offsetInBytes)
1697 : length { 1796 : length {
1698 if (offsetInBytes < 0 || offsetInBytes >= array.lengthInBytes()) { 1797 if (offsetInBytes < 0 || offsetInBytes >= array.lengthInBytes()) {
(...skipping 27 matching lines...) Expand all
1726 return new _ByteArrayIterator<int>(this); 1825 return new _ByteArrayIterator<int>(this);
1727 } 1826 }
1728 1827
1729 List<int> getRange(int start, int length) { 1828 List<int> getRange(int start, int length) {
1730 _rangeCheck(this, start, length); 1829 _rangeCheck(this, start, length);
1731 List<int> result = new Int8List(length); 1830 List<int> result = new Int8List(length);
1732 result.setRange(0, length, this, start); 1831 result.setRange(0, length, this, start);
1733 return result; 1832 return result;
1734 } 1833 }
1735 1834
1736 void setRange(int start, int length, List<int> from, [int startFrom = 0]) { 1835 void setRange(int start, int length, List<int> from, [int startFrom = 0]) {
Anders Johnsen 2012/05/11 08:52:13 I was wondering if we should change this to a call
1737 Arrays.copy(from, startFrom, this, start, length); 1836 Arrays.copy(from, startFrom, this, start, length);
1738 } 1837 }
1739 1838
1740 String toString() { 1839 String toString() {
1741 return Collections.collectionToString(this); 1840 return Collections.collectionToString(this);
1742 } 1841 }
1743 1842
1744 int bytesPerElement() { 1843 int bytesPerElement() {
1745 return _BYTES_PER_ELEMENT; 1844 return _BYTES_PER_ELEMENT;
1746 } 1845 }
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after
2430 } 2529 }
2431 _rangeCheck(start, length); 2530 _rangeCheck(start, length);
2432 return _array.subByteArray(_offset + start, length); 2531 return _array.subByteArray(_offset + start, length);
2433 } 2532 }
2434 2533
2435 static final int _BYTES_PER_ELEMENT = 8; 2534 static final int _BYTES_PER_ELEMENT = 8;
2436 final ByteArray _array; 2535 final ByteArray _array;
2437 final int _offset; 2536 final int _offset;
2438 final int _length; 2537 final int _length;
2439 } 2538 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698