Created
October 11, 2012 09:19
-
-
Save tanakh/3871207 to your computer and use it in GitHub Desktop.
binary-shared test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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