Add changelog entry for BoolList. (#210)
* Add changelog entry for BoolList.
* Add author to AUTHORS.
* Small cleanups.
* Rename `.from` constructor to `.of`, for consistency.
* Still on 1.16.0-dev.
diff --git a/AUTHORS b/AUTHORS
index 76494b6..80712fd 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -4,4 +4,5 @@
# Name/Organization <email address>
Google Inc.
+AAABramenko (https://github.com/AAAbramenko)
TimWhiting tim@whitings.org
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 85f17ee..f52dcc7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
## 1.16.0-dev
+* Add `BoolList` class for space-efficient lists of boolean values.
* Use a stable sort algorithm in the `IterableExtension.sortedBy` method.
* Add `min`, `max`, `minOrNull` and `maxOrNull` getters to `IterableDoubleExtension`, `IterableNumberExtension` and `IterableIntegerExtension`
diff --git a/lib/src/boollist.dart b/lib/src/boollist.dart
index cf23b57..5f9dba3 100644
--- a/lib/src/boollist.dart
+++ b/lib/src/boollist.dart
@@ -1,4 +1,4 @@
-// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@@ -17,8 +17,12 @@
static const int _entrySignBitIndex = 31;
+ /// The length of the list.
+ ///
+ /// Maybe be shorter than the capacity of the backing store.
int _length;
+ /// Backing store for bits.
Uint32List _data;
BoolList._(this._data, this._length);
@@ -94,10 +98,20 @@
///
/// This constructor creates a growable [BoolList] when [growable] is true;
/// otherwise, it returns a fixed-length list.
- factory BoolList.from(Iterable<bool> elements, {bool growable = false}) {
+ factory BoolList.of(Iterable<bool> elements, {bool growable = false}) {
return BoolList._selectType(elements.length, growable)..setAll(0, elements);
}
+ /// The number of boolean values in this list.
+ ///
+ /// The valid indices for a list are `0` through `length - 1`.
+ ///
+ /// If the list is growable, setting the length will change the
+ /// number of values.
+ /// Setting the length to a smaller number will remove all
+ /// values with indices greater than or equal to the new length.
+ /// Setting the length to a larger number will increase the number of
+ /// values, and all the new values will be `false`.
@override
int get length => _length;
@@ -110,9 +124,9 @@
}
@override
- void operator []=(int index, bool val) {
+ void operator []=(int index, bool value) {
RangeError.checkValidIndex(index, this, 'index', _length);
- _setBit(index, val);
+ _setBit(index, value);
}
@override
@@ -145,23 +159,23 @@
}
}
- /// Returns custom iterator for [BoolList].
+ /// Creates an iterator for the elements of this [BoolList].
///
- /// To provide null safety [Iterator.current] getter of returned iterator
- /// returns `false` before and after iteration process.
+ /// The [Iterator.current] getter of the returned iterator
+ /// is `false` when the iterator has no current element.
@override
Iterator<bool> get iterator => _BoolListIterator(this);
- void _setBit(int index, bool val) {
- if (val) {
+ void _setBit(int index, bool value) {
+ if (value) {
_data[index >> _entryShift] |= 1 << (index & _entrySignBitIndex);
} else {
_data[index >> _entryShift] &= ~(1 << (index & _entrySignBitIndex));
}
}
- static int _lengthInWords(int bitsLength) {
- return (bitsLength + (_bitsPerEntry - 1)) >> _entryShift;
+ static int _lengthInWords(int bitLength) {
+ return (bitLength + (_bitsPerEntry - 1)) >> _entryShift;
}
}
@@ -194,7 +208,7 @@
if (length > _data.length * BoolList._bitsPerEntry) {
_data = Uint32List(
BoolList._lengthInWords(length * _growthFactor),
- )..setAll(0, _data);
+ )..setRange(0, _data.length, _data);
}
_length = length;
}
diff --git a/test/boollist_test.dart b/test/boollist_test.dart
index 263186c..0027ba7 100644
--- a/test/boollist_test.dart
+++ b/test/boollist_test.dart
@@ -1,4 +1,4 @@
-// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@@ -34,9 +34,9 @@
);
});
- test('BoolList.from()', () {
+ test('BoolList.of()', () {
var src = List.generate(1024, generator);
- expect(BoolList.from(src), src);
+ expect(BoolList.of(src), src);
});
group('[], []=', () {