Skip to content

Commit 4a3fd8e

Browse files
committed
added operator == and hashCode to BitSet
1 parent 857cb81 commit 4a3fd8e

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

lib/src/bit_array.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of bit_array;
22

33
/// Bit array to store bits.
4-
class BitArray implements BitSet {
4+
class BitArray extends BitSet {
55
Uint32List _data;
66
int _length;
77

lib/src/bit_set.dart

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ abstract class BitSet {
2424
/// Returns an iterable wrapper of the [BitSet] that iterates over the index
2525
/// members that are set to true.
2626
Iterable<int> asIntIterable();
27+
28+
@override
29+
bool operator ==(Object other) {
30+
if (identical(this, other)) {
31+
return true;
32+
}
33+
if (other is BitSet &&
34+
runtimeType == other.runtimeType &&
35+
length == other.length) {
36+
final iter = asUint32Iterable().iterator;
37+
final otherIter = other.asUint32Iterable().iterator;
38+
while (iter.moveNext() && otherIter.moveNext()) {
39+
if (iter.current != otherIter.current) {
40+
return false;
41+
}
42+
}
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
@override
49+
int get hashCode =>
50+
asUint32Iterable().fold(
51+
0, (int previousValue, element) => previousValue ^ element.hashCode) ^
52+
length.hashCode;
2753
}
2854

2955
/// Memory-efficient empty [BitSet].
@@ -53,7 +79,7 @@ class EmptySet implements BitSet {
5379
const emptyBitSet = EmptySet();
5480

5581
/// A list-based [BitSet] implementation.
56-
class ListSet implements BitSet {
82+
class ListSet extends BitSet {
5783
final List<int> _list;
5884

5985
ListSet.fromSorted(this._list);
@@ -102,7 +128,7 @@ class ListSet implements BitSet {
102128
}
103129

104130
/// A range-based [BitSet] implementation.
105-
class RangeSet implements BitSet {
131+
class RangeSet extends BitSet {
106132
final List<int> _list;
107133

108134
RangeSet.fromSortedRangeLength(this._list);

lib/src/composite_set.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BitSetChunk {
2828
///
2929
/// By default, each chunk is using a maximum cardinality of 2^16 entries,
3030
/// following the RoaringBitmap pattern.
31-
class CompositeSet implements BitSet {
31+
class CompositeSet extends BitSet {
3232
/// The bits used for each chunk.
3333
final int chunkBits;
3434

test/bit_array_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ void main() {
8888
});
8989
});
9090

91+
group('BitArray equals and hashCode', () {
92+
final oiii = BitArray(32)..setBits([1, 2, 3]);
93+
final oioi = BitArray(32)..setBits([1, 3]);
94+
final ooio = BitArray(32)..setBits([2]);
95+
96+
test('equals', () {
97+
expect(oiii ^ oioi, equals(ooio));
98+
});
99+
100+
test('not equals', () {
101+
expect(oiii, isNot(ooio));
102+
});
103+
104+
test('hashCode', () {
105+
expect((oiii ^ oioi).hashCode, equals(ooio.hashCode));
106+
});
107+
});
108+
91109
group('BitArray + BitSet', () {
92110
final array = BitArray(128)..setBits([13, 113]);
93111
final list = ListSet.fromSorted([13, 33]);

test/bit_set_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ void main() {
5757
});
5858
});
5959

60+
group('ListSet equals and hashCode', () {
61+
test('equals', () {
62+
expect(ListSet.fromSorted([1, 3]), equals(ListSet.fromSorted([1, 3])));
63+
});
64+
65+
test('not equals', () {
66+
expect(ListSet.fromSorted([1, 3]), isNot(ListSet.fromSorted([1, 2])));
67+
});
68+
69+
test('hashCode', () {
70+
expect(ListSet.fromSorted([1, 3]).hashCode,
71+
equals(ListSet.fromSorted([1, 3]).hashCode));
72+
});
73+
});
74+
6075
group('RangeSet', () {
6176
final set = RangeSet.fromSortedRangeLength([2, 0, 6, 3, 15, 2, 21, 4]);
6277

@@ -94,6 +109,23 @@ void main() {
94109
]);
95110
});
96111
});
112+
113+
group('RangeSet equals and hashCode', () {
114+
test('equals', () {
115+
expect(RangeSet.fromSortedRangeLength([1, 3]),
116+
equals(RangeSet.fromSortedRangeLength([1, 2, 4, 0])));
117+
});
118+
119+
test('not equals', () {
120+
expect(RangeSet.fromSortedRangeLength([1, 3]),
121+
isNot(RangeSet.fromSortedRangeLength([1, 2])));
122+
});
123+
124+
test('hashCode', () {
125+
expect(RangeSet.fromSortedRangeLength([1, 3]).hashCode,
126+
equals(RangeSet.fromSortedRangeLength([1, 2, 4, 0]).hashCode));
127+
});
128+
});
97129
}
98130

99131
String _rev(String s) => String.fromCharCodes(s.codeUnits.reversed);

test/composite_set_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,33 @@ void main() {
3939
expect(set.asIntIterable().toList(), [65537]);
4040
});
4141
});
42+
43+
group('CompositeSet equals and hashCode', () {
44+
final oiii = CompositeSet();
45+
final oioi = CompositeSet();
46+
final ooio = CompositeSet();
47+
48+
setUp(() {
49+
oiii[1] = true;
50+
oiii[2] = true;
51+
oiii[3] = true;
52+
53+
oioi[1] = true;
54+
oioi[3] = true;
55+
56+
ooio[2] = true;
57+
});
58+
59+
test('equals', () {
60+
expect(oioi..or(ooio), equals(oiii));
61+
});
62+
63+
test('not equals', () {
64+
expect(oiii, isNot(ooio));
65+
});
66+
67+
test('hashCode', () {
68+
expect((oioi..or(ooio)).hashCode, equals(oiii.hashCode));
69+
});
70+
});
4271
}

0 commit comments

Comments
 (0)