Package CHEM :: Package datatype :: Module compress :: Class EliasGammaCodec
[hide private]
[frames] | no frames]

Class EliasGammaCodec



Codec --+
        |
       EliasGammaCodec


Encode/Decode integer according to:
http://www.inference.phy.cam.ac.uk/mackay/itprnn/code/c/compress/

Integer must be >=1, unless codec is initialized with the "signed=True"

FORMAT:
  -run of zeros of length = 1 - bits needed to encode data
  -data, first bit MUST be 1
  -(optional) 1 bit for the sign

notice, that zero cannot be encoded by this scheme. So:

if data<=0: we subtract 1 before encoding (add 1 after decoding) to enable zero

also, the coding most efficiently enodes, numbers close to 1 (and does one the best).
The position of this optimal encoding can be adjusted by setting the "center" option.

OPTIONS:
  'sign' = DEFAULT=False, allow encoding data less than center?
  'center' = DEFAULT=1, most efficient encoding of this integer, and lower bound if sign=False



Instance Methods [hide private]
 
__init__(self, center=1, sign=False)
 
encodeL(self, data)
 
decodeI(self, iter)

Inherited from Codec: __call__, decode, finger

Method Details [hide private]

__init__(self, center=1, sign=False)
(Constructor)

 
>>> CA=EliasGammaCodec()
>>> print CA(1), CA(4)
1 00100
>>> for x in xrange(1,100): 
...   if x!=CA.decode(CA(x)): print x, CA(x), CA.decode(CA(x))
>>> CA=EliasGammaCodec(sign=True)
>>> print CA(1), CA(0), CA(-1)
11 10 0100
>>> for x in xrange(-100,100):
...   if x!=CA.decode(CA(x)): print x, CA(x), CA.decode(CA(x))
>>> CA=EliasGammaCodec(center=10)
>>> print CA(10)
1
>>> for x in xrange(10,100):
...   if x!=CA.decode(CA(x)): print x, CA(x), CA.decode(CA(x))
>>> CA=EliasGammaCodec(center=10)
>>> print CA(10)
1
>>> CA=EliasGammaCodec(center=10, sign=True)
>>> for x in xrange(0,20):
...   if x!=CA.decode(CA(x)): print x, CA(x), CA.decode(CA(x))
Overrides: Codec.__init__

encodeL(self, data)

 
Overrides: Codec.encodeL

decodeI(self, iter)

 
Overrides: Codec.decodeI