Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add sampled pointers class.
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |