3 #include "deltavector.h"
10 DeltaVector::DeltaVector(std::ifstream& file) :
14 DeltaVector::DeltaVector(std::FILE * file) :
20 DeltaVector::DeltaVector(DeltaEncoder& encoder, usint universe_size) :
21 BitVector(encoder, universe_size)
25 DeltaVector::~DeltaVector()
29 //--------------------------------------------------------------------------
32 DeltaVector::rank(usint value, bool at_least)
34 if(value >= this->size) { return this->items; }
35 this->getSample(this->sampleForValue(value));
37 while(this->cur < this->block_items && this->val < value)
39 this->val += this->buffer->readDeltaCode();
43 usint idx = this->sample.first + this->cur + 1;
44 if(!at_least && this->val > value) { idx--; }
45 if(at_least && this->val < value) { this->getSample(this->block + 1); }
50 DeltaVector::select(usint index)
52 if(index >= this->items) { return this->size; }
53 this->getSample(this->sampleForIndex(index));
55 usint lim = index - this->sample.first;
56 for(; this->cur < lim; this->cur++)
58 this->val += this->buffer->readDeltaCode();
65 DeltaVector::selectNext()
67 if(this->cur >= this->block_items)
69 this->getSample(this->block + 1);
74 this->val += this->buffer->readDeltaCode();
79 DeltaVector::valueAfter(usint value)
81 if(value >= this->size) { return pair_type(this->size, this->items); }
82 this->getSample(this->sampleForValue(value));
84 while(this->cur < this->block_items && this->val < value)
86 this->val += this->buffer->readDeltaCode();
91 this->getSample(this->block + 1);
94 return pair_type(this->val, this->sample.first + this->cur);
98 DeltaVector::nextValue()
100 if(this->cur >= this->block_items)
102 this->getSample(this->block + 1);
103 return pair_type(this->val, this->sample.first);
107 this->val += this->buffer->readDeltaCode();
108 return pair_type(this->val, this->sample.first + this->cur);
112 DeltaVector::selectRun(usint index, usint max_length)
114 return pair_type(this->select(index), 0);
118 DeltaVector::selectNextRun(usint max_length)
120 return pair_type(this->selectNext(), 0);
124 DeltaVector::isSet(usint value)
126 if(value >= this->size) { return false; }
127 this->getSample(this->sampleForValue(value));
129 while(this->cur < this->block_items && this->val < value)
131 this->val += this->buffer->readDeltaCode();
135 return (this->val == value);
138 //--------------------------------------------------------------------------
141 DeltaVector::reportSize()
143 usint bytes = sizeof(*this);
144 bytes += BitVector::reportSize();
148 //--------------------------------------------------------------------------
150 DeltaEncoder::DeltaEncoder(usint block_bytes, usint superblock_size) :
151 VectorEncoder(block_bytes, superblock_size)
155 DeltaEncoder::~DeltaEncoder()
160 DeltaEncoder::setBit(usint value)
164 this->setFirstBit(value);
167 if(value < this->size) { return; }
169 usint diff = value + 1 - this->size;
170 this->size = value + 1;
172 if(this->buffer->writeDeltaCode(diff)) { return; }
174 // Didn't fit into the block. A new sample & block required.