Refactored getT out of BootString

darcs-hash:20080120010829-a4fee-7d617d76cc2e67b0e33662510c9736367396d2ea
This commit is contained in:
Henning Guenther 2008-01-19 17:08:29 -08:00
parent 1510b86c10
commit 8d740cf88a

View File

@ -56,12 +56,12 @@ punyChar c
where where
norep = throwDyn OutOfRange norep = throwDyn OutOfRange
threshold :: BootString -> Int -> Int -> Int getT :: BootString -> Int -> Int -> Int
threshold bs bias pos getT bs k bias
| r > tmax bs = tmax bs | k <= bias + (tmin bs) = tmin bs
| r < tmin bs = tmin bs | k >= bias + (tmax bs) = tmax bs
where | otherwise = k-bias
r = (base bs)*(pos+1)-bias
adapt :: BootString -> Int -> Int -> Bool -> Int adapt :: BootString -> Int -> Int -> Bool -> Int
adapt bs delta numpoints firsttime = let adapt bs delta numpoints firsttime = let
@ -77,11 +77,7 @@ adapt bs delta numpoints firsttime = let
decodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int] -> (Int,[Int]) decodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int] -> (Int,[Int])
decodeValue bs bias i k w (x:xs) = let decodeValue bs bias i k w (x:xs) = let
ni = i + x*w ni = i + x*w
t = if k <= bias + (tmin bs) t = getT bs k bias
then tmin bs
else (if k >= bias + (tmax bs)
then tmax bs
else k-bias)
in if x < t in if x < t
then (ni,xs) then (ni,xs)
else decodeValue bs bias ni (k+base bs) (w*(base bs - t)) xs else decodeValue bs bias ni (k+base bs) (w*(base bs - t)) xs
@ -109,11 +105,7 @@ punyDecode base ext = insertDeltas (decodeValues punycode (length base) (map pun
encodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int] encodeValue :: BootString -> Int -> Int -> Int -> Int -> [Int]
encodeValue bs bias delta n c = unfoldr (\(q,k,out) -> let encodeValue bs bias delta n c = unfoldr (\(q,k,out) -> let
t = if k <= bias + tmin bs t = getT bs k bias
then tmin bs
else (if k >= bias + tmax bs
then tmax bs
else k - bias)
(nq,dc) = (q-t) `divMod` (base bs - t) (nq,dc) = (q-t) `divMod` (base bs - t)
in if out in if out
then Nothing then Nothing