Skip to content

Instantly share code, notes, and snippets.

@kc1212
Created December 2, 2015 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kc1212/b7981809673589dae168 to your computer and use it in GitHub Desktop.
Save kc1212/b7981809673589dae168 to your computer and use it in GitHub Desktop.
Testing b*tree in haskell
import Pipes
import qualified BTree as BT
import Control.Monad (unless, liftM)
import System.IO
-- probably don't need IO
listToProducer :: [BT.BLeaf String Int] -> Producer (BT.BLeaf String Int) IO ()
listToProducer [] = return ()
listToProducer a@(x:xs) =
unless (null a) $ do
yield x
listToProducer xs
producer1 = listToProducer [BT.BLeaf x y | (x,y) <- zip ["ab", "bc", "cd"] [30..90] ]
producer2 = listToProducer [BT.BLeaf x y | (x,y) <- zip ["aa", "bc", "cc"] [10..20] ]
getTreeIO :: Producer (BT.BLeaf String Int) IO () -> IO (BT.LookupTree String Int)
getTreeIO producer = do
let order = 10
let size = 100
hSetBuffering stdin LineBuffering
tree <- liftM BT.fromByteString (BT.fromOrderedToByteString order size producer)
return $ case tree of
Left _ -> error "impossible"
Right x -> x
tree1 = getTreeIO producer1
tree2 = getTreeIO producer2
merged :: IO (BT.LookupTree String Int)
merged = do
t1 <- tree1
t2 <- tree2
BT.mergeTrees (\a b -> return a) 10 fname [t1, t2]
-- ^^^^^^^^^^^^^^^^^ order to merge
eitherTree <- BT.open fname
return $ case eitherTree of
Left _ -> error "impossible!!"
Right x -> x
where fname = "/tmp/tree.tmp"
-- to test, do the following in ghci
-- t <- merged
-- BT.lookup t "bc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment