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

Side by Side Diff: pkg/analyzer/lib/src/summary/flat_buffers.dart

Issue 1588043006: Write and read support for Float64 lists in FlatBuffers. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 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
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/flat_buffers_test.dart » ('j') | 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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library analyzer.src.summary.flat_buffers; 5 library analyzer.src.summary.flat_buffers;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:math'; 9 import 'dart:math';
10 import 'dart:typed_data'; 10 import 'dart:typed_data';
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 return _advance(uOffset); 42 return _advance(uOffset);
43 } 43 }
44 44
45 @override 45 @override
46 String toString() => _offset.toString(); 46 String toString() => _offset.toString();
47 47
48 BufferPointer _advance(int delta) { 48 BufferPointer _advance(int delta) {
49 return new BufferPointer._(_buffer, _offset + delta); 49 return new BufferPointer._(_buffer, _offset + delta);
50 } 50 }
51 51
52 double _getFloat64([int delta = 0]) =>
53 _buffer.getFloat64(_offset + delta, Endianness.LITTLE_ENDIAN);
54
52 int _getInt32([int delta = 0]) => 55 int _getInt32([int delta = 0]) =>
53 _buffer.getInt32(_offset + delta, Endianness.LITTLE_ENDIAN); 56 _buffer.getInt32(_offset + delta, Endianness.LITTLE_ENDIAN);
54 57
55 int _getInt8([int delta = 0]) => _buffer.getInt8(_offset + delta); 58 int _getInt8([int delta = 0]) => _buffer.getInt8(_offset + delta);
56 59
57 int _getUint16([int delta = 0]) => 60 int _getUint16([int delta = 0]) =>
58 _buffer.getUint16(_offset + delta, Endianness.LITTLE_ENDIAN); 61 _buffer.getUint16(_offset + delta, Endianness.LITTLE_ENDIAN);
59 62
60 int _getUint32([int delta = 0]) => 63 int _getUint32([int delta = 0]) =>
61 _buffer.getUint32(_offset + delta, Endianness.LITTLE_ENDIAN); 64 _buffer.getUint32(_offset + delta, Endianness.LITTLE_ENDIAN);
62 65
66 int _getUint64([int delta = 0]) =>
67 _buffer.getUint64(_offset + delta, Endianness.LITTLE_ENDIAN);
68
63 /** 69 /**
64 * If the [byteList] is already a [Uint8List] return it. 70 * If the [byteList] is already a [Uint8List] return it.
65 * Otherwise return a [Uint8List] copy of the [byteList]. 71 * Otherwise return a [Uint8List] copy of the [byteList].
66 */ 72 */
67 static Uint8List _asUint8List(List<int> byteList) { 73 static Uint8List _asUint8List(List<int> byteList) {
68 if (byteList is Uint8List) { 74 if (byteList is Uint8List) {
69 return byteList; 75 return byteList;
70 } else { 76 } else {
71 return new Uint8List.fromList(byteList); 77 return new Uint8List.fromList(byteList);
72 } 78 }
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 _setUint32AtTail(_buf, tail, values.length); 298 _setUint32AtTail(_buf, tail, values.length);
293 tail -= 4; 299 tail -= 4;
294 for (Offset value in values) { 300 for (Offset value in values) {
295 _setUint32AtTail(_buf, tail, tail - value._tail); 301 _setUint32AtTail(_buf, tail, tail - value._tail);
296 tail -= 4; 302 tail -= 4;
297 } 303 }
298 return result; 304 return result;
299 } 305 }
300 306
301 /** 307 /**
308 * Write the given list of 64-bit float [values].
309 */
310 Offset writeListFloat64(List<double> values) {
311 if (_currentVTable != null) {
312 throw new StateError(
313 'Cannot write a non-scalar value while writing a table.');
314 }
315 _prepare(8, 1 + values.length);
316 Offset result = new Offset(_tail);
317 int tail = _tail;
318 _setUint64AtTail(_buf, tail, values.length);
319 tail -= 8;
320 for (double value in values) {
321 _setFloat64AtTail(_buf, tail, value);
322 tail -= 8;
323 }
324 return result;
325 }
326
327 /**
302 * Write the given list of signed 32-bit integer [values]. 328 * Write the given list of signed 32-bit integer [values].
303 */ 329 */
304 Offset writeListInt32(List<int> values) { 330 Offset writeListInt32(List<int> values) {
305 if (_currentVTable != null) { 331 if (_currentVTable != null) {
306 throw new StateError( 332 throw new StateError(
307 'Cannot write a non-scalar value while writing a table.'); 333 'Cannot write a non-scalar value while writing a table.');
308 } 334 }
309 _prepare(4, 1 + values.length); 335 _prepare(4, 1 + values.length);
310 Offset result = new Offset(_tail); 336 Offset result = new Offset(_tail);
311 int tail = _tail; 337 int tail = _tail;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 _tail += bufSize; 402 _tail += bufSize;
377 } 403 }
378 404
379 /** 405 /**
380 * Record the offset of the given [field]. 406 * Record the offset of the given [field].
381 */ 407 */
382 void _trackField(int field) { 408 void _trackField(int field) {
383 _currentVTable.addField(field, _tail); 409 _currentVTable.addField(field, _tail);
384 } 410 }
385 411
412 static void _setFloat64AtTail(ByteData _buf, int tail, double x) {
413 _buf.setFloat64(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
414 }
415
386 static void _setInt32AtTail(ByteData _buf, int tail, int x) { 416 static void _setInt32AtTail(ByteData _buf, int tail, int x) {
387 _buf.setInt32(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN); 417 _buf.setInt32(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
388 } 418 }
389 419
390 static void _setUint32AtTail(ByteData _buf, int tail, int x) { 420 static void _setUint32AtTail(ByteData _buf, int tail, int x) {
391 _buf.setUint32(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN); 421 _buf.setUint32(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
392 } 422 }
423
424 static void _setUint64AtTail(ByteData _buf, int tail, int x) {
425 _buf.setUint64(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
426 }
393 } 427 }
394 428
395 /** 429 /**
430 * The reader of lists of 64-bit float values.
431 *
432 * The returned unmodifiable lists lazily read values on access.
433 */
434 class Float64ListReader extends Reader<List<double>> {
435 const Float64ListReader();
436
437 @override
438 int get size => 4;
439
440 @override
441 List<double> read(BufferPointer bp) => new _FbFloat64List(bp.derefObject());
442 }
443
444 /**
396 * The reader of 32-bit signed integers. 445 * The reader of 32-bit signed integers.
397 */ 446 */
398 class Int32Reader extends Reader<int> { 447 class Int32Reader extends Reader<int> {
399 const Int32Reader() : super(); 448 const Int32Reader() : super();
400 449
401 @override 450 @override
402 int get size => 4; 451 int get size => 4;
403 452
404 @override 453 @override
405 int read(BufferPointer bp) => bp._getInt32(); 454 int read(BufferPointer bp) => bp._getInt32();
(...skipping 20 matching lines...) Expand all
426 class ListReader<E> extends Reader<List<E>> { 475 class ListReader<E> extends Reader<List<E>> {
427 final Reader<E> _elementReader; 476 final Reader<E> _elementReader;
428 477
429 const ListReader(this._elementReader); 478 const ListReader(this._elementReader);
430 479
431 @override 480 @override
432 int get size => 4; 481 int get size => 4;
433 482
434 @override 483 @override
435 List<E> read(BufferPointer bp) => 484 List<E> read(BufferPointer bp) =>
436 new _FbList<E>(_elementReader, bp.derefObject()); 485 new _FbInt32List<E>(_elementReader, bp.derefObject());
437 } 486 }
438 487
439 /** 488 /**
440 * The offset from the end of the buffer to a serialized object of the type [T]. 489 * The offset from the end of the buffer to a serialized object of the type [T].
441 */ 490 */
442 class Offset<T> { 491 class Offset<T> {
443 final int _tail; 492 final int _tail;
444 493
445 Offset(this._tail); 494 Offset(this._tail);
446 } 495 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 */ 561 */
513 T createObject(BufferPointer bp); 562 T createObject(BufferPointer bp);
514 563
515 @override 564 @override
516 T read(BufferPointer bp) { 565 T read(BufferPointer bp) {
517 bp = bp.derefObject(); 566 bp = bp.derefObject();
518 return createObject(bp); 567 return createObject(bp);
519 } 568 }
520 } 569 }
521 570
522 class _FbList<E> extends Object with ListMixin<E> implements List<E> { 571 /**
572 * The list backed by 64-bit values - Uint64 length and Float64.
573 */
574 class _FbFloat64List extends _FbList<double> {
575 final BufferPointer bp;
576
577 int _length;
578 List<double> _items;
579
580 _FbFloat64List(this.bp);
581
582 @override
583 int get length {
584 _length ??= bp._getUint64();
585 return _length;
586 }
587
588 @override
589 double operator [](int i) {
590 _items ??= new List<double>(length);
591 double item = _items[i];
592 if (item == null) {
593 BufferPointer ref = bp._advance(8 + 8 * i);
594 item = ref._getFloat64();
595 _items[i] = item;
596 }
597 return item;
598 }
599 }
600
601 /**
602 * The list backed by 32-bit values - offsets or integers.
603 */
604 class _FbInt32List<E> extends _FbList<E> {
523 final Reader<E> elementReader; 605 final Reader<E> elementReader;
524 final BufferPointer bp; 606 final BufferPointer bp;
525 607
526 int _length; 608 int _length;
527 List<E> _items; 609 List<E> _items;
528 610
529 _FbList(this.elementReader, this.bp); 611 _FbInt32List(this.elementReader, this.bp);
530 612
531 @override 613 @override
532 int get length { 614 int get length {
533 _length ??= bp._getUint32(); 615 _length ??= bp._getUint32();
534 return _length; 616 return _length;
535 } 617 }
536 618
537 @override 619 @override
538 void set length(int i) =>
539 throw new StateError('Attempt to modify immutable list');
540
541 @override
542 E operator [](int i) { 620 E operator [](int i) {
543 _items ??= new List<E>(length); 621 _items ??= new List<E>(length);
544 E item = _items[i]; 622 E item = _items[i];
545 if (item == null) { 623 if (item == null) {
546 BufferPointer ref = bp._advance(4 + elementReader.size * i); 624 BufferPointer ref = bp._advance(4 + 4 * i);
547 item = elementReader.read(ref); 625 item = elementReader.read(ref);
548 _items[i] = item; 626 _items[i] = item;
549 } 627 }
550 return item; 628 return item;
551 } 629 }
630 }
631
632 /**
633 * An immutable list abstract list.
Paul Berry 2016/01/15 22:26:37 This comment is hard to interpret. How about some
634 */
635 abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> {
636 @override
637 void set length(int i) =>
638 throw new StateError('Attempt to modify immutable list');
552 639
553 @override 640 @override
554 void operator []=(int i, E e) => 641 void operator []=(int i, E e) =>
555 throw new StateError('Attempt to modify immutable list'); 642 throw new StateError('Attempt to modify immutable list');
556 } 643 }
557 644
558 /** 645 /**
559 * Class that describes the structure of a table. 646 * Class that describes the structure of a table.
560 */ 647 */
561 class _VTable { 648 class _VTable {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 // Table size. 709 // Table size.
623 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN); 710 buf.setUint16(bufOffset, tableSize, Endianness.LITTLE_ENDIAN);
624 bufOffset += 2; 711 bufOffset += 2;
625 // Field offsets. 712 // Field offsets.
626 for (int fieldOffset in fieldOffsets) { 713 for (int fieldOffset in fieldOffsets) {
627 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN); 714 buf.setUint16(bufOffset, fieldOffset, Endianness.LITTLE_ENDIAN);
628 bufOffset += 2; 715 bufOffset += 2;
629 } 716 }
630 } 717 }
631 } 718 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/flat_buffers_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698