00001 /* 00002 Copyright(c) 2002-2009 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com) 00003 00004 Permission is hereby granted, free of charge, to any person 00005 obtaining a copy of this software and associated documentation 00006 files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, 00008 publish, distribute, sublicense, and/or sell copies of the Software, 00009 and to permit persons to whom the Software is furnished to do so, 00010 subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included 00013 in all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00017 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00018 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00019 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00020 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00021 OTHER DEALINGS IN THE SOFTWARE. 00022 00023 For more information please visit: http://bmagic.sourceforge.net 00024 00025 */ 00026 00027 #ifndef BMGAMMAENC__H__INCLUDED__ 00028 #define BMGAMMAENC__H__INCLUDED__ 00029 00030 00031 namespace bm 00032 { 00033 00034 00035 /** 00036 Elias Gamma decoder 00037 */ 00038 template<typename T, typename TBitIO> 00039 class gamma_decoder 00040 { 00041 public: 00042 gamma_decoder(TBitIO& bin) : bin_(bin) 00043 {} 00044 00045 /** 00046 Start encoding sequence 00047 */ 00048 void start() 00049 {} 00050 00051 /** 00052 Stop decoding sequence 00053 */ 00054 void stop() 00055 {} 00056 00057 /** 00058 Decode word 00059 */ 00060 T operator()(void) 00061 { 00062 unsigned l = bin_.eat_zero_bits(); 00063 bin_.get_bit(); // get border bit 00064 T current = 0; 00065 for (unsigned i = 0; i < l; ++i) 00066 { 00067 if (bin_.get_bit()) 00068 { 00069 current += 1 << i; 00070 } 00071 } 00072 current |= (1 << l); 00073 return current; 00074 } 00075 private: 00076 gamma_decoder(const gamma_decoder&); 00077 gamma_decoder& operator=(const gamma_decoder&); 00078 private: 00079 TBitIO& bin_; 00080 }; 00081 00082 00083 00084 } // bm 00085 00086 #endif 00087