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
def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
def encode(string, c0=BETA0, c1=BETA1, adaptive=True):
assert c0 > 0
assert c1 > 0
@ -29,7 +29,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
a = 0
tot0 = 0
tot1 = 0
if adaptive == 0:
if not adaptive:
p0 = c0 * 1 / (c0 + c1)
ans = ""
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
if adaptive:
cT = c0 + c1
p0 = c0 * 1.0 / cT
p0 = c0 * 1 / cT
boundary = a + int(p0 * w)
# 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
tot1 += 1
if adaptive:
c1 += 1.0
c1 += 1
elif c == '0':
b = boundary
tot0 += 1
if adaptive:
c0 += 1.0
c0 += 1
# ignore other characters
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
while a > QUARTER and b < THREEQU:
charstack[0] += 1
a = 2*a-HALF
b = 2*b-HALF
a += a - HALF
b += b - HALF
assert a <= HALF
assert b >= HALF
@ -107,7 +107,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
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.
assert c0 > 0
assert c1 > 0
@ -116,8 +116,8 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
a = 0
tot0 = 0
tot1 = 0
model_needs_updating = 1
if adaptive == 0:
model_needs_updating = True
if not adaptive:
p0 = c0 * 1 / (c0 + c1)
ans = ""
@ -142,37 +142,37 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0):
# Read bits until we can decide what the source symbol was.
# Then emulate the encoder's computations,
# and tie (u,v) to tag along for the ride.
while 1:
while 1: # do-while
firsttime = 0
if model_needs_updating:
w = b - a
if adaptive:
cT = c0 + c1
p0 = c0 * 1 / cT
boundary = a + int(p0*w)
boundary = a + int(p0 * w)
if boundary == a:
boundary += 1
print("warningA")
if boundary == b:
boundary -= 1
print("warningB")
model_needs_updating = 0
model_needs_updating = False
if boundary <= u:
ans += "1"
tot1 += 1
if adaptive:
c1 += 1.0
c1 += 1
a = boundary
model_needs_updating = 1
model_needs_updating = True
N -= 1
elif boundary >= v:
ans += "0"
tot0 += 1
if adaptive:
c0 += 1.0
c0 += 1
b = boundary
model_needs_updating = 1
model_needs_updating = True
N -= 1
else:
# 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.
while a >= HALF or b <= HALF:
if a >= HALF:
a = a - HALF
b = b - HALF
u = u - HALF
v = v - HALF
a -= HALF
b -= HALF
u -= HALF
v -= HALF
a *= 2
b *= 2
u *= 2
v *= 2
model_needs_updating = 1
model_needs_updating = True
assert a <= 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
while a > QUARTER and b < THREEQU:
a = 2 * a - HALF
b = 2 * b - HALF
u = 2 * u - HALF
v = 2 * v - HALF
a *= 2
b *= 2
u *= 2
v *= 2
a -= HALF
b -= HALF
u -= HALF
v -= HALF
# this is the condition for this do-while loop
if not (N > 0 and model_needs_updating):