3 #include "deltavector.h"
10 DeltaVector::DeltaVector(std::ifstream& file) :
15 DeltaVector::DeltaVector(std::FILE * file) :
21 DeltaVector::DeltaVector(Encoder& encoder, usint universe_size) :
22 BitVector(encoder, universe_size)
26 DeltaVector::~DeltaVector()
30 //--------------------------------------------------------------------------
33 DeltaVector::reportSize() const
35 usint bytes = sizeof(*this);
36 bytes += BitVector::reportSize();
40 //--------------------------------------------------------------------------
42 DeltaVector::Iterator::Iterator(const DeltaVector& par) :
43 BitVector::Iterator(par)
47 DeltaVector::Iterator::~Iterator()
52 DeltaVector::Iterator::rank(usint value, bool at_least)
54 const DeltaVector& par = (const DeltaVector&)(this->parent);
56 if(value >= par.size) { return par.items; }
57 this->getSample(this->sampleForValue(value));
59 while(this->cur < this->block_items && this->val < value)
61 this->val += this->buffer.readDeltaCode();
65 usint idx = this->sample.first + this->cur + 1;
66 if(!at_least && this->val > value) { idx--; }
67 if(at_least && this->val < value) { this->getSample(this->block + 1); }
72 DeltaVector::Iterator::select(usint index)
74 const DeltaVector& par = (const DeltaVector&)(this->parent);
76 if(index >= par.items) { return par.size; }
77 this->getSample(this->sampleForIndex(index));
79 usint lim = index - this->sample.first;
80 for(; this->cur < lim; this->cur++)
82 this->val += this->buffer.readDeltaCode();
89 DeltaVector::Iterator::selectNext()
91 if(this->cur >= this->block_items)
93 this->getSample(this->block + 1);
98 this->val += this->buffer.readDeltaCode();
103 DeltaVector::Iterator::valueAfter(usint value)
105 const DeltaVector& par = (const DeltaVector&)(this->parent);
107 if(value >= par.size) { return pair_type(par.size, par.items); }
108 this->getSample(this->sampleForValue(value));
110 while(this->cur < this->block_items && this->val < value)
112 this->val += this->buffer.readDeltaCode();
115 if(this->val < value)
117 this->getSample(this->block + 1);
120 return pair_type(this->val, this->sample.first + this->cur);
124 DeltaVector::Iterator::nextValue()
126 if(this->cur >= this->block_items)
128 this->getSample(this->block + 1);
129 return pair_type(this->val, this->sample.first);
133 this->val += this->buffer.readDeltaCode();
134 return pair_type(this->val, this->sample.first + this->cur);
138 DeltaVector::Iterator::selectRun(usint index, usint max_length)
140 return pair_type(this->select(index), 0);
144 DeltaVector::Iterator::selectNextRun(usint max_length)
146 return pair_type(this->selectNext(), 0);
150 DeltaVector::Iterator::isSet(usint value)
152 const DeltaVector& par = (const DeltaVector&)(this->parent);
154 if(value >= par.size) { return false; }
155 this->getSample(this->sampleForValue(value));
157 while(this->cur < this->block_items && this->val < value)
159 this->val += this->buffer.readDeltaCode();
163 return (this->val == value);
166 //--------------------------------------------------------------------------
168 DeltaEncoder::DeltaEncoder(usint block_bytes, usint superblock_size) :
169 VectorEncoder(block_bytes, superblock_size)
173 DeltaEncoder::~DeltaEncoder()
178 DeltaEncoder::setBit(usint value)
182 this->setFirstBit(value);
185 if(value < this->size) { return; }
187 usint diff = value + 1 - this->size;
188 this->size = value + 1;
190 if(this->buffer->writeDeltaCode(diff)) { return; }
192 // Didn't fit into the block. A new sample & block required.
197 DeltaEncoder::setRun(usint start, usint len)
199 for(usint i = start; i < start + len; i++)
206 DeltaEncoder::addBit(usint value)
212 DeltaEncoder::addRun(usint start, usint len)
214 this->setRun(start, len);
218 DeltaEncoder::flush()