BitMagic-C++
bmgamma.h
Go to the documentation of this file.
1#ifndef BMGAMMAENC__H__INCLUDED__
2#define BMGAMMAENC__H__INCLUDED__
3/*
4Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18For more information please visit: http://bitmagic.io
19*/
20
21/*! \file bmgamma.h
22 \brief Elias Gamma Utils used for compact serialization (internal)
23*/
24
25
26namespace bm
27{
28
29/*!
30 \defgroup gammacode Elias Gamma Code (internal)
31 Elias Gamma Encoder
32 @internal
33 \ingroup bvserial
34
35 */
36
37/**
38 Elias Gamma decoder
39 \ingroup gammacode
40*/
41template<typename T, typename TBitIO>
42class gamma_decoder
43{
44public:
45 gamma_decoder(TBitIO& bin) BMNOEXEPT : bin_(bin)
46 {}
47
48 /**
49 Start encoding sequence
50 */
51 void start() BMNOEXEPT
52 {}
53
54 /**
55 Stop decoding sequence
56 */
57 void stop() BMNOEXEPT
58 {}
59
60 /**
61 Decode word
62 */
63 T operator()(void) BMNOEXEPT
64 {
65 unsigned l = bin_.eat_zero_bits();
66 bin_.get_bit(); // get border bit
67 T current = 0;
68 for (unsigned i = 0; i < l; ++i)
69 {
70 if (bin_.get_bit())
71 {
72 current += 1 << i;
73 }
74 }
75 current |= (1 << l);
76 return current;
77 }
78private:
80 gamma_decoder& operator=(const gamma_decoder&);
81private:
82 TBitIO& bin_;
83};
84
85
86
87} // bm
88
89#endif
90
Elias Gamma decoder.
Definition: encoding.h:362
void stop() BMNOEXEPT
Stop decoding sequence.
Definition: bmgamma.h:57
T operator()(void) BMNOEXEPT
Decode word.
Definition: bmgamma.h:63
void start() BMNOEXEPT
Start encoding sequence.
Definition: bmgamma.h:51
gamma_decoder(TBitIO &bin) BMNOEXEPT
Definition: bmgamma.h:45
gamma_decoder(TBitIO &bin)
Definition: encoding.h:364
Definition: bm.h:78