Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created October 11, 2012 09:19
Show Gist options
  • Select an option

  • Save tanakh/3871207 to your computer and use it in GitHub Desktop.

Select an option

Save tanakh/3871207 to your computer and use it in GitHub Desktop.
binary-shared test
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Binary as B
import Data.Binary.Shared as S
import qualified Data.ByteString.Lazy as L
import Data.Typeable
import Data.List
import System.Random
import Control.Monad
data Bin a = Bin (Bin a) (Bin a) | Tip a
deriving (Typeable, Ord, Eq, Show)
instance Binary a => Binary (Bin a) where
put v = case v of
Tip n ->
B.put False >> B.put n
Bin l r ->
B.put True >> B.put l >> B.put r
get = undefined
instance BinaryShared a => BinaryShared (Bin a) where
put = S.putShared $ \v -> case v of
Tip n ->
S.put False >> S.put n
Bin l r ->
S.put True >> S.put l >> S.put r
get = undefined
gen :: Int -> Bin Int
gen 0 = Tip 0
gen n = let b = gen (n - 1) in Bin b b
suffixSort :: String -> [String]
suffixSort = sort . tails
main :: IO ()
main = do
print . L.length . B.encode $ gen 20 -- 10485759
print . L.length . S.encodeSer $ gen 20 -- 389
str <- replicateM 1000 $ randomRIO ('a', 'z')
let sa = suffixSort str
print . L.length . B.encode $ sa -- 508516
print . L.length . S.encodeSer $ sa -- 517534 <- ???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment