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

Unified Diff: pkg/analyzer/lib/src/summary/flat_buffers.dart

Issue 1747663002: Support for boolean lists in FlatBuffer. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Rewrite using Uint8List and padding. Created 4 years, 10 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 65a786a650a19879153a7cf6175cb0b0b50af0d5..e228a6080cb901c4dd56e0e5f27d5489a8e6bedd 100644
--- a/pkg/analyzer/lib/src/summary/flat_buffers.dart
+++ b/pkg/analyzer/lib/src/summary/flat_buffers.dart
@@ -10,6 +10,21 @@ import 'dart:math';
import 'dart:typed_data';
/**
+ * Reader of lists of boolean values.
+ *
+ * The returned unmodifiable lists lazily read values on access.
+ */
+class BoolListReader extends Reader<List<bool>> {
+ const BoolListReader();
+
+ @override
+ int get size => 4;
+
+ @override
+ List<bool> read(BufferPointer bp) => new _FbBoolList(bp.derefObject());
+}
+
+/**
* The reader of booleans.
*/
class BoolReader extends Reader<bool> {
@@ -335,6 +350,45 @@ class Builder {
}
/**
+ * Write the given list of boolean [values].
+ */
+ Offset writeListBool(List<bool> values) {
+ int bitLength = values.length;
+ int padding;
+ int byteLength;
+ if (bitLength % 8 == 0) {
Paul Berry 2016/02/29 22:36:12 Consider replacing this if/else with: int padding
scheglov 2016/03/01 02:06:35 Done. Thank you!
+ padding = 0;
+ byteLength = bitLength ~/ 8;
+ } else {
+ padding = 8 - bitLength % 8;
+ byteLength = bitLength ~/ 8 + 1;
+ }
+ // Prepare the backing Uint8List.
+ Uint8List bytes = new Uint8List(byteLength + 1);
+ // Record every bit.
+ int byteIndex = 0;
+ int byte = 0;
+ int mask = 1;
+ for (int bitIndex = 0; bitIndex < bitLength; bitIndex++) {
+ if (bitIndex != 0 && (bitIndex % 8 == 0)) {
+ bytes[byteIndex++] = byte;
+ byte = 0;
+ mask = 1;
+ }
+ if (values[bitIndex]) {
+ byte |= mask;
+ }
+ mask <<= 1;
+ }
+ // Write the last byte, even if it may be on the padding.
+ bytes[byteIndex] = byte;
+ // Write the padding length.
+ bytes[byteLength] = padding;
+ // Write as a Uint8 list.
+ return writeListUint8(bytes);
+ }
+
+ /**
* Write the given list of 64-bit float [values].
*/
Offset writeListFloat64(List<double> values) {
@@ -645,7 +699,7 @@ abstract class TableReader<T> extends Reader<T> {
}
/**
- * Reader of lists of 32-bit float values.
+ * Reader of lists of unsigned 32-bit integer values.
*
* The returned unmodifiable lists lazily read values on access.
*/
@@ -686,6 +740,40 @@ class Uint8Reader extends Reader<int> {
}
/**
+ * List of booleans backed by 8-bit unsigned integers.
+ */
+class _FbBoolList extends Object with ListMixin<bool> implements List<bool> {
+ final List<int> uint8List;
+ int _length;
+
+ _FbBoolList(BufferPointer bp)
+ : uint8List = new _FbGenericList<int>(const Uint8Reader(), bp);
+
+ @override
+ int get length {
+ if (_length == null) {
+ _length = (uint8List.length - 1) * 8 - uint8List.last;
+ }
+ return _length;
+ }
+
+ @override
+ void set length(int i) =>
+ throw new StateError('Attempt to modify immutable list');
+
+ @override
+ bool operator [](int i) {
+ int index = i ~/ 8;
+ int mask = 1 << i % 8;
+ return uint8List[index] & mask != 0;
+ }
+
+ @override
+ void operator []=(int i, bool e) =>
+ throw new StateError('Attempt to modify immutable list');
+}
+
+/**
* The list backed by 64-bit values - Uint64 length and Float64.
*/
class _FbFloat64List extends _FbList<double> {
« 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