| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // patch classes for Int8List ..... Float64List and ByteData implementations. | 5 // patch classes for Int8List ..... Float64List and ByteData implementations. |
| 6 | 6 |
| 7 import "dart:_internal"; | 7 import "dart:_internal"; |
| 8 import 'dart:math' show Random; | 8 import 'dart:math' show Random; |
| 9 | 9 |
| 10 patch class Int8List { | 10 patch class Int8List { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 /* patch */ factory Uint8ClampedList.fromList(List<int> elements) { | 49 /* patch */ factory Uint8ClampedList.fromList(List<int> elements) { |
| 50 return new _Uint8ClampedArray(elements.length) | 50 return new _Uint8ClampedArray(elements.length) |
| 51 ..setRange(0, elements.length, elements); | 51 ..setRange(0, elements.length, elements); |
| 52 } | 52 } |
| 53 | 53 |
| 54 /* patch */ factory Uint8ClampedList.view(ByteBuffer buffer, | 54 /* patch */ factory Uint8ClampedList.view(ByteBuffer buffer, |
| 55 [int offsetInBytes = 0, | 55 [int offsetInBytes = 0, |
| 56 int length]) { | 56 int length]) { |
| 57 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); | 57 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); |
| 58 } | 58 } |
| 59 | |
| 60 bool _isClamped() { return true; } | |
| 61 } | 59 } |
| 62 | 60 |
| 63 | 61 |
| 64 patch class Int16List { | 62 patch class Int16List { |
| 65 /* patch */ factory Int16List(int length) { | 63 /* patch */ factory Int16List(int length) { |
| 66 return new _Int16Array(length); | 64 return new _Int16Array(length); |
| 67 } | 65 } |
| 68 | 66 |
| 69 /* patch */ factory Int16List.fromList(List<int> elements) { | 67 /* patch */ factory Int16List.fromList(List<int> elements) { |
| 70 return new _Int16Array(elements.length) | 68 return new _Int16Array(elements.length) |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 _rangeCheck(this.length, start, length); | 519 _rangeCheck(this.length, start, length); |
| 522 List result = _createList(length); | 520 List result = _createList(length); |
| 523 result.setRange(0, length, this, start); | 521 result.setRange(0, length, this, start); |
| 524 return result; | 522 return result; |
| 525 } | 523 } |
| 526 | 524 |
| 527 Iterable getRange(int start, [int end]) { | 525 Iterable getRange(int start, [int end]) { |
| 528 return IterableMixinWorkaround.getRangeList(this, start, end); | 526 return IterableMixinWorkaround.getRangeList(this, start, end); |
| 529 } | 527 } |
| 530 | 528 |
| 531 bool _isClamped() { return false; } | |
| 532 | |
| 533 void setRange(int start, int end, Iterable from, [int skipCount = 0]) { | 529 void setRange(int start, int end, Iterable from, [int skipCount = 0]) { |
| 534 // Check ranges. | 530 // Check ranges. |
| 535 if ((start < 0) || (start > length)) { | 531 if ((start < 0) || (start > length)) { |
| 536 _throwRangeError(start, length + 1); | 532 _throwRangeError(start, length + 1); |
| 537 } | 533 } |
| 538 if ((end < 0) || (end > length)) { | 534 if ((end < 0) || (end > length)) { |
| 539 _throwRangeError(end, length + 1); | 535 _throwRangeError(end, length + 1); |
| 540 } | 536 } |
| 541 if (start > end) { | 537 if (start > end) { |
| 542 _throwRangeError(start, end + 1); | 538 _throwRangeError(start, end + 1); |
| 543 } | 539 } |
| 544 if (skipCount < 0) { | 540 if (skipCount < 0) { |
| 545 throw new ArgumentError(skipCount); | 541 throw new ArgumentError(skipCount); |
| 546 } | 542 } |
| 547 | 543 |
| 548 final count = end - start; | 544 final count = end - start; |
| 549 if ((from.length - skipCount) < count) { | 545 if ((from.length - skipCount) < count) { |
| 550 throw new StateError("Not enough elements"); | 546 throw new StateError("Not enough elements"); |
| 551 } | 547 } |
| 552 | 548 |
| 553 if (from is _TypedListBase) { | 549 if (from is _TypedListBase) { |
| 554 final needsClamping = | |
| 555 this._isClamped() && (this._isClamped() != from._isClamped()); | |
| 556 if (this.elementSizeInBytes == from.elementSizeInBytes) { | 550 if (this.elementSizeInBytes == from.elementSizeInBytes) { |
| 557 if (needsClamping) { | 551 if (this.buffer._setRange( |
| 558 Lists.copy(from, skipCount, this, start, count); | 552 start * elementSizeInBytes + this.offsetInBytes, |
| 559 return; | 553 count * elementSizeInBytes, |
| 560 } else if (this.buffer._setRange( | 554 from.buffer, |
| 561 start * elementSizeInBytes + this.offsetInBytes, | 555 skipCount * elementSizeInBytes + from.offsetInBytes, |
| 562 count * elementSizeInBytes, | 556 this._cid, from._cid)) { |
| 563 from.buffer, | |
| 564 skipCount * elementSizeInBytes + from.offsetInBytes)) { | |
| 565 return; | 557 return; |
| 566 } | 558 } |
| 567 } else if (from.buffer == this.buffer) { | 559 } else if (from.buffer == this.buffer) { |
| 568 // Different element sizes, but same buffer means that we need | 560 // Different element sizes, but same buffer means that we need |
| 569 // an intermediate structure. | 561 // an intermediate structure. |
| 570 // TODO(srdjan): Optimize to skip copying if the range does not overlap. | 562 // TODO(srdjan): Optimize to skip copying if the range does not overlap. |
| 571 final temp_buffer = new List(count); | 563 final temp_buffer = new List(count); |
| 572 for (int i = 0; i < count; i++) { | 564 for (int i = 0; i < count; i++) { |
| 573 temp_buffer[i] = from[skipCount + i]; | 565 temp_buffer[i] = from[skipCount + i]; |
| 574 } | 566 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 595 // Method(s) implementing Object interface. | 587 // Method(s) implementing Object interface. |
| 596 | 588 |
| 597 String toString() { | 589 String toString() { |
| 598 return IterableMixinWorkaround.toStringIterable(this, '[', ']'); | 590 return IterableMixinWorkaround.toStringIterable(this, '[', ']'); |
| 599 } | 591 } |
| 600 | 592 |
| 601 | 593 |
| 602 // Internal utility methods. | 594 // Internal utility methods. |
| 603 | 595 |
| 604 // Returns true if operation succeeds. | 596 // Returns true if operation succeeds. |
| 605 // Returns false if 'from' and 'this' do not have the same element types. | 597 // 'fromCid' and 'toCid' may be cid-s of the views and therefore may not |
| 606 // The copy occurs using a memory copy (no clamping, conversion, etc). | 598 // match the cids of 'this' and 'from'. |
| 599 // Uses toCid and fromCid to decide if clamping is necessary. |
| 600 // Element size of toCid and fromCid must match (test at caller). |
| 607 bool _setRange(int startInBytes, int lengthInBytes, | 601 bool _setRange(int startInBytes, int lengthInBytes, |
| 608 _TypedListBase from, int startFromInBytes) | 602 _TypedListBase from, int startFromInBytes, |
| 603 int toCid, int fromCid) |
| 609 native "TypedData_setRange"; | 604 native "TypedData_setRange"; |
| 605 |
| 606 int get _cid native "Object_cid"; |
| 610 } | 607 } |
| 611 | 608 |
| 612 | 609 |
| 613 abstract class _TypedList extends _TypedListBase implements ByteBuffer { | 610 abstract class _TypedList extends _TypedListBase implements ByteBuffer { |
| 614 // Default method implementing parts of the TypedData interface. | 611 // Default method implementing parts of the TypedData interface. |
| 615 int get offsetInBytes { | 612 int get offsetInBytes { |
| 616 return 0; | 613 return 0; |
| 617 } | 614 } |
| 618 | 615 |
| 619 int get lengthInBytes { | 616 int get lengthInBytes { |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 } | 787 } |
| 791 | 788 |
| 792 factory _Uint8ClampedArray.view(ByteBuffer buffer, | 789 factory _Uint8ClampedArray.view(ByteBuffer buffer, |
| 793 [int offsetInBytes = 0, int length]) { | 790 [int offsetInBytes = 0, int length]) { |
| 794 if (length == null) { | 791 if (length == null) { |
| 795 length = buffer.lengthInBytes - offsetInBytes; | 792 length = buffer.lengthInBytes - offsetInBytes; |
| 796 } | 793 } |
| 797 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); | 794 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); |
| 798 } | 795 } |
| 799 | 796 |
| 800 bool _isClamped() { return true; } | |
| 801 | |
| 802 // Methods implementing List interface. | 797 // Methods implementing List interface. |
| 803 | 798 |
| 804 int operator[](int index) { | 799 int operator[](int index) { |
| 805 if (index < 0 || index >= length) { | 800 if (index < 0 || index >= length) { |
| 806 _throwRangeError(index, length); | 801 _throwRangeError(index, length); |
| 807 } | 802 } |
| 808 return _getUint8(index); | 803 return _getUint8(index); |
| 809 } | 804 } |
| 810 | 805 |
| 811 void operator[]=(int index, int value) { | 806 void operator[]=(int index, int value) { |
| (...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1617 } | 1612 } |
| 1618 | 1613 |
| 1619 | 1614 |
| 1620 class _ExternalUint8ClampedArray extends _TypedList implements Uint8ClampedList
{ | 1615 class _ExternalUint8ClampedArray extends _TypedList implements Uint8ClampedList
{ |
| 1621 // Factory constructors. | 1616 // Factory constructors. |
| 1622 | 1617 |
| 1623 factory _ExternalUint8ClampedArray(int length) { | 1618 factory _ExternalUint8ClampedArray(int length) { |
| 1624 return _new(length); | 1619 return _new(length); |
| 1625 } | 1620 } |
| 1626 | 1621 |
| 1627 bool _isClamped() { return true; } | |
| 1628 | |
| 1629 // Method(s) implementing the List interface. | 1622 // Method(s) implementing the List interface. |
| 1630 | 1623 |
| 1631 int operator[](int index) { | 1624 int operator[](int index) { |
| 1632 if (index < 0 || index >= length) { | 1625 if (index < 0 || index >= length) { |
| 1633 _throwRangeError(index, length); | 1626 _throwRangeError(index, length); |
| 1634 } | 1627 } |
| 1635 return _getUint8(index); | 1628 return _getUint8(index); |
| 1636 } | 1629 } |
| 1637 | 1630 |
| 1638 void operator[]=(int index, int value) { | 1631 void operator[]=(int index, int value) { |
| (...skipping 1014 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2653 : super(buffer, _offsetInBytes, | 2646 : super(buffer, _offsetInBytes, |
| 2654 _defaultIfNull(_length, | 2647 _defaultIfNull(_length, |
| 2655 ((buffer.lengthInBytes - _offsetInBytes) ~/ | 2648 ((buffer.lengthInBytes - _offsetInBytes) ~/ |
| 2656 Uint8List.BYTES_PER_ELEMENT))) { | 2649 Uint8List.BYTES_PER_ELEMENT))) { |
| 2657 _rangeCheck(buffer.lengthInBytes, | 2650 _rangeCheck(buffer.lengthInBytes, |
| 2658 offsetInBytes, | 2651 offsetInBytes, |
| 2659 length * Uint8List.BYTES_PER_ELEMENT); | 2652 length * Uint8List.BYTES_PER_ELEMENT); |
| 2660 } | 2653 } |
| 2661 | 2654 |
| 2662 | 2655 |
| 2663 bool _isClamped() { return true; } | |
| 2664 | |
| 2665 // Method(s) implementing List interface. | 2656 // Method(s) implementing List interface. |
| 2666 | 2657 |
| 2667 int operator[](int index) { | 2658 int operator[](int index) { |
| 2668 if (index < 0 || index >= length) { | 2659 if (index < 0 || index >= length) { |
| 2669 _throwRangeError(index, length); | 2660 _throwRangeError(index, length); |
| 2670 } | 2661 } |
| 2671 return _typedData._getUint8(offsetInBytes + | 2662 return _typedData._getUint8(offsetInBytes + |
| 2672 (index * Uint8List.BYTES_PER_ELEMENT)); | 2663 (index * Uint8List.BYTES_PER_ELEMENT)); |
| 2673 } | 2664 } |
| 2674 | 2665 |
| (...skipping 965 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3640 return value; | 3631 return value; |
| 3641 } | 3632 } |
| 3642 return object; | 3633 return object; |
| 3643 } | 3634 } |
| 3644 | 3635 |
| 3645 | 3636 |
| 3646 void _throwRangeError(int index, int length) { | 3637 void _throwRangeError(int index, int length) { |
| 3647 String message = "$index must be in the range [0..$length)"; | 3638 String message = "$index must be in the range [0..$length)"; |
| 3648 throw new RangeError(message); | 3639 throw new RangeError(message); |
| 3649 } | 3640 } |
| OLD | NEW |