Chromium Code Reviews| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 55 result[i] = elements[i]; | 55 result[i] = elements[i]; |
| 56 } | 56 } |
| 57 return result; | 57 return result; |
| 58 } | 58 } |
| 59 | 59 |
| 60 /* patch */ factory Uint8ClampedList.view(ByteBuffer buffer, | 60 /* patch */ factory Uint8ClampedList.view(ByteBuffer buffer, |
| 61 [int offsetInBytes = 0, | 61 [int offsetInBytes = 0, |
| 62 int length]) { | 62 int length]) { |
| 63 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); | 63 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); |
| 64 } | 64 } |
| 65 | |
| 66 bool _isClamped() { return true; } | |
| 65 } | 67 } |
| 66 | 68 |
| 67 | 69 |
| 68 patch class Int16List { | 70 patch class Int16List { |
| 69 /* patch */ factory Int16List(int length) { | 71 /* patch */ factory Int16List(int length) { |
| 70 return new _Int16Array(length); | 72 return new _Int16Array(length); |
| 71 } | 73 } |
| 72 | 74 |
| 73 /* patch */ factory Int16List.fromList(List<int> elements) { | 75 /* patch */ factory Int16List.fromList(List<int> elements) { |
| 74 var result = new _Int16Array(elements.length); | 76 var result = new _Int16Array(elements.length); |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 516 _rangeCheck(this.length, start, length); | 518 _rangeCheck(this.length, start, length); |
| 517 List result = _createList(length); | 519 List result = _createList(length); |
| 518 result.setRange(0, length, this, start); | 520 result.setRange(0, length, this, start); |
| 519 return result; | 521 return result; |
| 520 } | 522 } |
| 521 | 523 |
| 522 Iterable getRange(int start, [int end]) { | 524 Iterable getRange(int start, [int end]) { |
| 523 return IterableMixinWorkaround.getRangeList(this, start, end); | 525 return IterableMixinWorkaround.getRangeList(this, start, end); |
| 524 } | 526 } |
| 525 | 527 |
| 526 void setRange(int start, int end, Iterable iterable, [int skipCount = 0]) { | 528 bool _isClamped() { return false; } |
| 527 if (!_setRange(start, end - start, iterable, skipCount)) { | 529 |
| 528 IterableMixinWorkaround.setRangeList(this, start, | 530 void setRange(int start, int end, Iterable from, [int skipCount = 0]) { |
| 529 end, iterable, skipCount); | 531 if (from is _TypedListBase){ |
| 532 final needsClamping = | |
| 533 this._isClamped() && (this._isClamped() != from._isClamped()); | |
| 534 if (!needsClamping && | |
| 535 (this.elementSizeInBytes == from.elementSizeInBytes)) { | |
| 536 if (this.buffer._setRange( | |
|
sra1
2014/01/07 18:55:48
this.buffer is a ByteBuffer.
There is no guarantee
srdjan
2014/01/07 19:20:35
OK.
On 2014/01/07 18:55:48, sra1 wrote:
| |
| 537 start + (this.offsetInBytes ~/ this.elementSizeInBytes), | |
| 538 end - start, | |
| 539 from.buffer, | |
| 540 skipCount + (from.offsetInBytes ~/ from.elementSizeInBytes))) { | |
| 541 return; | |
| 542 } | |
| 543 } else if (from.buffer == this.buffer) { | |
| 544 // Different element sizes, but same buffer means that we need | |
| 545 // an intermediate structure. | |
|
sra1
2014/01/07 18:55:48
You never need an intermediate structure.
You migh
srdjan
2014/01/07 19:20:35
Yes, next CL.
On 2014/01/07 18:55:48, sra1 wrote:
| |
| 546 // TODO(srdjan): Optimize to skip copying if the range does not overlap. | |
| 547 final len = end - start; | |
| 548 final buffer = new List(len); | |
| 549 for (int i = 0; i < len; i++) { | |
| 550 buffer[i] = from[skipCount + i]; | |
| 551 } | |
| 552 for (int i = start; i < end; i++) { | |
| 553 this[i] = buffer[i - start]; | |
| 554 } | |
| 555 return; | |
| 556 } | |
| 530 } | 557 } |
| 558 IterableMixinWorkaround.setRangeList(this, start, | |
| 559 end, from, skipCount); | |
| 531 } | 560 } |
| 532 | 561 |
| 533 void setAll(int index, Iterable iterable) { | 562 void setAll(int index, Iterable iterable) { |
| 534 IterableMixinWorkaround.setAllList(this, index, iterable); | 563 IterableMixinWorkaround.setAllList(this, index, iterable); |
| 535 } | 564 } |
| 536 | 565 |
| 537 void fillRange(int start, int end, [fillValue]) { | 566 void fillRange(int start, int end, [fillValue]) { |
| 538 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); | 567 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); |
| 539 } | 568 } |
| 540 | 569 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 560 } | 589 } |
| 561 | 590 |
| 562 int get lengthInBytes { | 591 int get lengthInBytes { |
| 563 return length * elementSizeInBytes; | 592 return length * elementSizeInBytes; |
| 564 } | 593 } |
| 565 | 594 |
| 566 ByteBuffer get buffer { | 595 ByteBuffer get buffer { |
| 567 return this; | 596 return this; |
| 568 } | 597 } |
| 569 | 598 |
| 570 | |
| 571 // Methods implementing the collection interface. | 599 // Methods implementing the collection interface. |
| 572 | 600 |
| 573 int get length native "TypedData_length"; | 601 int get length native "TypedData_length"; |
| 574 | 602 |
| 575 | |
| 576 // Internal utility methods. | 603 // Internal utility methods. |
| 577 | 604 |
| 578 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; | 605 int _getInt8(int offsetInBytes) native "TypedData_GetInt8"; |
| 579 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; | 606 void _setInt8(int offsetInBytes, int value) native "TypedData_SetInt8"; |
| 580 | 607 |
| 581 int _getUint8(int offsetInBytes) native "TypedData_GetUint8"; | 608 int _getUint8(int offsetInBytes) native "TypedData_GetUint8"; |
| 582 void _setUint8(int offsetInBytes, int value) native "TypedData_SetUint8"; | 609 void _setUint8(int offsetInBytes, int value) native "TypedData_SetUint8"; |
| 583 | 610 |
| 584 int _getInt16(int offsetInBytes) native "TypedData_GetInt16"; | 611 int _getInt16(int offsetInBytes) native "TypedData_GetInt16"; |
| 585 void _setInt16(int offsetInBytes, int value) native "TypedData_SetInt16"; | 612 void _setInt16(int offsetInBytes, int value) native "TypedData_SetInt16"; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 } | 758 } |
| 732 | 759 |
| 733 factory _Uint8ClampedArray.view(ByteBuffer buffer, | 760 factory _Uint8ClampedArray.view(ByteBuffer buffer, |
| 734 [int offsetInBytes = 0, int length]) { | 761 [int offsetInBytes = 0, int length]) { |
| 735 if (length == null) { | 762 if (length == null) { |
| 736 length = buffer.lengthInBytes - offsetInBytes; | 763 length = buffer.lengthInBytes - offsetInBytes; |
| 737 } | 764 } |
| 738 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); | 765 return new _Uint8ClampedArrayView(buffer, offsetInBytes, length); |
| 739 } | 766 } |
| 740 | 767 |
| 768 bool _isClamped() { return true; } | |
| 741 | 769 |
| 742 // Methods implementing List interface. | 770 // Methods implementing List interface. |
| 743 | 771 |
| 744 int operator[](int index) { | 772 int operator[](int index) { |
| 745 if (index < 0 || index >= length) { | 773 if (index < 0 || index >= length) { |
| 746 _throwRangeError(index, length); | 774 _throwRangeError(index, length); |
| 747 } | 775 } |
| 748 return _getUint8(index); | 776 return _getUint8(index); |
| 749 } | 777 } |
| 750 | 778 |
| (...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1496 } | 1524 } |
| 1497 | 1525 |
| 1498 | 1526 |
| 1499 class _ExternalUint8ClampedArray extends _TypedList implements Uint8ClampedList { | 1527 class _ExternalUint8ClampedArray extends _TypedList implements Uint8ClampedList { |
| 1500 // Factory constructors. | 1528 // Factory constructors. |
| 1501 | 1529 |
| 1502 factory _ExternalUint8ClampedArray(int length) { | 1530 factory _ExternalUint8ClampedArray(int length) { |
| 1503 return _new(length); | 1531 return _new(length); |
| 1504 } | 1532 } |
| 1505 | 1533 |
| 1534 bool _isClamped() { return true; } | |
| 1506 | 1535 |
| 1507 // Method(s) implementing the List interface. | 1536 // Method(s) implementing the List interface. |
| 1508 | 1537 |
| 1509 int operator[](int index) { | 1538 int operator[](int index) { |
| 1510 if (index < 0 || index >= length) { | 1539 if (index < 0 || index >= length) { |
| 1511 _throwRangeError(index, length); | 1540 _throwRangeError(index, length); |
| 1512 } | 1541 } |
| 1513 return _getUint8(index); | 1542 return _getUint8(index); |
| 1514 } | 1543 } |
| 1515 | 1544 |
| (...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2279 } | 2308 } |
| 2280 | 2309 |
| 2281 | 2310 |
| 2282 class _TypedListView extends _TypedListBase implements TypedData { | 2311 class _TypedListView extends _TypedListBase implements TypedData { |
| 2283 _TypedListView(ByteBuffer _buffer, int _offset, int _length) | 2312 _TypedListView(ByteBuffer _buffer, int _offset, int _length) |
| 2284 : _typedData = _buffer, // This assignment is type safe. | 2313 : _typedData = _buffer, // This assignment is type safe. |
| 2285 offsetInBytes = _offset, | 2314 offsetInBytes = _offset, |
| 2286 length = _length { | 2315 length = _length { |
| 2287 } | 2316 } |
| 2288 | 2317 |
| 2289 | |
| 2290 // Method(s) implementing the TypedData interface. | 2318 // Method(s) implementing the TypedData interface. |
| 2291 | 2319 |
| 2292 int get lengthInBytes { | 2320 int get lengthInBytes { |
| 2293 return length * elementSizeInBytes; | 2321 return length * elementSizeInBytes; |
| 2294 } | 2322 } |
| 2295 | 2323 |
| 2296 ByteBuffer get buffer { | 2324 ByteBuffer get buffer { |
| 2297 return _typedData.buffer; | 2325 return _typedData.buffer; |
| 2298 } | 2326 } |
| 2299 | 2327 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2412 : super(buffer, _offsetInBytes, | 2440 : super(buffer, _offsetInBytes, |
| 2413 _defaultIfNull(_length, | 2441 _defaultIfNull(_length, |
| 2414 ((buffer.lengthInBytes - _offsetInBytes) ~/ | 2442 ((buffer.lengthInBytes - _offsetInBytes) ~/ |
| 2415 Uint8List.BYTES_PER_ELEMENT))) { | 2443 Uint8List.BYTES_PER_ELEMENT))) { |
| 2416 _rangeCheck(buffer.lengthInBytes, | 2444 _rangeCheck(buffer.lengthInBytes, |
| 2417 offsetInBytes, | 2445 offsetInBytes, |
| 2418 length * Uint8List.BYTES_PER_ELEMENT); | 2446 length * Uint8List.BYTES_PER_ELEMENT); |
| 2419 } | 2447 } |
| 2420 | 2448 |
| 2421 | 2449 |
| 2450 bool _isClamped() { return true; } | |
| 2451 | |
| 2422 // Method(s) implementing List interface. | 2452 // Method(s) implementing List interface. |
| 2423 | 2453 |
| 2424 int operator[](int index) { | 2454 int operator[](int index) { |
| 2425 if (index < 0 || index >= length) { | 2455 if (index < 0 || index >= length) { |
| 2426 _throwRangeError(index, length); | 2456 _throwRangeError(index, length); |
| 2427 } | 2457 } |
| 2428 return _typedData._getUint8(offsetInBytes + | 2458 return _typedData._getUint8(offsetInBytes + |
| 2429 (index * Uint8List.BYTES_PER_ELEMENT)); | 2459 (index * Uint8List.BYTES_PER_ELEMENT)); |
| 2430 } | 2460 } |
| 2431 | 2461 |
| (...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3345 return value; | 3375 return value; |
| 3346 } | 3376 } |
| 3347 return object; | 3377 return object; |
| 3348 } | 3378 } |
| 3349 | 3379 |
| 3350 | 3380 |
| 3351 void _throwRangeError(int index, int length) { | 3381 void _throwRangeError(int index, int length) { |
| 3352 String message = "$index must be in the range [0..$length)"; | 3382 String message = "$index must be in the range [0..$length)"; |
| 3353 throw new RangeError(message); | 3383 throw new RangeError(message); |
| 3354 } | 3384 } |
| OLD | NEW |