This commit is contained in:
Connor 2018-09-10 12:23:05 -07:00 committed by GitHub
parent 74cd548adb
commit 207603e462

View file

@ -21,7 +21,7 @@ def clear(c, charstack):
return a return a
def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
assert c0 > 0 assert c0 > 0
assert c1 > 0 assert c1 > 0
@ -29,7 +29,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
a = 0 a = 0
tot0 = 0 tot0 = 0
tot1 = 0 tot1 = 0
if adaptive == 0: if not adaptive:
p0 = c0 * 1 / (c0 + c1) p0 = c0 * 1 / (c0 + c1)
ans = "" ans = ""
charstack = [0] # how many undecided characters remain to print charstack = [0] # how many undecided characters remain to print
@ -38,7 +38,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
w = b - a w = b - a
if adaptive: if adaptive:
cT = c0 + c1 cT = c0 + c1
p0 = c0 * 1.0 / cT p0 = c0 * 1 / cT
boundary = a + int(p0 * w) boundary = a + int(p0 * w)
# these warnings mean that some of the probabilities # these warnings mean that some of the probabilities
@ -56,12 +56,12 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
a = boundary a = boundary
tot1 += 1 tot1 += 1
if adaptive: if adaptive:
c1 += 1.0 c1 += 1
elif c == '0': elif c == '0':
b = boundary b = boundary
tot0 += 1 tot0 += 1
if adaptive: if adaptive:
c0 += 1.0 c0 += 1
# ignore other characters # ignore other characters
while a >= HALF or b <= HALF: # output bits while a >= HALF or b <= HALF: # output bits
@ -82,8 +82,8 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
# if the gap a-b is getting small, rescale it # if the gap a-b is getting small, rescale it
while a > QUARTER and b < THREEQU: while a > QUARTER and b < THREEQU:
charstack[0] += 1 charstack[0] += 1
a = 2*a-HALF a += a - HALF
b = 2*b-HALF b += b - HALF
assert a <= HALF assert a <= HALF
assert b >= HALF assert b >= HALF
@ -107,7 +107,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
return ans return ans
def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True):
# must supply N, the number of source characters remaining. # must supply N, the number of source characters remaining.
assert c0 > 0 assert c0 > 0
assert c1 > 0 assert c1 > 0
@ -116,8 +116,8 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
a = 0 a = 0
tot0 = 0 tot0 = 0
tot1 = 0 tot1 = 0
model_needs_updating = 1 model_needs_updating = True
if adaptive == 0: if not adaptive:
p0 = c0 * 1 / (c0 + c1) p0 = c0 * 1 / (c0 + c1)
ans = "" ans = ""
@ -142,7 +142,7 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
# Read bits until we can decide what the source symbol was. # Read bits until we can decide what the source symbol was.
# Then emulate the encoder's computations, # Then emulate the encoder's computations,
# and tie (u,v) to tag along for the ride. # and tie (u,v) to tag along for the ride.
while 1: while 1: # do-while
firsttime = 0 firsttime = 0
if model_needs_updating: if model_needs_updating:
w = b - a w = b - a
@ -156,23 +156,23 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
if boundary == b: if boundary == b:
boundary -= 1 boundary -= 1
print("warningB") print("warningB")
model_needs_updating = 0 model_needs_updating = False
if boundary <= u: if boundary <= u:
ans += "1" ans += "1"
tot1 += 1 tot1 += 1
if adaptive: if adaptive:
c1 += 1.0 c1 += 1
a = boundary a = boundary
model_needs_updating = 1 model_needs_updating = True
N -= 1 N -= 1
elif boundary >= v: elif boundary >= v:
ans += "0" ans += "0"
tot0 += 1 tot0 += 1
if adaptive: if adaptive:
c0 += 1.0 c0 += 1
b = boundary b = boundary
model_needs_updating = 1 model_needs_updating = True
N -= 1 N -= 1
else: else:
# not enough bits have yet been read to know the decision. # not enough bits have yet been read to know the decision.
@ -182,15 +182,15 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
# and tie (u,v) to tag along for the ride. # and tie (u,v) to tag along for the ride.
while a >= HALF or b <= HALF: while a >= HALF or b <= HALF:
if a >= HALF: if a >= HALF:
a = a - HALF a -= HALF
b = b - HALF b -= HALF
u = u - HALF u -= HALF
v = v - HALF v -= HALF
a *= 2 a *= 2
b *= 2 b *= 2
u *= 2 u *= 2
v *= 2 v *= 2
model_needs_updating = 1 model_needs_updating = True
assert a <= HALF assert a <= HALF
assert b >= HALF assert b >= HALF
@ -199,10 +199,14 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
# if the gap a-b is getting small, rescale it # if the gap a-b is getting small, rescale it
while a > QUARTER and b < THREEQU: while a > QUARTER and b < THREEQU:
a = 2 * a - HALF a *= 2
b = 2 * b - HALF b *= 2
u = 2 * u - HALF u *= 2
v = 2 * v - HALF v *= 2
a -= HALF
b -= HALF
u -= HALF
v -= HALF
# this is the condition for this do-while loop # this is the condition for this do-while loop
if not (N > 0 and model_needs_updating): if not (N > 0 and model_needs_updating):