32 #ifndef PLANCK_COMPRESS_UTILS_H 33 #define PLANCK_COMPRESS_UTILS_H 46 std::vector<uint8> data;
49 template<
typename T>
void put_internal (
const T &val, uint8 bits)
51 int bitsleft = 8-(bitpos&7);
52 if (bitsleft==8) data.push_back(0);
55 data.back() |= ((val&((T(1)<<bits)-1))<<(bitsleft-bits));
60 data.back() |= ((val>>(bits-bitsleft))&((1<<bitsleft)-1));
62 put_internal(val,bits-bitsleft);
67 obitstream() : bitpos(0) {}
69 template<
typename T>
void put (
const T &val, uint8 bits)
72 planck_assert(bits<=(
sizeof(T)<<3),
"too many bits for this data type");
74 put_internal(val,bits);
77 const std::vector<uint8> &state()
const 84 const std::vector<uint8> data;
87 template<
typename T>
void get_internal (T &val,
int bits)
89 int bitsleft = 8-(bitpos&7);
92 val |= ((data[bitpos>>3]>>(bitsleft-bits))&((1<<bits)-1));
97 val |= T((data[bitpos>>3])&((1<<bitsleft)-1))<<(bits-bitsleft);
99 get_internal(val,bits-bitsleft);
103 ibitstream(
const std::vector<uint8> &indata) : data(indata), bitpos(0) {}
105 template<
typename T> T
get (uint8 bits)
107 if (bits==0)
return T(0);
108 planck_assert(bits<=(
sizeof(T)<<3),
"too many bits for this data type");
109 planck_assert((bitpos+bits)<=8*data.size(),
"reading past end of stream");
111 get_internal(res,bits);
116 template<
typename T,
bool>
struct notNegativeHelper__
117 { notNegativeHelper__(
const T &) {} };
118 template<
typename T>
struct notNegativeHelper__<T,true>
120 notNegativeHelper__(
const T &val)
123 template<
typename T>
void assertNotNegative(
const T &val)
124 { notNegativeHelper__<T,std::numeric_limits<T>::is_signed> dummy(val); }
126 template<
typename Iter>
void interpol_encode2 (Iter l, Iter r, obitstream &obs,
130 typedef std::iterator_traits<Iter> traits;
131 typedef typename traits::value_type T;
134 T nval = ((*r-*l)>>shift) - (r-l) + 1;
137 uint8 nb = 1+
ilog2(nval-1);
138 T val = ((*m)>>shift)-(((*l)>>shift)+(m-l));
139 T nshort=(T(1)<<nb)-nval;
142 T nrot=nval-(nshort>>1);
151 obs.put(val+nshort,nb);
152 interpol_encode2(l,m,obs,shift);
153 interpol_encode2(m,r,obs,shift);
156 template<
typename Iter>
void interpol_encode (Iter l, Iter r, obitstream &obs)
158 typedef std::iterator_traits<Iter> traits;
159 typedef typename traits::value_type T;
162 { obs.put(0,8);
return; }
164 assertNotNegative(*l);
167 for (Iter i=l+1; i!=r; ++i)
172 int shift = trailingZeros(combo);
173 T maxnum=(*(r-1))>>shift;
174 if (T(r-l)>maxnum) maxnum=T(r-l);
175 uint8 maxbits=1+
ilog2(maxnum);
178 obs.put(r-l,maxbits);
179 obs.put((*l)>>shift,maxbits);
181 obs.put((*(r-1))>>shift,maxbits);
182 interpol_encode2(l,r-1,obs,shift);
185 template<
typename Iter>
void interpol_decode2 (Iter l, Iter r, ibitstream &ibs,
189 typedef std::iterator_traits<Iter> traits;
190 typedef typename traits::value_type T;
193 T nval = ((*r-*l)>>shift) - (r-l) + 1;
198 uint8 nb = 1+
ilog2(nval-1);
199 T nshort=(T(1)<<nb)-nval;
200 val=ibs.get<T>(nb-1);
202 val=(val<<1)+ ibs.get<T>(1) - nshort;
205 T nrot=nval-(nshort>>1);
212 *m=*l+(((m-l)+val)<<shift);
214 interpol_decode2(l,m,ibs,shift);
215 interpol_decode2(m,r,ibs,shift);
218 template<
typename T>
void interpol_decode (std::vector<T> &v, ibitstream &ibs)
220 uint8 maxbits=ibs.get<uint8>(8);
221 if (maxbits==0) { v.clear();
return; }
222 int shift = ibs.get<
int>(8);
223 v.resize(ibs.get<
tsize>(maxbits));
224 v[0]=ibs.get<T>(maxbits)<<shift;
225 if (v.size()==1)
return;
226 v[v.size()-1]=ibs.get<T>(maxbits)<<shift;
227 interpol_decode2(v.begin(),v.end()-1,ibs,shift);
#define planck_assert(testval, msg)