Skip to content

Commit

Permalink
Add size operation and add example
Browse files Browse the repository at this point in the history
  • Loading branch information
zMoooooritz committed Jul 25, 2022
1 parent 8a8f4c5 commit f37b0a7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ NAME = ditvector
CC = g++
CFLAGS = -Wall -g -std=c++20

.PHONY: all test clean info
.PHONY: all example test clean info

all: test
all: example

example: example.o
@$(CC) $(CFLAGS) -o example example.o

example.o: example.cpp avl.hpp bit_vector.hpp bit_vector.cpp
@$(CC) $(CFLAGS) -c example.cpp

test: test.o
@$(CC) $(CFLAGS) -o test test.o
Expand Down
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@

A dynamic bitvector that support rank and select queries.

The datastructure allows for one template argument that defines the size of the bitblocks stored in leaf.
The datastructure allows for one template argument that defines the size of the bitblocks stored in the leafs.
This parameter can be used to select the appropriate trade-off between space and time complexity.
As a sensible default a size of 512 bits is used.

## Operations

The datastructure supports the following instructions which all have logarithmic runtime.
* insert(index, true/false)
* del(index)
* set(index)
* unset(index)
* flip(index)
* rank(index, true/false)
* select(index, true/false)
* `insert(index, true/false)`
* `del(index)`
* `set(index)`
* `unset(index)`
* `flip(index)`
* `rank(index, true/false)`
* `select(index, true/false)`
* `complement()`
* `size()` returns number of bits in bitvector
* `extract()` returns all bits as std::vector<bool>

## Usage

```c++
BitVector<> bv;
BitVector<16> bv;
bv.insert(0, false);
bv.insert(0, true);
bv.access(0); // true
bv.rank(1, true); // 1
bv.access(0); // 1
bv.rank(1, true); // 1
bv.flip(0);
bv.rank(1, true); // 0
bv.rank(1, true); // 0
```

13 changes: 13 additions & 0 deletions bit_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ void BitVector<S>::complement() {
complement(this->root);
}

template <size_t S>
uint32_t BitVector<S>::size() {
return size(this->root);
}

// collect all the bits in the bitvector and return it as one consecutive bool vector
template <size_t S>
std::vector<bool> BitVector<S>::extract() {
Expand Down Expand Up @@ -295,6 +300,14 @@ void BitVector<S>::complement(BV_Node<S> *node) {
}
}

// calculate the number of bits that are stored in the structure
template <size_t S>
uint32_t BitVector<S>::size(BV_Node<S> *node) {
if (!node)
return 0;
return node->nums + size(node->r);
}

// find the node (always a leaf) that contains the bit at the position index
// index is updated as well to locate the bit inside the leaf block
template <size_t S>
Expand Down
2 changes: 2 additions & 0 deletions bit_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class BitVector : public AVL<BV_Node<S>> {
uint32_t select(BV_Node<S> *, uint32_t, bool);
bool access(BV_Node<S> *, uint32_t);
void complement(BV_Node<S> *);
uint32_t size(BV_Node<S> *);
BV_Node<S> *find_block(BV_Node<S> *, uint32_t*);

#ifdef ADS_DEBUG
Expand Down Expand Up @@ -85,6 +86,7 @@ class BitVector : public AVL<BV_Node<S>> {
uint32_t select(uint32_t, bool);
bool access(uint32_t);
void complement();
uint32_t size();
std::vector<bool> extract();

#ifdef ADS_DEBUG
Expand Down
17 changes: 17 additions & 0 deletions example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "bit_vector.cpp"

const size_t BLOCK_SIZE = 64;

int main(int argc, char *argv[]) {

BitVector<BLOCK_SIZE> bv;
bv.insert(0, false);
bv.insert(0, true);
std::cout << bv.access(0) << std::endl;
std::cout << bv.rank(1, true) << std::endl;
bv.flip(0);
std::cout << bv.rank(1, true) << std::endl;
bv.del(0);
std::cout << bv.size() << std::endl;
}

0 comments on commit f37b0a7

Please sign in to comment.