-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseBitset.java
66 lines (52 loc) · 1.96 KB
/
SparseBitset.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.BitSet;
// Sparse Bitsets: Sparse bitsets are used to represent a large set of integers efficiently, where most of the integers are not present.
// Bitwise operations are used to represent and manipulate sparse sets with minimal memory usage.
public class SparseBitset {
private final BitSet bitSet;
// Constructor to initialize the sparse bitset
public SparseBitset() {
bitSet = new BitSet();
}
// Add an integer to the sparse bitset
public void add(int num) {
bitSet.set(num);
}
// Remove an integer from the sparse bitset
public void remove(int num) {
bitSet.clear(num);
}
// Check if an integer is present in the sparse bitset
public boolean contains(int num) {
return bitSet.get(num);
}
// Get the size of the sparse bitset (number of integers present)
public int size() {
return bitSet.cardinality();
}
// Check if the sparse bitset is empty
public boolean isEmpty() {
return bitSet.isEmpty();
}
// Return a string representation of the sparse bitset
@Override
public String toString() {
return bitSet.toString();
}
public static void main(String[] args) {
// Create a sparse bitset
SparseBitset sparseSet = new SparseBitset();
// Add some integers to the sparse bitset
sparseSet.add(5);
sparseSet.add(10);
sparseSet.add(20);
// Check if integers are present
System.out.println("Contains 5: " + sparseSet.contains(5));
System.out.println("Contains 15: " + sparseSet.contains(15));
// Remove an integer
sparseSet.remove(10);
// Check the size of the sparse bitset
System.out.println("Size: " + sparseSet.size());
// Print the sparse bitset
System.out.println("Sparse Bitset: " + sparseSet.toString());
}
}