Skip to content

[collection] explicitly make BoolList abstract interface #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/collection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- Add `IterableMapEntryExtension` for working on `Map` as a list of pairs, using
`Map.entries`.
- Explicitly mark `BoolList` as `abstract interface`
- Address diagnostics from `strict_top_level_inference`.

## 1.19.1
Expand Down
27 changes: 18 additions & 9 deletions pkgs/collection/lib/src/boollist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import 'dart:collection' show ListMixin;
import 'dart:typed_data' show Uint32List;

import 'package:meta/meta.dart';

import 'unmodifiable_wrappers.dart' show NonGrowableListMixin;

/// A space-efficient list of boolean values.
///
/// Uses list of integers as internal storage to reduce memory usage.
abstract /*mixin*/ class BoolList with ListMixin<bool> {
@sealed
// TODO: replace `interface` with `final` in the next major release.
abstract interface class BoolList with ListMixin<bool> {
static const int _entryShift = 5;

static const int _bitsPerEntry = 32;
Expand Down Expand Up @@ -119,9 +123,7 @@ abstract /*mixin*/ class BoolList with ListMixin<bool> {
@override
bool operator [](int index) {
RangeError.checkValidIndex(index, this, 'index', _length);
return (_data[index >> _entryShift] &
(1 << (index & _entrySignBitIndex))) !=
0;
return _getBit(index);
}

@override
Expand Down Expand Up @@ -167,6 +169,7 @@ abstract /*mixin*/ class BoolList with ListMixin<bool> {
@override
Iterator<bool> get iterator => _BoolListIterator(this);

// Note: [index] is NOT checked for validity.
void _setBit(int index, bool value) {
if (value) {
_data[index >> _entryShift] |= 1 << (index & _entrySignBitIndex);
Expand All @@ -175,12 +178,19 @@ abstract /*mixin*/ class BoolList with ListMixin<bool> {
}
}

// Note: [index] is NOT checked for validity.
@pragma('dart2js:prefer-inline')
@pragma('vm:prefer-inline')
@pragma('wasm:prefer-inline')
bool _getBit(int index) =>
(_data[index >> _entryShift] & (1 << (index & _entrySignBitIndex))) != 0;

static int _lengthInWords(int bitLength) {
return (bitLength + (_bitsPerEntry - 1)) >> _entryShift;
}
}

class _GrowableBoolList extends BoolList {
final class _GrowableBoolList extends BoolList {
static const int _growthFactor = 2;

_GrowableBoolList._withCapacity(int length, int capacity)
Expand Down Expand Up @@ -228,7 +238,8 @@ class _GrowableBoolList extends BoolList {
}
}

class _NonGrowableBoolList extends BoolList with NonGrowableListMixin<bool> {
final class _NonGrowableBoolList extends BoolList
with NonGrowableListMixin<bool> {
_NonGrowableBoolList._withCapacity(int length, int capacity)
: super._(
Uint32List(BoolList._lengthInWords(capacity)),
Expand Down Expand Up @@ -262,9 +273,7 @@ class _BoolListIterator implements Iterator<bool> {

if (_pos < _boolList.length) {
var pos = _pos++;
_current = _boolList._data[pos >> BoolList._entryShift] &
(1 << (pos & BoolList._entrySignBitIndex)) !=
0;
_current = _boolList._getBit(pos);
return true;
}
_current = false;
Expand Down
3 changes: 3 additions & 0 deletions pkgs/collection/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ topics:
environment:
sdk: ^3.4.0

dependencies:
meta: ^1.16.0

dev_dependencies:
benchmark_harness: ^2.3.1
dart_flutter_team_lints: ^3.0.0
Expand Down