Skip to content

Commit

Permalink
mv fixed_length_vector class to cds/.
Browse files Browse the repository at this point in the history
  • Loading branch information
echizentm committed Jan 22, 2017
1 parent a1df9a6 commit 400eed7
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 57 deletions.
1 change: 1 addition & 0 deletions chapter_03/CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ add_executable(
fixed_length_vector_sample
fixed_length_vector_sample.cpp
cds/bit_vector.cpp
cds/fixed_length_vector.cpp
)
add_executable(
sampled_pointers_sample
Expand Down
49 changes: 49 additions & 0 deletions chapter_03/cds/fixed_length_vector.cpp
@@ -0,0 +1,49 @@
#include "fixed_length_vector.h"


namespace cds {
unsigned int fixed_length_vector::get_bit_index(unsigned int index) {
return (
(this->is_rapid && this->rest_size_of_a_cell > 0) ?
index / this->elements_in_a_cell * this->bv.cell_size + index % this->elements_in_a_cell * this->length :
index * this->length
);
}

fixed_length_vector::fixed_length_vector(unsigned int length, unsigned int size, bool is_rapid) {
this->length = length;
this->is_rapid = is_rapid;
this->elements_in_a_cell = bv.cell_size / this->length;
this->rest_size_of_a_cell = bv.cell_size % this->length;
resize(size);
}

void fixed_length_vector::resize(unsigned int size) {
this->size = size;
this->bv.resize(this->get_bit_index(this->size));
}

unsigned int fixed_length_vector::vector_size() {
return this->bv.vector_size();
}

unsigned int fixed_length_vector::read(unsigned int index) {
unsigned int bit_index = this->get_bit_index(index);

if (this->is_rapid || this->rest_size_of_a_cell == 0) {
return this->bv.bits_read_from_a_cell(bit_index, bit_index + this->length);
} else {
return this->bv.bits_read(bit_index, bit_index + this->length);
}
}

void fixed_length_vector::write(unsigned int index, unsigned int value) {
unsigned int bit_index = this->get_bit_index(index);

if (this->is_rapid || this->rest_size_of_a_cell == 0) {
this->bv.bits_write_to_a_cell(bit_index, bit_index + this->length, value);
} else {
this->bv.bits_write(bit_index, bit_index + this->length, value);
}
}
}
24 changes: 24 additions & 0 deletions chapter_03/cds/fixed_length_vector.h
@@ -0,0 +1,24 @@
#include "bit_vector.h"


namespace cds {
class fixed_length_vector {
bit_vector bv;
unsigned int elements_in_a_cell;
unsigned int rest_size_of_a_cell;

unsigned int get_bit_index(unsigned int index);

public:
unsigned int length;
unsigned int size;
bool is_rapid;

fixed_length_vector(unsigned int length, unsigned int size = 0, bool is_rapid = false);

void resize(unsigned int size);
unsigned int vector_size();
unsigned int read(unsigned int index);
void write(unsigned int index, unsigned int value);
};
}
4 changes: 4 additions & 0 deletions chapter_03/cds/sampled_pointers.cpp
Expand Up @@ -7,6 +7,10 @@ namespace cds {
this->size = 0;
}

unsigned int sampled_pointers::vector_size() {
return this->bv.vector_size() + this->samples.size();
}

unsigned int sampled_pointers::read(unsigned int index) {
unsigned int begin = this->samples[index / this->rate];
unsigned int values_to_read = index % this->rate;
Expand Down
1 change: 1 addition & 0 deletions chapter_03/cds/sampled_pointers.h
Expand Up @@ -13,6 +13,7 @@ namespace cds {

sampled_pointers(unsigned int rate = 4);

unsigned int vector_size();
unsigned int read(unsigned int index);
void push_back(unsigned int value);
};
Expand Down
58 changes: 1 addition & 57 deletions chapter_03/fixed_length_vector_sample.cpp
Expand Up @@ -3,70 +3,14 @@
#include <iostream>
#include <numeric>
#include <vector>
#include "cds/bit_vector.h"
#include "cds/fixed_length_vector.h"


using namespace std;
using namespace std::chrono;
using namespace cds;


class fixed_length_vector {
unsigned int length;
unsigned int size;
bit_vector bv;
bool is_rapid;
unsigned int cell_rest_size;
unsigned int elements_in_a_cell;

unsigned int get_bit_index(unsigned int index) {
return (
(is_rapid && cell_rest_size > 0) ?
index / elements_in_a_cell * bv.cell_size + index % elements_in_a_cell * length :
index * length
);
}
unsigned int bits_read(unsigned int begin, unsigned int end) {
if (is_rapid || bv.is_in_a_cell(begin, end)) {
return bv.bits_read_from_a_cell(begin, end);
} else {
return bv.bits_read_from_two_cells(begin, end);
}
}
void bits_write(unsigned int begin, unsigned int end, unsigned int value) {
if (is_rapid || bv.is_in_a_cell(begin, end)) {
bv.bits_write_to_a_cell(begin, end, value);
} else {
bv.bits_write_to_two_cells(begin, end, value);
}
}

public:
fixed_length_vector(unsigned int length, unsigned int size = 0, bool is_rapid = false) {
this->length = length;
this->is_rapid = is_rapid;
elements_in_a_cell = bv.cell_size / length;
cell_rest_size = bv.cell_size % length;
resize(size);
}
unsigned int read(unsigned int index) {
unsigned int bit_index = get_bit_index(index);
return bits_read(bit_index, bit_index + length);
}
void write(unsigned int index, unsigned int value) {
unsigned int bit_index = get_bit_index(index);
bits_write(bit_index, bit_index + length, value);
}
void resize(unsigned int size) {
this->size = size;
bv.resize(get_bit_index(size));
}
unsigned int vector_size() {
return bv.vector_size();
}
};


bool run_vector(int length, int num) {
vector<unsigned int> vec(num);
vector<int> range(num);
Expand Down
2 changes: 2 additions & 0 deletions chapter_03/sampled_pointers_sample.cpp
Expand Up @@ -22,5 +22,7 @@ int main(int argc, char **argv) {
}
cout << endl;

cout << "vector size: " << sp.vector_size() << endl;

return 0;
}

0 comments on commit 400eed7

Please sign in to comment.