File tree Expand file tree Collapse file tree 3 files changed +98
-0
lines changed Expand file tree Collapse file tree 3 files changed +98
-0
lines changed Original file line number Diff line number Diff line change 1
1
## 2.3.0
2
2
3
3
- Updated SDK constraint, lints and format.
4
+ - ` BitArray.setWhere ` and ` BitArray.clearWhere ` for callback-driven updates.
4
5
5
6
## 2.2.1
6
7
Original file line number Diff line number Diff line change @@ -121,6 +121,28 @@ class BitArray extends BitSet {
121
121
indexes.forEach (clearBit);
122
122
}
123
123
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
+
124
146
/// Sets all of the bits in the current [BitArray] to false.
125
147
void clearAll () {
126
148
for (var i = 0 ; i < _data.length; i++ ) {
@@ -138,6 +160,28 @@ class BitArray extends BitSet {
138
160
indexes.forEach (setBit);
139
161
}
140
162
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
+
141
185
/// Sets all the bit values in the current [BitArray] to true.
142
186
void setAll () {
143
187
for (var i = 0 ; i < _data.length; i++ ) {
Original file line number Diff line number Diff line change @@ -135,6 +135,59 @@ void main() {
135
135
expect ((oioi ^ oooo).toBinaryString ().substring (0 , 4 ), '0101' );
136
136
expect ((oioi ^ ioio).toBinaryString ().substring (0 , 4 ), '1111' );
137
137
});
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
+ });
138
191
});
139
192
140
193
group ('BitArray equals and hashCode' , () {
You can’t perform that action at this time.
0 commit comments