| 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 patch class Int8List { | 7 patch class Int8List { |
| 8 /* patch */ factory Int8List(int length) { | 8 /* patch */ factory Int8List(int length) { |
| 9 return new _Int8Array(length); | 9 return new _Int8Array(length); |
| 10 } | 10 } |
| (...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 return result; | 521 return result; |
| 522 } | 522 } |
| 523 | 523 |
| 524 Iterable getRange(int start, [int end]) { | 524 Iterable getRange(int start, [int end]) { |
| 525 return IterableMixinWorkaround.getRangeList(this, start, end); | 525 return IterableMixinWorkaround.getRangeList(this, start, end); |
| 526 } | 526 } |
| 527 | 527 |
| 528 bool _isClamped() { return false; } | 528 bool _isClamped() { return false; } |
| 529 | 529 |
| 530 void setRange(int start, int end, Iterable from, [int skipCount = 0]) { | 530 void setRange(int start, int end, Iterable from, [int skipCount = 0]) { |
| 531 if (from is _TypedListBase){ | 531 // Check ranges. |
| 532 if ((start < 0) || (start > length)) { |
| 533 _throwRangeError(start, length + 1); |
| 534 } |
| 535 if ((end < 0) || (end > length)) { |
| 536 _throwRangeError(end, length + 1); |
| 537 } |
| 538 if (start > end) { |
| 539 _throwRangeError(start, end + 1); |
| 540 } |
| 541 if (skipCount < 0) { |
| 542 throw new ArgumentError(skipCount); |
| 543 } |
| 544 |
| 545 final count = end - start; |
| 546 if ((from.length - skipCount) < count) { |
| 547 throw new StateError("Not enough elements"); |
| 548 } |
| 549 |
| 550 if (from is _TypedListBase) { |
| 532 final needsClamping = | 551 final needsClamping = |
| 533 this._isClamped() && (this._isClamped() != from._isClamped()); | 552 this._isClamped() && (this._isClamped() != from._isClamped()); |
| 534 if (!needsClamping && | 553 if (this.elementSizeInBytes == from.elementSizeInBytes) { |
| 535 (this.elementSizeInBytes == from.elementSizeInBytes)) { | 554 if (needsClamping) { |
| 536 if (this.buffer._setRange( | 555 Lists.copy(from, skipCount, this, start, count); |
| 537 start + (this.offsetInBytes ~/ this.elementSizeInBytes), | 556 return; |
| 538 end - start, | 557 } else if (this.buffer._setRange( |
| 539 from.buffer, | 558 start * elementSizeInBytes + this.offsetInBytes, |
| 540 skipCount + (from.offsetInBytes ~/ from.elementSizeInBytes))) { | 559 count * elementSizeInBytes, |
| 560 from.buffer, |
| 561 skipCount * elementSizeInBytes + from.offsetInBytes)) { |
| 541 return; | 562 return; |
| 542 } | 563 } |
| 543 } else if (from.buffer == this.buffer) { | 564 } else if (from.buffer == this.buffer) { |
| 544 // Different element sizes, but same buffer means that we need | 565 // Different element sizes, but same buffer means that we need |
| 545 // an intermediate structure. | 566 // an intermediate structure. |
| 546 // TODO(srdjan): Optimize to skip copying if the range does not overlap. | 567 // TODO(srdjan): Optimize to skip copying if the range does not overlap. |
| 547 final len = end - start; | 568 final temp_buffer = new List(count); |
| 548 final buffer = new List(len); | 569 for (int i = 0; i < count; i++) { |
| 549 for (int i = 0; i < len; i++) { | 570 temp_buffer[i] = from[skipCount + i]; |
| 550 buffer[i] = from[skipCount + i]; | |
| 551 } | 571 } |
| 552 for (int i = start; i < end; i++) { | 572 for (int i = start; i < end; i++) { |
| 553 this[i] = buffer[i - start]; | 573 this[i] = temp_buffer[i - start]; |
| 554 } | 574 } |
| 555 return; | 575 return; |
| 556 } | 576 } |
| 557 } | 577 } |
| 558 IterableMixinWorkaround.setRangeList(this, start, | 578 IterableMixinWorkaround.setRangeList(this, start, |
| 559 end, from, skipCount); | 579 end, from, skipCount); |
| 560 } | 580 } |
| 561 | 581 |
| 562 void setAll(int index, Iterable iterable) { | 582 void setAll(int index, Iterable iterable) { |
| 563 IterableMixinWorkaround.setAllList(this, index, iterable); | 583 IterableMixinWorkaround.setAllList(this, index, iterable); |
| 564 } | 584 } |
| 565 | 585 |
| 566 void fillRange(int start, int end, [fillValue]) { | 586 void fillRange(int start, int end, [fillValue]) { |
| 567 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 587 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
| 568 } | 588 } |
| 569 | 589 |
| 570 | 590 |
| 571 // Method(s) implementing Object interface. | 591 // Method(s) implementing Object interface. |
| 572 | 592 |
| 573 String toString() { | 593 String toString() { |
| 574 return IterableMixinWorkaround.toStringIterable(this, '[', ']'); | 594 return IterableMixinWorkaround.toStringIterable(this, '[', ']'); |
| 575 } | 595 } |
| 576 | 596 |
| 577 | 597 |
| 578 // Internal utility methods. | 598 // Internal utility methods. |
| 579 | 599 |
| 580 bool _setRange(int start, int length, Iterable from, int startFrom) | 600 // Returns true if operation succeeds. |
| 601 // Returns false if 'from' and 'this' do not have the same element types. |
| 602 // The copy occurs using a memory copy (no clamping, conversion, etc). |
| 603 bool _setRange(int startInBytes, int lengthInBytes, |
| 604 _TypedListBase from, int startFromInBytes) |
| 581 native "TypedData_setRange"; | 605 native "TypedData_setRange"; |
| 582 } | 606 } |
| 583 | 607 |
| 584 | 608 |
| 585 abstract class _TypedList extends _TypedListBase implements ByteBuffer { | 609 abstract class _TypedList extends _TypedListBase implements ByteBuffer { |
| 586 // Default method implementing parts of the TypedData interface. | 610 // Default method implementing parts of the TypedData interface. |
| 587 int get offsetInBytes { | 611 int get offsetInBytes { |
| 588 return 0; | 612 return 0; |
| 589 } | 613 } |
| 590 | 614 |
| (...skipping 2784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3375 return value; | 3399 return value; |
| 3376 } | 3400 } |
| 3377 return object; | 3401 return object; |
| 3378 } | 3402 } |
| 3379 | 3403 |
| 3380 | 3404 |
| 3381 void _throwRangeError(int index, int length) { | 3405 void _throwRangeError(int index, int length) { |
| 3382 String message = "$index must be in the range [0..$length)"; | 3406 String message = "$index must be in the range [0..$length)"; |
| 3383 throw new RangeError(message); | 3407 throw new RangeError(message); |
| 3384 } | 3408 } |
| OLD | NEW |