Skip to content

Commit

Permalink
debugged first part of issure #24
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaprezza committed Jul 22, 2020
1 parent b11d42b commit ee74e12
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 89 deletions.
23 changes: 21 additions & 2 deletions debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ int main() {
dyn::succinct_spsi spsi;
dyn::spsi_check<> check;

int n=40000;
int n=100000;
int sigma=120;

srand(time(NULL));

for(int i=0;i<n;++i){

cout << i << endl;
cout << "insert number " << i << endl;

int j = rand()%(i+1);
int x = rand()%sigma;
Expand All @@ -35,5 +35,24 @@ int main() {
}



for(int i=0;i<n/2;++i){

cout << "remove number " << i << endl;

int j = rand()%spsi.size();

spsi.remove(j);
check.remove(j);

}

for(int i=0;i<spsi.size();++i){

assert(spsi[i]==check[i]);

}


return 0;
}
2 changes: 1 addition & 1 deletion include/dynamic/internal/hacked_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class hacked_vector{
}
}

//shift ints left, from position i + 1 onwords
//shift ints left, from position i + 1 onwards
shift_left( i );


Expand Down
202 changes: 116 additions & 86 deletions include/dynamic/internal/packed_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ namespace dyn{

words = vector<uint64_t>( size_/int_per_word_ + ( size_%int_per_word_ != 0 ) );

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

packed_vector(vector<uint64_t>&& _words, uint64_t size, uint8_t width) {
Expand All @@ -148,10 +148,10 @@ namespace dyn{

psum_=psum(size_-1);

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

virtual ~packed_vector() {}
Expand Down Expand Up @@ -423,47 +423,60 @@ namespace dyn{

}

void remove(uint64_t i) {
assert(i < size_);
auto x = this->at(i);
void remove(uint64_t i) {

assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

if (width_>1) { // otherwise, cannot rebuild
if (bitsize(x) == width_) {
assert(i < size_);
auto x = this->at(i);

uint8_t max_b = 0;
if (width_>1) { // otherwise, cannot rebuild
if (bitsize(x) == width_) {

for(ulint j = 0; j < size_ ;++j){
if (j != i) {
auto x = this->at(j);
uint8_t max_b = 0;

uint8_t bs = bitsize(x);
for(ulint j = 0; j < size_ ;++j){
if (j != i) {
auto x = this->at(j);

if(bs>max_b) max_b=bs;
}

}
uint8_t bs = bitsize(x);

//rebuild entire vector
rebuild_rem( i, max_b );
if(bs>max_b) max_b=bs;
}

return;
}

}
}
//rebuild entire vector
rebuild_rem( i, max_b );

//shift ints left, from position i + 1 onwords
shift_left( i );
return;

--size_;
psum_ -= x;
}
}

while ( words.size() > size_ / int_per_word_ + extra_ ) {
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

//shift ints left, from position i + 1 onwards
shift_left( i );

//set_without_psum_update(size_-1, 0);

--size_;
psum_ -= x;

while ( words.size() > size_ / int_per_word_ + (size_%int_per_word_!=0) + extra_ ) {
words.pop_back();
}
}

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

}
Expand Down Expand Up @@ -503,10 +516,10 @@ namespace dyn{
psum_+=x;
++size_;

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

void insert_word(uint64_t i,
Expand Down Expand Up @@ -580,10 +593,10 @@ namespace dyn{
psum_+=x;
size_++;

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

uint64_t size() const {
Expand All @@ -599,6 +612,11 @@ namespace dyn{
*/
packed_vector* split(){

assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

uint64_t tot_words = (size_/int_per_word_) + (size_%int_per_word_!=0);

assert(tot_words <= words.size());
Expand Down Expand Up @@ -632,10 +650,10 @@ namespace dyn{
words.shrink_to_fit();
words[size_ / int_per_word_] &= ((~uint64_t(0)) >> (64 - ((size_ % int_per_word_) * width_)));

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

return right;

Expand Down Expand Up @@ -663,10 +681,10 @@ namespace dyn{
//insert x inside i-th position
words[word_nr] |= (x<<(width_*pos));

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

/*
Expand Down Expand Up @@ -736,10 +754,10 @@ namespace dyn{

in.read((char*)&int_per_word_,sizeof(int_per_word_));

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

uint64_t width() const {
Expand Down Expand Up @@ -859,9 +877,15 @@ namespace dyn{
//from the (i + 1)-st.
void shift_left(uint64_t i){

assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");


//number of integers that fit in a memory word
assert(int_per_word_>0);
assert(i < size_);
assert(i < size_);

if (i == (size_ - 1)) {
set_without_psum_update( i, 0 );
Expand Down Expand Up @@ -901,6 +925,13 @@ namespace dyn{
set_without_psum_update(j * int_per_word_ + int_per_word_ - 1, falling_in_idx);
}


assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");


}

// Rebuilds entire vector, setting j to value y
Expand Down Expand Up @@ -946,10 +977,10 @@ namespace dyn{
width_ = new_width_;
int_per_word_ = new_int_per_word_;

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

// Rebuilds entire vector, removing j from the vector
Expand All @@ -962,10 +993,10 @@ namespace dyn{
int_per_word_ = 0;
psum_ = 0;

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
return;
}

Expand Down Expand Up @@ -1001,10 +1032,10 @@ namespace dyn{
width_ = new_width_;
int_per_word_ = new_int_per_word_;

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

// Rebuilds entire vector, inserting y at position j
Expand Down Expand Up @@ -1058,10 +1089,10 @@ namespace dyn{

words.assign( new_words.begin(), new_words.end() );

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

void rebuild(packed_vector& vec){
Expand All @@ -1086,10 +1117,10 @@ namespace dyn{

}

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

}

Expand Down Expand Up @@ -1171,11 +1202,10 @@ class packed_bit_vector : public packed_vector {
words[(size_ - 1) / int_per_word_] |= static_cast<uint64_t>(1) << ((size_ - 1) % 64);
psum_++;
}

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
}

virtual void set_without_psum_update(uint64_t i, uint64_t x) override final {
Expand Down Expand Up @@ -1218,10 +1248,10 @@ class packed_bit_vector : public packed_vector {

auto right = new packed_bit_vector(std::move(right_words), nr_right_ints);

assert(size_ / int_per_word_ <= words.size());
assert((size_ / int_per_word_ == words.size()
|| !(words[size_ / int_per_word_] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");
assert(size_ / int_per_word_ + (size_%int_per_word_!=0) <= words.size());
assert((size_ / int_per_word_ + (size_%int_per_word_!=0) == words.size()
|| !(words[words.size()-1] >> ((size_ % int_per_word_) * width_)))
&& "uninitialized non-zero values in the end of the vector");

return right;
}
Expand Down

0 comments on commit ee74e12

Please sign in to comment.