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 1 commit
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
Prev Previous commit
Next Next commit
go with @Sealed for BoolList for now
  • Loading branch information
kevmoo committed Apr 2, 2025
commit c0d8bf81f473f33aa94787518b355400c0a3b678
17 changes: 11 additions & 6 deletions pkgs/collection/lib/src/boollist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
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.
@sealed
// TODO: replace `interface` with `final` in the next major release.
abstract interface class BoolList with ListMixin<bool> {
static const int _entryShift = 5;

Expand Down Expand Up @@ -119,9 +123,7 @@ abstract interface 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 interface 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,6 +178,10 @@ abstract interface class BoolList with ListMixin<bool> {
}
}

/// Note: [index] is NOT checked for validity.
bool _getBit(int index) =>
(_data[index >> _entryShift] & (1 << (index & _entrySignBitIndex))) != 0;

static int _lengthInWords(int bitLength) {
return (bitLength + (_bitsPerEntry - 1)) >> _entryShift;
}
Expand Down Expand Up @@ -263,9 +270,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