Created
December 2, 2015 18:35
-
-
Save kc1212/b7981809673589dae168 to your computer and use it in GitHub Desktop.
Testing b*tree in haskell
This file contains 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
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