Skip to content

Commit ab0b34f

Browse files
committed
BitArray.setWhere and clearWhere.
1 parent 79a4698 commit ab0b34f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 2.3.0
22

33
- Updated SDK constraint, lints and format.
4+
- `BitArray.setWhere` and `BitArray.clearWhere` for callback-driven updates.
45

56
## 2.2.1
67

lib/src/bit_array.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ class BitArray extends BitSet {
121121
indexes.forEach(clearBit);
122122
}
123123

124+
/// Sets the bits where the [fn] returns true, to false.
125+
///
126+
/// Calls [fn] only for index values where the bit is true.
127+
void clearWhere(bool Function(int index) fn) {
128+
var index = 0;
129+
for (var i = 0; i < _data.length; i++) {
130+
var d = _data[i];
131+
if (d == 0) {
132+
index += 32;
133+
continue;
134+
}
135+
for (var j = 0; j < _bitMask.length; j++) {
136+
final bm = _bitMask[j];
137+
if ((d & bm) != 0 && fn(index)) {
138+
d = d & _clearMask[j];
139+
}
140+
index++;
141+
}
142+
_data[i] = d;
143+
}
144+
}
145+
124146
/// Sets all of the bits in the current [BitArray] to false.
125147
void clearAll() {
126148
for (var i = 0; i < _data.length; i++) {
@@ -138,6 +160,28 @@ class BitArray extends BitSet {
138160
indexes.forEach(setBit);
139161
}
140162

163+
/// Sets the bits where the [fn] returns true, to true.
164+
///
165+
/// Calls [fn] only for index values where the bit is false.
166+
void setWhere(bool Function(int index) fn) {
167+
var index = 0;
168+
for (var i = 0; i < _data.length; i++) {
169+
var d = _data[i];
170+
if (d == -1) {
171+
index += 32;
172+
continue;
173+
}
174+
for (var j = 0; j < _bitMask.length; j++) {
175+
final bm = _bitMask[j];
176+
if ((d & bm) == 0 && fn(index)) {
177+
d = d | bm;
178+
}
179+
index++;
180+
}
181+
_data[i] = d;
182+
}
183+
}
184+
141185
/// Sets all the bit values in the current [BitArray] to true.
142186
void setAll() {
143187
for (var i = 0; i < _data.length; i++) {

test/bit_array_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,59 @@ void main() {
135135
expect((oioi ^ oooo).toBinaryString().substring(0, 4), '0101');
136136
expect((oioi ^ ioio).toBinaryString().substring(0, 4), '1111');
137137
});
138+
139+
test('setWhere calling one', () {
140+
for (var i = 0; i < 128; i++) {
141+
final array = BitArray(128);
142+
array.setAll();
143+
array.clearBit(i);
144+
final called = [];
145+
array.setWhere((i) {
146+
called.add(i);
147+
return false;
148+
});
149+
expect(called, [i]);
150+
for (var j = 0; j < 128; j++) {
151+
expect(array[j], j != i);
152+
}
153+
154+
called.clear();
155+
array.setWhere((i) {
156+
called.add(i);
157+
return true;
158+
});
159+
expect(called, [i]);
160+
for (var j = 0; j < 128; j++) {
161+
expect(array[j], true);
162+
}
163+
}
164+
});
165+
166+
test('clearWhere calling one', () {
167+
for (var i = 0; i < 128; i++) {
168+
final array = BitArray(128);
169+
array.setBit(i);
170+
final called = [];
171+
array.clearWhere((i) {
172+
called.add(i);
173+
return false;
174+
});
175+
expect(called, [i]);
176+
for (var j = 0; j < 128; j++) {
177+
expect(array[j], j == i);
178+
}
179+
180+
called.clear();
181+
array.clearWhere((i) {
182+
called.add(i);
183+
return true;
184+
});
185+
expect(called, [i]);
186+
for (var j = 0; j < 128; j++) {
187+
expect(array[j], false);
188+
}
189+
}
190+
});
138191
});
139192

140193
group('BitArray equals and hashCode', () {

0 commit comments

Comments
 (0)