tudocomp
– The TU Dortmund Compression Framework
vbyte.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/util.hpp>
4 
5 namespace tdc {
6 
10 template<class int_t>
11 inline int_t read_vbyte(std::istream& is) {
12  constexpr size_t data_width = sizeof(std::istream::char_type)*8 - 1; // each byte of a vbyte-array stores 7bits of data, 1bit for marking that this byte is not the end byte
13  int_t ret = 0;
14  uint8_t which_byte = 0;
15  while(is.good()) {
16  uint8_t byte = is.get();
17  ret |= (byte & ((1UL<<data_width)-1))<<(data_width * which_byte++);
18  if( !(byte & (1UL<<data_width))) return ret;
19  }
20  DCHECK(false) << "VByte ended without reading a byte with the most significant bit equals zero.";
21  return 0;
22 }
23 
28 template<class int_t>
29 inline void write_vbyte(std::ostream& os, int_t v) {
30  constexpr size_t data_width = sizeof(std::ostream::char_type)*8 - 1; // each byte of a vbyte-array stores 7bits of data, 1bit for marking that this byte is not the end byte
31  do {
32  uint8_t byte = v & ((1UL<<data_width)-1);
33  v >>= data_width;
34  if(v > 0) byte |= (1UL<<data_width);
35  os << byte;
36  } while(v > 0);
37 }
38 
39 
40 }//ns
41 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
void write_vbyte(std::ostream &os, int_t v)
Store an integer as a bunch of bytes.
Definition: vbyte.hpp:29
int_t read_vbyte(std::istream &is)
Reads an integer stored as a bunch of bytes in the vbyte-encoding.
Definition: vbyte.hpp:11