{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE MultiParamTypeClasses #-}import Criterion.Mainimport Criterion.Types (Config(..))import Control.DeepSeqimport Prelude hiding (lookup, max, foldr)import System.Randomimport Data.Foldable (foldr)import Data.List (sort, foldl')import Data.IntervalMap.Generic.Intervalimport qualified Data.IntervalSet as Sseed :: Intseed = 54321ensure :: NFData a => a -> IO aensure xs = xs `deepseq` return xsforceRange :: Int -> Int -> Int -> IntforceRange lo hi n | n >= lo && n <= hi = n | n < 0 = forceRange lo hi (0 - n) | otherwise = lo + (n `rem` (1 + hi - lo))genRandomInts :: Int -> Int -> Int -> [Int]genRandomInts lo hi n = Prelude.map (forceRange lo hi) . take n . randoms . mkStdGen $ seedgenRandomIntervals :: Int -> Int -> Int -> [(Int,Int)]genRandomIntervals max lap n = genIvs . take (2*n) . randoms . mkStdGen $ seed where genIvs [] = [] genIvs [_] = [] genIvs (x:y:xs) = let lo = forceRange 1 max x sz = forceRange 0 lap y in (lo, lo + sz) : genIvs xsbenchConfig :: ConfigbenchConfig = defaultConfig { reportFile = Just "bench-set.html" }cDATA_SIZE :: IntcDATA_SIZE = 10000data IV = IV {-# UNPACK #-} !Int {-# UNPACK #-} !Int deriving (Eq, Ord)instance NFData IV where rnf a = a `seq` ()instance Interval IV Int where lowerBound (IV l _) = l upperBound (IV _ u) = umove :: Int -> IV -> IVmove n (IV a b) = IV (a+n) (b+n)main :: IO ()main = do let ivs = genRandomIntervals cDATA_SIZE 50 cDATA_SIZE ivsP <- ensure $ [IV lo hi | (lo,hi) <- ivs] oIvsP <- ensure $ sort ivsP let lookupKeys = ivsP set <- ensure $ S.fromList ivsP rndInts <- ensure (genRandomInts 1 cDATA_SIZE cDATA_SIZE) rndInts2 <- ensure (take (2 * log2 cDATA_SIZE) rndInts) rndIvs <- ensure [IV (p - 500) (p + 500) | p <- rndInts2] defaultMainWith benchConfig [ bgroup "fromList" [ bench "fromList" $ nf S.fromList ivsP, bench "fromAscList" $ nf S.fromAscList oIvsP ], bench "size" $ whnf S.size set, bench "member" $ whnf (\s -> sum [bval (S.member i s) | i <- lookupKeys]) set, bgroup "points" [ bench "containing" $ whnf (\s -> sum [maxValue (S.containing s p) | p <- rndInts]) set, bench "splitAt lo" $ whnf (\s -> sum [maxValue (splitAt1 s p) | p <- rndInts2]) set, bench "splitAt c" $ whnf (\s -> sum [maxValue (splitAt2 s p) | p <- rndInts]) set, bench "splitAt hi" $ whnf (\s -> sum [minValue (splitAt3 s p) | p <- rndInts2]) set ], bgroup "intervals" [ bench "intersecting" $ whnf (\s -> sum [maxValue (s `S.intersecting` i) | i <- rndIvs]) set, bench "within" $ whnf (\s -> sum [maxValue (s `S.within` i) | i <- rndIvs]) set, bench "spi1" $ whnf (\s -> sum [maxValue (spi1 s i) | i <- rndIvs]) set, bench "spi2" $ whnf (\s -> sum [maxValue (spi2 s i) | i <- rndIvs]) set, bench "spi3" $ whnf (\s -> sum [maxValue (spi3 s i) | i <- rndIvs]) set ], bgroup "map" [ bench "map" $ nf (S.map (move 1)) set, bench "monotonic" $ nf (S.mapMonotonic (move 1)) set ], bgroup "delete" [ bench "deleteMin" $ nf S.deleteMin set, bench "deleteMax" $ nf S.deleteMax set, bench "delete" $ nf (\(k,s) -> S.delete k s) (head rndIvs, set) ] ]log2 :: Int -> Intlog2 m = h (-1) m where h r n | r `seq` n <= 0 = r | otherwise = h (r + 1) (n `quot` 2)bval :: Bool -> Intbval False = 0bval True = 1minValue :: S.IntervalSet IV -> IntminValue s = case S.findMin s of Nothing -> 0 Just (IV lo _) -> lomaxValue :: S.IntervalSet IV -> IntmaxValue s = case S.findMax s of Nothing -> 0 Just (IV lo _) -> lo splitAt1, splitAt2, splitAt3 :: (Interval i e) => S.IntervalSet i -> e -> S.IntervalSet isplitAt1 s p = case S.splitAt s p of (lo,_,_) -> losplitAt2 s p = case S.splitAt s p of (_,c,_) -> csplitAt3 s p = case S.splitAt s p of (_,_,hi) -> hispi1, spi2, spi3 :: (Interval i e, Ord i) => S.IntervalSet i -> i -> S.IntervalSet ispi1 s i = case S.splitIntersecting s i of (x,_,_) -> xspi2 s i = case S.splitIntersecting s i of (_,x,_) -> xspi3 s i = case S.splitIntersecting s i of (_,_,x) -> x