1
1
part of bit_array;
2
2
3
3
/// Bit array to store bits.
4
- class BitArray {
4
+ class BitArray implements BitSet {
5
5
Uint64List _data;
6
6
int _length;
7
7
@@ -19,7 +19,17 @@ class BitArray {
19
19
return new BitArray ._(data);
20
20
}
21
21
22
+ /// Creates a bit array using a generic bit set.
23
+ factory BitArray .fromBitSet (BitSet set , {int length}) {
24
+ length ?? = set .length;
25
+ final setDataLength = _bufferLength64 (set .length);
26
+ final data = new Uint64List (_bufferLength64 (length));
27
+ data.setRange (0 , setDataLength, set .asUint64Iterable ());
28
+ return new BitArray ._(data);
29
+ }
30
+
22
31
/// The value of the bit with the specified [index] .
32
+ @override
23
33
bool operator [](int index) {
24
34
return (_data[index >> 6 ] & _bitMask[index & 0x3f ]) != 0 ;
25
35
}
@@ -50,6 +60,7 @@ class BitArray {
50
60
}
51
61
52
62
/// The number of bits set to true.
63
+ @override
53
64
int get cardinality => _data.buffer
54
65
.asUint8List ()
55
66
.fold (0 , (sum, value) => sum + _cardinalityBitCounts[value]);
@@ -106,70 +117,76 @@ class BitArray {
106
117
}
107
118
108
119
/// Update the current [BitArray] using a logical AND operation with the
109
- /// corresponding elements in the specified [array] .
110
- /// Excess size of the [array] is ignored.
111
- void and (BitArray array) {
112
- final minLength = math.min (_data.length, array._data.length);
113
- for (int i = 0 ; i < minLength; i++ ) {
114
- _data[i] & = array._data[i];
120
+ /// corresponding elements in the specified [set] .
121
+ /// Excess size of the [set] is ignored.
122
+ void and (BitSet set ) {
123
+ final iter = set .asUint64Iterable ().iterator;
124
+ int i = 0 ;
125
+ for (; i < _data.length && iter.moveNext (); i++ ) {
126
+ _data[i] & = iter.current;
115
127
}
116
- for (int i = minLength ; i < _data.length; i++ ) {
128
+ for (; i < _data.length; i++ ) {
117
129
_data[i] = 0 ;
118
130
}
119
131
}
120
132
121
133
/// Update the current [BitArray] using a logical AND NOT operation with the
122
- /// corresponding elements in the specified [array ] .
123
- /// Excess size of the [array ] is ignored.
124
- void andNot (BitArray array ) {
125
- final minLength = math. min (_data.length, array._data.length) ;
126
- for (int i = 0 ; i < minLength ; i++ ) {
127
- _data[i] & = ~ array._data[i] ;
134
+ /// corresponding elements in the specified [set ] .
135
+ /// Excess size of the [set ] is ignored.
136
+ void andNot (BitSet set ) {
137
+ final iter = set . asUint64Iterable ().iterator ;
138
+ for (int i = 0 ; i < _data.length && iter. moveNext () ; i++ ) {
139
+ _data[i] & = ~ iter.current ;
128
140
}
129
141
}
130
142
131
143
/// Update the current [BitArray] using a logical OR operation with the
132
- /// corresponding elements in the specified [array ] .
133
- /// Excess size of the [array ] is ignored.
134
- void or (BitArray array ) {
135
- final minLength = math. min (_data.length, array._data.length) ;
136
- for (int i = 0 ; i < minLength ; i++ ) {
137
- _data[i] | = array._data[i] ;
144
+ /// corresponding elements in the specified [set ] .
145
+ /// Excess size of the [set ] is ignored.
146
+ void or (BitSet set ) {
147
+ final iter = set . asUint64Iterable ().iterator ;
148
+ for (int i = 0 ; i < _data.length && iter. moveNext () ; i++ ) {
149
+ _data[i] | = iter.current ;
138
150
}
139
151
}
140
152
141
153
/// Update the current [BitArray] using a logical XOR operation with the
142
- /// corresponding elements in the specified [array ] .
143
- /// Excess size of the [array ] is ignored.
144
- void xor (BitArray array ) {
145
- final minLength = math. min (_data.length, array._data.length) ;
146
- for (int i = 0 ; i < minLength ; i++ ) {
147
- _data[i] = _data[i] ^ array._data[i] ;
154
+ /// corresponding elements in the specified [set ] .
155
+ /// Excess size of the [set ] is ignored.
156
+ void xor (BitSet set ) {
157
+ final iter = set . asUint64Iterable ().iterator ;
158
+ for (int i = 0 ; i < _data.length && iter. moveNext () ; i++ ) {
159
+ _data[i] = _data[i] ^ iter.current ;
148
160
}
149
161
}
150
162
151
163
/// Creates a copy of the current [BitArray] .
164
+ @override
152
165
BitArray clone () {
153
166
final newData = new Uint64List (_data.length);
154
167
newData.setRange (0 , _data.length, _data);
155
168
return new BitArray ._(newData);
156
169
}
157
170
158
171
/// Creates a new [BitArray] using a logical AND operation with the
159
- /// corresponding elements in the specified [array] .
160
- BitArray operator & (BitArray array) => clone ()..and (array);
172
+ /// corresponding elements in the specified [set] .
173
+ /// Excess size of the [set] is ignored.
174
+ BitArray operator & (BitSet set ) => clone ()..and (set );
161
175
162
176
/// Creates a new [BitArray] using a logical AND NOT operation with the
163
- /// corresponding elements in the specified [array] .
164
- BitArray operator % (BitArray array) => clone ()..andNot (array);
177
+ /// corresponding elements in the specified [set] .
178
+ /// Excess size of the [set] is ignored.
179
+ BitArray operator % (BitSet set ) => clone ()..andNot (set );
165
180
166
181
/// Creates a new [BitArray] using a logical OR operation with the
167
- /// corresponding elements in the specified [array] .
168
- BitArray operator | (BitArray array) => clone ()..or (array);
182
+ /// corresponding elements in the specified [set] .
183
+ /// Excess size of the [set] is ignored.
184
+ BitArray operator | (BitSet set ) => clone ()..or (set );
169
185
170
186
/// Creates a new [BitArray] using a logical XOR operation with the
171
- /// corresponding elements in the specified [array] .
172
- BitArray operator ^ (BitArray array) => clone ()..xor (array);
187
+ /// corresponding elements in the specified [set] .
188
+ /// Excess size of the [set] is ignored.
189
+ BitArray operator ^ (BitSet set ) => clone ()..xor (set );
173
190
174
191
/// Creates a string of 0s and 1s of the content of the array.
175
192
String toBinaryString () {
@@ -186,10 +203,12 @@ class BitArray {
186
203
187
204
/// Returns an iterable wrapper of the bit array that iterates over the index
188
205
/// numbers and returns the 64-bit int blocks.
206
+ @override
189
207
Iterable <int > asUint64Iterable () => _data;
190
208
191
209
/// Returns an iterable wrapper of the bit array that iterates over the index
192
210
/// numbers that match [value] (by default the bits that are set).
211
+ @override
193
212
Iterable <int > asIntIterable ([bool value = true ]) {
194
213
return new _IntIterable (this , value);
195
214
}
0 commit comments