Xpace
elt_int.h
Go to the documentation of this file.
1 
2 /**********************************************************//**
3  **
4  ** @file data/compress/elt_int.h
5  **
6  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
7  **
8  ** www.xpace.net
9  **
10  **************************************************************/
11 
12 #ifndef XPACE_ELT_INT_H
13 #define XPACE_ELT_INT_H
14 
15 #include "QtCore/QHash" // TODO Xpace Hash
16 
17 #include "util/bitstream.h"
18 
19 namespace Xpace
20 {
21  // ================================ INFO ====================
22 
23  template<typename N>
24  class eltIntInfo
25  {
26  public:
27  void set
28  (const std::vector<N>& vals);
29 
31  ()
32  const
33  {
34  return term_count;
35  }
36 
37  N getNormMax
38  ()
39  const
40  {
41  return norm_max_val;
42  }
43 
44  QHash<N, uint>* getNormHash
45  ()
46  {
47  return &norm_hash;
48  }
49 
50  QHash<N, uint>* getRunsHash
51  ()
52  {
53  return &runs_hash;
54  }
55 
56  private:
57  QHash<N, uint> norm_hash;
58  QHash<N, uint> runs_hash;
59 
60  uint term_count;
61  N norm_max_val;
62  };
63 
64  // ================================ FIXED ===================
65 
66  template<typename N>
68  {
69  public:
70  void set
71  (const std::vector<N>* v,
72  const N& maxValue);
73 
74  size_t getLen
75  ()
76  const;
77 
78  void startEncode
79  (MemBitStream* n)
80  const;
81 
82  void encode
83  (const N& n,
84  MemBitStream* b)
85  const;
86 
87  void startDecode
88  (MemBitStream* b,
89  BytePool*);
90 
91  N decode
92  (MemBitStream* b)
93  const
94  {
95  return N::getBits(b, bits);
96  }
97 
98  private:
99  const std::vector<N>* vals;
100  size_t bits;
101  };
102 
103  template<typename N>
104  class eltIntVar
105  {
106  public:
107  void set
108  (const std::vector<N>* v)
109  {
110  vals = v;
111  }
112 
113  void startEncode
114  (MemBitStream*)
115  const
116  {
117  }
118 
119  size_t getLen
120  ()
121  const
122  {
123  size_t ret(0);
124  for (typename std::vector<N>::const_iterator n(vals->begin()); n != vals->end(); ++n)
125  ret += n->lenVar();
126  return ret;
127  }
128 
129  void encode
130  (const N& n,
131  MemBitStream* b)
132  const
133  {
134  n.addVar(b);
135  }
136 
137  void startDecode
138  (MemBitStream*,
139  BytePool* p)
140  {
141  pool = p;
142  }
143 
144  N decode
145  (MemBitStream* b)
146  const
147  {
148  return N::getVar(b, pool);
149  }
150 
151  private:
152  const std::vector<N>* vals;
153  BytePool* pool;
154  };
155 
156  // ================================ CODED ===================
157 
158  template<typename N>
159  class eltIntCoded
160  {
161  public:
162  void set
163  (eltIntInfo<N>* h,
164  bool isRun);
165 
166  size_t getLen
167  ()
168  const;
169 
170  void startEncode
171  (MemBitStream* b)
172  const;
173 
174  void encode
175  (const N& norm,
176  MemBitStream* b)
177  const;
178 
179  void startDecode
180  (MemBitStream* b,
181  BytePool*);
182 
183  N decode
184  (MemBitStream* b)
185  const;
186 
187  private:
188  eltIntInfo<N>* hash;
189  QHash<N, uint>* qh;
190 
191  size_t code_bits;
192  size_t fixed_size;
193  size_t table_size;
194  std::vector<N> codes;
195  };
196 
197  // ================================ VAR CODED ===============
198 
199  template<typename N>
201  {
202  public:
203  void set
204  (eltIntInfo<N>* h,
205  bool isRun);
206 
207  size_t getLen
208  ()
209  const;
210 
211  void startEncode
212  (MemBitStream* b)
213  const;
214 
215  void encode
216  (const N& n,
217  MemBitStream* b)
218  const;
219 
220  void startDecode
221  (MemBitStream* b,
222  BytePool* pool);
223 
224  N decode
225  (MemBitStream* b)
226  const;
227 
228  private:
229  eltIntInfo<N>* hash;
230  QHash<N, uint>* qh;
231  QHash<N, uint> freq_hash;
232 
233  size_t fixed_size;
234  size_t table_size;
235  std::vector<N> codes;
236 
237  struct freq_elt
238  {
239  freq_elt
240  (N k,
241  uint f) :
242  key(k),
243  freq(f)
244  {
245  }
246 
247  bool operator<
248  (const freq_elt& rhs)
249  const
250  {
251  return freq > rhs.freq;
252  }
253 
254  N key;
255  uint freq;
256  };
257  std::vector<freq_elt> freq_list;
258  };
259 
260  // ==========================================================
261  // ==========================================================
262  // ==========================================================
263 
264  #define SHOW_STATS !NDEBUG
265 
266  #if SHOW_STATS
267  extern uint _counts[16];
268  extern size_t _sizes[16];
269  extern bool _is_delta;
270  extern uint _max_ht_size, _max_ht_pct;
271  #endif
272 
273  // ================================ INFO ====================
274 
275  template<typename N>
276  inline
277  void eltIntInfo<N>::set
278  (const std::vector<N>& vals)
279  {
280  term_count = narrow_to<uint>(vals.size());
281 
282  norm_hash.clear();
283  runs_hash.clear();
284 
285  norm_max_val = N::min();
286 
287  N prev_val(N::max()); // as long as it's different from val[0]
288  for (typename std::vector<N>::const_iterator v(vals.begin()); v != vals.end(); ++v)
289  {
290  typename QHash<N, uint>::iterator it(norm_hash.find(*v));
291  if (it == norm_hash.end())
292  norm_hash.insert(*v, 0);
293  else
294  ++*it;
295 
296  if (*v != prev_val)
297  {
298  typename QHash<N, uint>::iterator it(runs_hash.find(*v));
299  if (it == runs_hash.end())
300  runs_hash.insert(*v, 0);
301  else
302  ++*it;
303  prev_val = *v;
304 
305  if (*v > norm_max_val)
306  norm_max_val = *v;
307  }
308  }
309  }
310 
311  // ================================ FIXED ===================
312 
313  template<typename N>
314  inline
316  (const std::vector<N>* n,
317  const N& maxValue)
318  {
319  vals = n;
320  bits = std::max(size_t(1), maxValue.bitsNeeded());
321  }
322 
323  template<typename N>
324  inline
326  ()
327  const
328  {
329  return MemBitStream::lenVar(bits) + vals->size() * bits;
330  }
331 
332  template<typename N>
333  inline
335  (MemBitStream* b)
336  const
337  {
338  b->addVar(bits - 1);
339  }
340 
341  template<typename N>
342  inline
344  (const N& n,
345  MemBitStream* b)
346  const
347  {
348  n.addBits(bits, b);
349  }
350 
351  template<typename N>
352  inline
354  (MemBitStream* b,
355  BytePool*)
356  {
357  bits = 1 + b->getVar<size_t>();
358  }
359 
360  // ================================ CODED ===================
361 
362  template<typename N>
363  inline
366  bool isRun)
367  {
368  hash = h;
369  qh = (isRun ? hash->getRunsHash() : hash->getNormHash());
370  assert(qh->size());
371  code_bits = bitsNeeded(qh->size() - 1);
372 
373  // calculate table size for fixed-size elts
374  fixed_size = hash->getNormMax().bitsNeeded();
375  table_size = MemBitStream::lenVar(fixed_size) + fixed_size * qh->size();
376 
377  // see if variable is smaller
378  size_t var_table_size(2); // place for fixed_size = 0
379  for (typename QHash<N, uint>::const_iterator it(qh->begin()); var_table_size < table_size; )
380  {
381  var_table_size += it.key().lenVar();
382  if (++it == qh->end())
383  {
384  fixed_size = 0;
385  table_size = var_table_size;
386  break;
387  }
388  }
389 
390  // add space for the number of terms in the table and code bits
391  table_size += MemBitStream::lenVar(size_t(qh->size() - 1))
392  + MemBitStream::lenVar(code_bits - 1);
393 
394  // add space for the actual terms
395  table_size += hash->getTermCount() * code_bits;
396  }
397 
398  template<typename N>
399  inline
401  ()
402  const
403  {
404  return table_size;
405  }
406 
407  template<typename N>
408  inline
410  (MemBitStream* b)
411  const
412  {
413  #if SHOW_STATS
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)
418  _max_ht_pct = pct;
419  #endif
420 
421  // some info about the hash table
422  b->addVar(uint(qh->size() - 1));
423  b->addVar(code_bits - 1);
424  b->addVar(fixed_size);
425 
426  // write the hash table
427  uint i(0);
428  if (fixed_size)
429  {
430  for (typename QHash<N, uint>::iterator it(qh->begin()); it != qh->end(); ++it)
431  {
432  N n(it.key());
433  *it = i++;
434  n.addBits(fixed_size, b);
435  }
436  }
437  else
438  for (typename QHash<N, uint>::iterator it(qh->begin()); it != qh->end(); ++it)
439  {
440  N n(it.key());
441  *it = i++;
442  n.addVar(b);
443  }
444  }
445 
446  template<typename N>
447  inline
449  (const N& n,
450  MemBitStream* b)
451  const
452  {
453  b->addBits(code_bits, (*qh)[n]);
454  }
455 
456  template<typename N>
457  inline
459  (MemBitStream* b,
460  BytePool* pool)
461  {
462  uint num_codes(b->getVar<uint>() + 1);
463  codes.reserve(num_codes);
464  codes.clear();
465  code_bits = b->getVar<uint>() + 1;
466  if ((fixed_size = b->getVar<size_t>()))
467  {
468  while (num_codes--)
469  codes.push_back(N::getBits(b, fixed_size));
470  }
471  else
472  while (num_codes--)
473  codes.push_back(N::getVar(b, pool));
474  }
475 
476  template<typename N>
477  inline
479  (MemBitStream* b)
480  const
481  {
482  return codes[b->getBits<uint>(code_bits)];
483  }
484 
485  // ================================ VAR CODED ===============
486 
487  template<typename N>
488  inline
491  bool isRun)
492  {
493  hash = h;
494  qh = isRun ? hash->getRunsHash() : hash->getNormHash();
495  assert(qh->size());
496 
497  // calculate table size for fixed-size elts
498  fixed_size = hash->getNormMax().bitsNeeded();
499  table_size = MemBitStream::lenVar(fixed_size) + fixed_size * qh->size();
500 
501  // see if variable is smaller
502  size_t var_table_size(2); // place for fixed_size = 0
503  for (typename QHash<N, uint>::const_iterator it(qh->begin()); var_table_size < table_size; )
504  {
505  var_table_size += it.key().lenVar();
506  if (++it == qh->end())
507  {
508  fixed_size = 0;
509  table_size = var_table_size;
510  break;
511  }
512  }
513 
514  // add space for the number of terms in the table
515  table_size += MemBitStream::lenVar(size_t(qh->size() - 1));
516 
517  uint i(0);
518  freq_list.clear();
519  freq_list.reserve(qh->size());
520 
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()));
523 
524  std::sort(freq_list.begin(), freq_list.end());
525  freq_hash.clear();
526  freq_hash.reserve(qh->size());
527  i = 0;
528  for (typename std::vector<freq_elt>::const_iterator it(freq_list.begin()); it != freq_list.end(); ++it)
529  {
530  table_size += MemBitStream::lenVar(i) * (1 + it->freq);
531  freq_hash.insert(it->key, i++);
532  }
533  }
534 
535  template<typename N>
536  inline
538  ()
539  const
540  {
541  return table_size;
542  }
543 
544  template<typename N>
545  inline
547  (MemBitStream* b)
548  const
549  {
550  #if SHOW_STATS
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)
555  _max_ht_pct = pct;
556  #endif
557 
558  b->addVar(uint(freq_hash.size() - 1));
559  b->addVar(fixed_size);
560 
561  if (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);
564  else
565  for (typename std::vector<freq_elt>::const_iterator it(freq_list.begin()); it != freq_list.end(); ++it)
566  it->key.addVar(b);
567  }
568 
569  template<typename N>
570  inline
572  (const N& n,
573  MemBitStream* b)
574  const
575  {
576  b->addVar(freq_hash[n]);
577  }
578 
579  template<typename N>
580  inline
582  (MemBitStream* b,
583  BytePool* pool)
584  {
585  uint num_codes(b->getVar<uint>() + 1);
586  codes.reserve(num_codes);
587  codes.clear();
588  if ((fixed_size = b->getVar<size_t>()))
589  {
590  while (num_codes--)
591  codes.push_back(N::getBits(b, fixed_size));
592  }
593  else
594  while (num_codes--)
595  codes.push_back(N::getVar(b, pool));
596  }
597 
598  template<typename N>
599  inline
601  (MemBitStream* b)
602  const
603  {
604  uint i(b->getVar<uint>());
605  if (i >= codes.size())
606  throw MemBitStream::Corrupt(b->getBitPos());
607  return codes[i];
608  }
609 }
610 #endif
void set(const std::vector< N > *v, const N &maxValue)
Definition: elt_int.h:316
void startEncode(MemBitStream *b) const
Definition: elt_int.h:410
unsigned int uint
Definition: types.h:75
void startDecode(MemBitStream *b, BytePool *)
Definition: elt_int.h:459
bool _is_delta
uint _counts[16]
N decode(MemBitStream *b) const
Definition: elt_int.h:601
unsigned int uint
Definition: types_c.h:42
void startDecode(MemBitStream *b, BytePool *)
Definition: elt_int.h:354
void startEncode(MemBitStream *b) const
Definition: elt_int.h:547
void startDecode(MemBitStream *b, BytePool *pool)
Definition: elt_int.h:582
void encode(const N &n, MemBitStream *b) const
Definition: elt_int.h:344
QHash< N, uint > * getNormHash()
Definition: elt_int.h:45
size_t getLen() const
Definition: elt_int.h:538
uint getTermCount() const
Definition: elt_int.h:31
void set(eltIntInfo< N > *h, bool isRun)
Definition: elt_int.h:490
size_t getLen() const
Definition: elt_int.h:326
N getNormMax() const
Definition: elt_int.h:38
uint _max_ht_size
N decode(MemBitStream *b) const
Definition: elt_int.h:479
void startEncode(MemBitStream *n) const
Definition: elt_int.h:335
uint _max_ht_pct
QHash< N, uint > * getRunsHash()
Definition: elt_int.h:51
void encode(const N &n, MemBitStream *b) const
Definition: elt_int.h:572
size_t _sizes[16]
size_t getLen() const
Definition: elt_int.h:401
void set(eltIntInfo< N > *h, bool isRun)
Definition: elt_int.h:365
void encode(const N &norm, MemBitStream *b) const
Definition: elt_int.h:449
Xpace project main namespace
Definition: datetime.h:18
void set(const std::vector< N > &vals)
Definition: elt_int.h:278

current as of Wed Jun 10 2026 12:00:05