BitMagic-C++: bmgamma.h Source File

BitMagic-C++

bmgamma.h
Go to the documentation of this file.
1 #ifndef BMGAMMAENC__H__INCLUDED__
2 #define BMGAMMAENC__H__INCLUDED__
3 /*
4 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 For 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
26 namespace 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 */
41 template<typename T, typename TBitIO>
42 class gamma_decoder
43 {
44 public:
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 }
78 private:
79 gamma_decoder(const gamma_decoder&);
80 gamma_decoder& operator=(const gamma_decoder&);
81 private:
82 TBitIO& bin_;
83 };
84
85
86
87 } // bm
88
89 #endif
90
bm::gamma_decoder::gamma_decoder
gamma_decoder(TBitIO &bin)
Definition: encoding.h:350
bm::gamma_decoder::operator()
T operator()(void) BMNOEXEPT
Decode word.
Definition: bmgamma.h:63
bm::gamma_decoder::start
void start() BMNOEXEPT
Start encoding sequence.
Definition: bmgamma.h:51
bm::gamma_decoder::stop
void stop() BMNOEXEPT
Stop decoding sequence.
Definition: bmgamma.h:57
bm::gamma_decoder
Elias Gamma decoder.
Definition: encoding.h:347
bm
Definition: bm.h:76
bm::gamma_decoder::gamma_decoder
gamma_decoder(TBitIO &bin) BMNOEXEPT
Definition: bmgamma.h:45