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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/summary/flat_buffers.dart
diff --git a/pkg/analyzer/lib/src/summary/flat_buffers.dart b/pkg/analyzer/lib/src/summary/flat_buffers.dart
index 3c7de13ec9a713ccc5619705dfe72cb07b645810..6aa34a3aff5e9aab1a8a76f12b747f902eca3908 100644
--- a/pkg/analyzer/lib/src/summary/flat_buffers.dart
+++ b/pkg/analyzer/lib/src/summary/flat_buffers.dart
@@ -49,6 +49,9 @@ class BufferPointer {
return new BufferPointer._(_buffer, _offset + delta);
}
+ double _getFloat64([int delta = 0]) =>
+ _buffer.getFloat64(_offset + delta, Endianness.LITTLE_ENDIAN);
+
int _getInt32([int delta = 0]) =>
_buffer.getInt32(_offset + delta, Endianness.LITTLE_ENDIAN);
@@ -60,6 +63,9 @@ class BufferPointer {
int _getUint32([int delta = 0]) =>
_buffer.getUint32(_offset + delta, Endianness.LITTLE_ENDIAN);
+ int _getUint64([int delta = 0]) =>
+ _buffer.getUint64(_offset + delta, Endianness.LITTLE_ENDIAN);
+
/**
* If the [byteList] is already a [Uint8List] return it.
* Otherwise return a [Uint8List] copy of the [byteList].
@@ -299,6 +305,26 @@ class Builder {
}
/**
+ * Write the given list of 64-bit float [values].
+ */
+ Offset writeListFloat64(List<double> values) {
+ if (_currentVTable != null) {
+ throw new StateError(
+ 'Cannot write a non-scalar value while writing a table.');
+ }
+ _prepare(8, 1 + values.length);
+ Offset result = new Offset(_tail);
+ int tail = _tail;
+ _setUint64AtTail(_buf, tail, values.length);
+ tail -= 8;
+ for (double value in values) {
+ _setFloat64AtTail(_buf, tail, value);
+ tail -= 8;
+ }
+ return result;
+ }
+
+ /**
* Write the given list of signed 32-bit integer [values].
*/
Offset writeListInt32(List<int> values) {
@@ -383,6 +409,10 @@ class Builder {
_currentVTable.addField(field, _tail);
}
+ static void _setFloat64AtTail(ByteData _buf, int tail, double x) {
+ _buf.setFloat64(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
+ }
+
static void _setInt32AtTail(ByteData _buf, int tail, int x) {
_buf.setInt32(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
}
@@ -390,6 +420,25 @@ class Builder {
static void _setUint32AtTail(ByteData _buf, int tail, int x) {
_buf.setUint32(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
}
+
+ static void _setUint64AtTail(ByteData _buf, int tail, int x) {
+ _buf.setUint64(_buf.lengthInBytes - tail, x, Endianness.LITTLE_ENDIAN);
+ }
+}
+
+/**
+ * The reader of lists of 64-bit float values.
+ *
+ * The returned unmodifiable lists lazily read values on access.
+ */
+class Float64ListReader extends Reader<List<double>> {
+ const Float64ListReader();
+
+ @override
+ int get size => 4;
+
+ @override
+ List<double> read(BufferPointer bp) => new _FbFloat64List(bp.derefObject());
}
/**
@@ -433,7 +482,7 @@ class ListReader<E> extends Reader<List<E>> {
@override
List<E> read(BufferPointer bp) =>
- new _FbList<E>(_elementReader, bp.derefObject());
+ new _FbInt32List<E>(_elementReader, bp.derefObject());
}
/**
@@ -519,14 +568,47 @@ abstract class TableReader<T> extends Reader<T> {
}
}
-class _FbList<E> extends Object with ListMixin<E> implements List<E> {
+/**
+ * The list backed by 64-bit values - Uint64 length and Float64.
+ */
+class _FbFloat64List extends _FbList<double> {
+ final BufferPointer bp;
+
+ int _length;
+ List<double> _items;
+
+ _FbFloat64List(this.bp);
+
+ @override
+ int get length {
+ _length ??= bp._getUint64();
+ return _length;
+ }
+
+ @override
+ double operator [](int i) {
+ _items ??= new List<double>(length);
+ double item = _items[i];
+ if (item == null) {
+ BufferPointer ref = bp._advance(8 + 8 * i);
+ item = ref._getFloat64();
+ _items[i] = item;
+ }
+ return item;
+ }
+}
+
+/**
+ * The list backed by 32-bit values - offsets or integers.
+ */
+class _FbInt32List<E> extends _FbList<E> {
final Reader<E> elementReader;
final BufferPointer bp;
int _length;
List<E> _items;
- _FbList(this.elementReader, this.bp);
+ _FbInt32List(this.elementReader, this.bp);
@override
int get length {
@@ -535,20 +617,25 @@ class _FbList<E> extends Object with ListMixin<E> implements List<E> {
}
@override
- void set length(int i) =>
- throw new StateError('Attempt to modify immutable list');
-
- @override
E operator [](int i) {
_items ??= new List<E>(length);
E item = _items[i];
if (item == null) {
- BufferPointer ref = bp._advance(4 + elementReader.size * i);
+ BufferPointer ref = bp._advance(4 + 4 * i);
item = elementReader.read(ref);
_items[i] = item;
}
return item;
}
+}
+
+/**
+ * An immutable list abstract list.
Paul Berry 2016/01/15 22:26:37 This comment is hard to interpret. How about some
+ */
+abstract class _FbList<E> extends Object with ListMixin<E> implements List<E> {
+ @override
+ void set length(int i) =>
+ throw new StateError('Attempt to modify immutable list');
@override
void operator []=(int i, E e) =>
« 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