Skip to content

Commit 29b8869

Browse files
kevmoolrhn
authored andcommitted
[collection] explicitly make BoolList abstract interface (#875)
It couldn't be extended in practice since it doesn't have a public constructor Also made the only implementations final
1 parent 970aa9a commit 29b8869

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

pkgs/collection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Optimize equality and hash code for maps by using `update` and a `values`
77
iterator to avoid extra lookups.
88
- Add `PriorityQueue.of` constructor and optimize adding many elements.
9+
- Explicitly mark `BoolList` as `abstract interface`
910
- Address diagnostics from `strict_top_level_inference`.
1011

1112
## 1.19.1

pkgs/collection/lib/src/boollist.dart

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
import 'dart:collection' show ListMixin;
66
import 'dart:typed_data' show Uint32List;
77

8+
import 'package:meta/meta.dart';
9+
810
import 'unmodifiable_wrappers.dart' show NonGrowableListMixin;
911

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

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

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

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

181+
// Note: [index] is NOT checked for validity.
182+
@pragma('dart2js:prefer-inline')
183+
@pragma('vm:prefer-inline')
184+
@pragma('wasm:prefer-inline')
185+
bool _getBit(int index) =>
186+
(_data[index >> _entryShift] & (1 << (index & _entrySignBitIndex))) != 0;
187+
178188
static int _lengthInWords(int bitLength) {
179189
return (bitLength + (_bitsPerEntry - 1)) >> _entryShift;
180190
}
181191
}
182192

183-
class _GrowableBoolList extends BoolList {
193+
final class _GrowableBoolList extends BoolList {
184194
static const int _growthFactor = 2;
185195

186196
_GrowableBoolList._withCapacity(int length, int capacity)
@@ -228,7 +238,8 @@ class _GrowableBoolList extends BoolList {
228238
}
229239
}
230240

231-
class _NonGrowableBoolList extends BoolList with NonGrowableListMixin<bool> {
241+
final class _NonGrowableBoolList extends BoolList
242+
with NonGrowableListMixin<bool> {
232243
_NonGrowableBoolList._withCapacity(int length, int capacity)
233244
: super._(
234245
Uint32List(BoolList._lengthInWords(capacity)),
@@ -262,9 +273,7 @@ class _BoolListIterator implements Iterator<bool> {
262273

263274
if (_pos < _boolList.length) {
264275
var pos = _pos++;
265-
_current = _boolList._data[pos >> BoolList._entryShift] &
266-
(1 << (pos & BoolList._entrySignBitIndex)) !=
267-
0;
276+
_current = _boolList._getBit(pos);
268277
return true;
269278
}
270279
_current = false;

pkgs/collection/pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ topics:
1212
environment:
1313
sdk: ^3.4.0
1414

15+
dependencies:
16+
meta: ^1.16.0
17+
1518
dev_dependencies:
1619
benchmark_harness: ^2.3.1
1720
dart_flutter_team_lints: ^3.0.0

0 commit comments

Comments
 (0)