Skip to content

Commit

Permalink
name change: binaryBits expresses how many bits the Rice encoding takes
Browse files Browse the repository at this point in the history
  • Loading branch information
lsb committed Sep 18, 2012
1 parent abbf321 commit 6a36d21
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions GolombCode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import Data.List (unfoldr, foldl')
import Data.Word

golombCode :: Int -> Int -> [Bool]
golombCode !unaryBits !integer = encodeUnary quotient ++ encodeBinary unaryBits remainder
where (quotient, remainder) = integer `divMod` (2 ^ unaryBits)
golombCode !binaryBits !integer = encodeUnary quotient ++ encodeBinary binaryBits remainder
where (quotient, remainder) = integer `divMod` (2 ^ binaryBits)

encodeUnary !quotient = go quotient [False]
where go 0 bits = bits
go !n bits = go (n-1) (True : bits)

encodeBinary !unaryBits !integer = reverse $ unfoldr numToBools (unaryBits,integer)
encodeBinary !binaryBits !integer = reverse $ unfoldr numToBools (binaryBits,integer)
where numToBools (!mB,!i) = if mB == 0 then Nothing else Just (odd i, (mB-1, i `div` 2))

golombDecode :: Int -> [Bool] -> (Int, [Bool])
golombDecode unaryBits bits = (quotient * (2 ^ unaryBits) + remainder, rest)
golombDecode binaryBits bits = (quotient * (2 ^ binaryBits) + remainder, rest)
where (!quotient, binaryAndRest) = decodeUnary bits
(!remainder, rest) = decodeBinary unaryBits binaryAndRest
(!remainder, rest) = decodeBinary binaryBits binaryAndRest

decodeUnary bits = (length ones, rest)
where (ones, z : rest) = span id bits

decodeBinary unaryBits bits = (int, rest)
where (binaryBits, rest) = splitAt unaryBits bits
decodeBinary binaryBits bits = (int, rest)
where (binaryBits, rest) = splitAt binaryBits bits
int = boolsToInt binaryBits

boolsToInt :: (Num a) => [Bool] -> a
Expand All @@ -34,15 +34,15 @@ boolsToBytes (b7:(b6:(b5:(b4:(b3:(b2:(b1:(b0:nextBytes)))))))) padding = (boolsT
boolsToBytes [] padding = []
boolsToBytes fewBitsShort padding = [boolsToInt $ take 8 $ fewBitsShort ++ repeat padding]

decodeMany 0 unaryBits bits = []
decodeMany !n unaryBits bits = i : (decodeMany (n-1) unaryBits rest)
where (i,rest) = golombDecode unaryBits bits
decodeMany 0 binaryBits bits = []
decodeMany !n binaryBits bits = i : (decodeMany (n-1) binaryBits rest)
where (i,rest) = golombDecode binaryBits bits

golombCodesNoBlocks :: Int -> [Int] -> [Word8]
golombCodesNoBlocks !unaryBits ints = boolsToBytes (concatMap (golombCode unaryBits) ints) False
golombCodesNoBlocks !binaryBits ints = boolsToBytes (concatMap (golombCode binaryBits) ints) False

golombDecodesNoBlocks :: Int -> Integer -> [Word8] -> [Int]
golombDecodesNoBlocks !unaryBits !count word8s = decodeMany count unaryBits (concatMap (encodeBinary 8) word8s)
golombDecodesNoBlocks !binaryBits !count word8s = decodeMany count binaryBits (concatMap (encodeBinary 8) word8s)

linewiseDiff :: [Integer] -> [Int]
linewiseDiff ints = zipWith (\ a b -> fromIntegral (a-b) ) ints (0:ints)
Expand Down
4 changes: 2 additions & 2 deletions golomb-compressed-sequences.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import System.Environment (getArgs)
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe (fromJust)

main = do [unaryBits] <- getArgs
B.interact (B.pack . map (toEnum . fromEnum) . golombCodesNoBlocks (read unaryBits) . linewiseDiff . map (fst . fromJust . B.readInteger) . B.lines)
main = do [binaryBits] <- getArgs
B.interact (B.pack . map (toEnum . fromEnum) . golombCodesNoBlocks (read binaryBits) . linewiseDiff . map (fst . fromJust . B.readInteger) . B.lines)
4 changes: 2 additions & 2 deletions golomb-query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import System.Environment (getArgs)
import qualified Data.ByteString.Lazy as BWord
import qualified Data.ByteString.Lazy.Char8 as BChar

main = do [lineCount, modulus, unaryBits, gcsFile] <- getArgs -- todo: use the -index via aeson
main = do [lineCount, modulus, binaryBits, gcsFile] <- getArgs -- todo: use the -index via aeson
f <- BWord.readFile gcsFile
queries <- BChar.getContents
BChar.putStr $ BChar.unlines $ golombFilterQueries (read lineCount) (read modulus) (read unaryBits) f (BChar.lines queries)
BChar.putStr $ BChar.unlines $ golombFilterQueries (read lineCount) (read modulus) (read binaryBits) f (BChar.lines queries)

0 comments on commit 6a36d21

Please sign in to comment.