12 #ifndef XPACE_ELT_INT_H 13 #define XPACE_ELT_INT_H 15 #include "QtCore/QHash" 17 #include "util/bitstream.h" 28 (
const std::vector<N>& vals);
57 QHash<N, uint> norm_hash;
58 QHash<N, uint> runs_hash;
71 (
const std::vector<N>* v,
95 return N::getBits(b, bits);
99 const std::vector<N>* vals;
108 (
const std::vector<N>* v)
124 for (
typename std::vector<N>::const_iterator n(vals->begin()); n != vals->end(); ++n)
148 return N::getVar(b, pool);
152 const std::vector<N>* vals;
194 std::vector<N> codes;
231 QHash<N, uint> freq_hash;
235 std::vector<N> codes;
248 (
const freq_elt& rhs)
251 return freq > rhs.freq;
257 std::vector<freq_elt> freq_list;
264 #define SHOW_STATS !NDEBUG 278 (
const std::vector<N>& vals)
280 term_count = narrow_to<uint>(vals.size());
285 norm_max_val = N::min();
287 N prev_val(N::max());
288 for (
typename std::vector<N>::const_iterator v(vals.begin()); v != vals.end(); ++v)
290 typename QHash<N, uint>::iterator it(norm_hash.find(*v));
291 if (it == norm_hash.end())
292 norm_hash.insert(*v, 0);
298 typename QHash<N, uint>::iterator it(runs_hash.find(*v));
299 if (it == runs_hash.end())
300 runs_hash.insert(*v, 0);
305 if (*v > norm_max_val)
316 (
const std::vector<N>* n,
320 bits = std::max(
size_t(1), maxValue.bitsNeeded());
329 return MemBitStream::lenVar(bits) + vals->size() * bits;
357 bits = 1 + b->getVar<
size_t>();
369 qh = (isRun ? hash->getRunsHash() : hash->getNormHash());
371 code_bits = bitsNeeded(qh->size() - 1);
375 table_size = MemBitStream::lenVar(fixed_size) + fixed_size * qh->size();
378 size_t var_table_size(2);
379 for (
typename QHash<N, uint>::const_iterator it(qh->begin()); var_table_size < table_size; )
381 var_table_size += it.key().lenVar();
382 if (++it == qh->end())
385 table_size = var_table_size;
391 table_size += MemBitStream::lenVar(
size_t(qh->size() - 1))
392 + MemBitStream::lenVar(code_bits - 1);
395 table_size += hash->getTermCount() * code_bits;
414 if (
uint(qh->size()) > _max_ht_size)
415 _max_ht_size = qh->size();
416 uint pct((qh->size() * 100) / hash->getTermCount());
417 if (pct > _max_ht_pct)
422 b->addVar(
uint(qh->size() - 1));
423 b->addVar(code_bits - 1);
424 b->addVar(fixed_size);
430 for (
typename QHash<N, uint>::iterator it(qh->begin()); it != qh->end(); ++it)
434 n.addBits(fixed_size, b);
438 for (
typename QHash<N, uint>::iterator it(qh->begin()); it != qh->end(); ++it)
453 b->addBits(code_bits, (*qh)[n]);
462 uint num_codes(b->getVar<
uint>() + 1);
463 codes.reserve(num_codes);
465 code_bits = b->getVar<
uint>() + 1;
466 if ((fixed_size = b->getVar<
size_t>()))
469 codes.push_back(N::getBits(b, fixed_size));
473 codes.push_back(N::getVar(b, pool));
482 return codes[b->getBits<
uint>(code_bits)];
494 qh = isRun ? hash->
getRunsHash() : hash->getNormHash();
498 fixed_size = hash->getNormMax().bitsNeeded();
499 table_size = MemBitStream::lenVar(fixed_size) + fixed_size * qh->size();
502 size_t var_table_size(2);
503 for (
typename QHash<N, uint>::const_iterator it(qh->begin()); var_table_size < table_size; )
505 var_table_size += it.key().lenVar();
506 if (++it == qh->end())
509 table_size = var_table_size;
515 table_size += MemBitStream::lenVar(
size_t(qh->size() - 1));
519 freq_list.reserve(qh->size());
521 for (
typename QHash<N, uint>::const_iterator it(qh->begin()); it != qh->end(); ++it)
522 freq_list.push_back(freq_elt(it.key(), it.value()));
524 std::sort(freq_list.begin(), freq_list.end());
526 freq_hash.reserve(qh->size());
528 for (
typename std::vector<freq_elt>::const_iterator it(freq_list.begin()); it != freq_list.end(); ++it)
530 table_size += MemBitStream::lenVar(i) * (1 + it->freq);
531 freq_hash.insert(it->key, i++);
551 if (
uint(freq_hash.size()) > _max_ht_size)
552 _max_ht_size = freq_hash.size();
553 uint pct(
uint(freq_hash.size() * 100) / hash->getTermCount());
554 if (pct > _max_ht_pct)
558 b->addVar(
uint(freq_hash.size() - 1));
559 b->addVar(fixed_size);
562 for (
typename std::vector<freq_elt>::const_iterator it(freq_list.begin()); it != freq_list.end(); ++it)
563 it->key.addBits(fixed_size, b);
565 for (
typename std::vector<freq_elt>::const_iterator it(freq_list.begin()); it != freq_list.end(); ++it)
576 b->addVar(freq_hash[n]);
585 uint num_codes(b->getVar<
uint>() + 1);
586 codes.reserve(num_codes);
588 if ((fixed_size = b->getVar<
size_t>()))
591 codes.push_back(N::getBits(b, fixed_size));
595 codes.push_back(N::getVar(b, pool));
605 if (i >= codes.size())
606 throw MemBitStream::Corrupt(b->getBitPos());
void set(const std::vector< N > *v, const N &maxValue)
void startEncode(MemBitStream *b) const
void startDecode(MemBitStream *b, BytePool *)
N decode(MemBitStream *b) const
void startDecode(MemBitStream *b, BytePool *)
void startEncode(MemBitStream *b) const
void startDecode(MemBitStream *b, BytePool *pool)
void encode(const N &n, MemBitStream *b) const
QHash< N, uint > * getNormHash()
uint getTermCount() const
void set(eltIntInfo< N > *h, bool isRun)
N decode(MemBitStream *b) const
void startEncode(MemBitStream *n) const
QHash< N, uint > * getRunsHash()
void encode(const N &n, MemBitStream *b) const
void set(eltIntInfo< N > *h, bool isRun)
void encode(const N &norm, MemBitStream *b) const
Xpace project main namespace
void set(const std::vector< N > &vals)