Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add sampled pointers class.
  • Loading branch information
echizentm committed Jan 19, 2017
1 parent 054f7ec commit 6299db6
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
6 changes: 6 additions & 0 deletions chapter_03/CMakeLists.txt
Expand Up @@ -10,3 +10,9 @@ add_executable(
fixed_length_vector_sample.cpp
cds/bit_vector.cpp
)
add_executable(
sampled_pointers_sample
sampled_pointers_sample.cpp
cds/bit_vector.cpp
cds/sampled_pointers.cpp
)
21 changes: 21 additions & 0 deletions chapter_03/cds/bit_vector.cpp
Expand Up @@ -33,6 +33,27 @@ namespace cds {
}


unsigned int bit_vector::bits_read(
unsigned int begin, unsigned int end
) {
if (this->is_in_a_cell(begin, end)) {
return this->bits_read_from_a_cell(begin, end);
} else {
return this->bits_read_from_two_cells(begin, end);
}
}

void bit_vector::bits_write(
unsigned int begin, unsigned int end, unsigned int value
) {
if (this->is_in_a_cell(begin, end)) {
this->bits_write_to_a_cell(begin, end, value);
} else {
this->bits_write_to_two_cells(begin, end, value);
}
}


bool bit_vector::is_in_a_cell(
unsigned int begin, unsigned int end
) {
Expand Down
3 changes: 3 additions & 0 deletions chapter_03/cds/bit_vector.h
Expand Up @@ -17,6 +17,9 @@ namespace cds {
void bit_set(unsigned int index);
void bit_clear(unsigned int index);

unsigned int bits_read(unsigned int begin, unsigned int end);
void bits_write(unsigned int begin, unsigned int end, unsigned int value);

bool is_in_a_cell(unsigned int begin, unsigned int end);
unsigned int bits_read_from_a_cell(unsigned int begin, unsigned int end);
unsigned int bits_read_from_two_cells(unsigned int begin, unsigned int end);
Expand Down
63 changes: 63 additions & 0 deletions chapter_03/cds/sampled_pointers.cpp
@@ -0,0 +1,63 @@
#include "sampled_pointers.h"


namespace cds {
sampled_pointers::sampled_pointers(unsigned int rate) {
this->rate = rate;
this->size = 0;
}

unsigned int sampled_pointers::read(unsigned int index) {
unsigned int begin = this->samples[index / this->rate];
unsigned int values_to_read = index % this->rate;

while (true) {
unsigned int zeros_length = 0;
while (this->bv.bit_read(begin) == 0) {
zeros_length++;
begin++;
}
unsigned int end = begin + zeros_length + 1;

if (values_to_read == 0) {
return this->bits_reverse(this->bv.bits_read(begin, end), end - begin) - 1;
}
values_to_read -= 1;
begin = end;
}
}

void sampled_pointers::push_back(unsigned int value) {
if (this->size % this->rate == 0) {
this->samples.push_back(this->bv.size);
}
this->size++;

value += 1;
unsigned int value_dummy = value;
unsigned int value_length = 0;
while (value_dummy != 0) {
value_dummy >>= 1;
value_length++;
}

unsigned int begin = this->bv.size + value_length - 1;
unsigned int end = begin + value_length;

this->bv.resize(end);
this->bv.bits_write(begin, end, this->bits_reverse(value, end - begin));
}

unsigned int sampled_pointers::bits_reverse(
unsigned int value, unsigned int length
) {
unsigned int reversed = 0;
while (length > 0) {
reversed <<= 1;
reversed |= (value & 1);
value >>= 1;
length -= 1;
}
return reversed;
}
}
19 changes: 19 additions & 0 deletions chapter_03/cds/sampled_pointers.h
@@ -0,0 +1,19 @@
#include <vector>
#include "bit_vector.h"


namespace cds {
class sampled_pointers {
public:
bit_vector bv;
std::vector<unsigned int> samples;
unsigned int rate;
unsigned int size;

sampled_pointers(unsigned int rate = 4);

unsigned int read(unsigned int index);
void push_back(unsigned int value);
unsigned int bits_reverse(unsigned int value, unsigned int length);
};
}
36 changes: 36 additions & 0 deletions chapter_03/sampled_pointers_sample.cpp
@@ -0,0 +1,36 @@
#include <iostream>
#include <vector>
#include "cds/sampled_pointers.h"


using namespace std;
using namespace cds;


int main(int argc, char **argv) {
sampled_pointers sp;
vector<int> numbers = {0, 1, 0, 2, 5, 1, 3, 2, 8, 2};

for (auto number : numbers) {
sp.push_back(number);
cout << number << " ";
}
cout << endl;

for (int i = 0; i < sp.bv.size; i++) {
cout << sp.bv.bit_read(i) << " ";
}
cout << endl;

for (auto sample : sp.samples) {
cout << sample << " ";
}
cout << endl;

for (int i = 0; i < sp.size; i++) {
cout << sp.read(i) << " ";
}
cout << endl;

return 0;
}

0 comments on commit 6299db6

Please sign in to comment.