3 #include "deltavector.h"
10 DeltaVector::DeltaVector(std::ifstream& file) :
15 DeltaVector::DeltaVector(DeltaEncoder& encoder, usint universe_size) :
16 BitVector(encoder, universe_size)
20 DeltaVector::~DeltaVector()
24 //--------------------------------------------------------------------------
27 DeltaVector::rank(usint value, bool at_least)
29 if(value >= this->size) { return this->items; }
30 this->getSample(this->sampleForValue(value));
32 while(this->cur < this->block_items && this->val < value)
34 this->val += this->buffer->readDeltaCode();
38 usint idx = this->sample.first + this->cur + 1;
39 if(!at_least && this->val > value) { idx--; }
40 if(at_least && this->val < value) { this->getSample(this->block + 1); }
45 DeltaVector::select(usint index)
47 if(index >= this->items) { return this->size; }
48 this->getSample(this->sampleForIndex(index));
50 usint lim = index - this->sample.first;
51 for(; this->cur < lim; this->cur++)
53 this->val += this->buffer->readDeltaCode();
60 DeltaVector::selectNext()
62 if(this->cur >= this->block_items)
64 this->getSample(this->block + 1);
69 this->val += this->buffer->readDeltaCode();
74 DeltaVector::valueAfter(usint value)
76 if(value >= this->size) { return pair_type(this->size, this->items); }
77 this->getSample(this->sampleForValue(value));
79 while(this->cur < this->block_items && this->val < value)
81 this->val += this->buffer->readDeltaCode();
86 this->getSample(this->block + 1);
89 return pair_type(this->val, this->sample.first + this->cur);
93 DeltaVector::nextValue()
95 if(this->cur >= this->block_items)
97 this->getSample(this->block + 1);
98 return pair_type(this->val, this->sample.first);
102 this->val += this->buffer->readDeltaCode();
103 return pair_type(this->val, this->sample.first + this->cur);
107 DeltaVector::selectRun(usint index, usint max_length)
109 return pair_type(this->select(index), 0);
113 DeltaVector::selectNextRun(usint max_length)
115 return pair_type(this->selectNext(), 0);
119 DeltaVector::isSet(usint value)
121 if(value >= this->size) { return false; }
122 this->getSample(this->sampleForValue(value));
124 while(this->cur < this->block_items && this->val < value)
126 this->val += this->buffer->readDeltaCode();
130 return (this->val == value);
133 //--------------------------------------------------------------------------
136 DeltaVector::reportSize()
138 usint bytes = sizeof(*this);
139 bytes += BitVector::reportSize();
143 //--------------------------------------------------------------------------
145 DeltaEncoder::DeltaEncoder(usint block_bytes, usint superblock_size) :
146 VectorEncoder(block_bytes, superblock_size)
150 DeltaEncoder::~DeltaEncoder()
155 DeltaEncoder::setBit(usint value)
159 this->setFirstBit(value);
162 if(value < this->size) { return; }
164 usint diff = value + 1 - this->size;
165 this->size = value + 1;
167 if(this->buffer->writeDeltaCode(diff)) { return; }
169 // Didn't fit into the block. A new sample & block required.