packages feed

IntervalMap-0.5.2.0 / bench / IvMapSortedList.hs

module IvMapSortedList (IVS, size, empty, singleton, insert, insertWithKey, lookup, containing, fromList) whereimport Prelude hiding (lookup)import Data.IntervalMap.Generic.Intervalimport Control.DeepSeqimport Data.List (sortBy)data Entry k v = E !k !v deriving (Eq, Ord, Show)instance (NFData k, NFData v) => NFData (Entry k v) where  rnf (E k v) = k `deepseq` v `deepseq` ()newtype IVS k v = IVS [Entry k v] deriving (Eq, Ord, Show)instance (NFData k, NFData v) => NFData (IVS k v) where  rnf (IVS v) = v `deepseq` ()size :: IVS k v -> Intsize (IVS es) = length esempty :: IVS k vempty = IVS []singleton :: k -> v -> IVS k vsingleton k v = IVS [E k v]insert :: Ord k => k -> v -> IVS k v -> IVS k vinsert =  insertWithKey (\_ val _ -> val)insertWithKey :: Ord k => (k -> v -> v -> v) -> k -> v -> IVS k v -> IVS k vinsertWithKey f key val (IVS m) = IVS (go m)  where    go [] = [E key val]     go es@(e@(E k v) : es') = case compare key k of                                GT -> e : go es'                                LT -> E key val : es                                EQ -> E key (f key val v) : es'lookup :: Ord k => k -> IVS k v -> Maybe vlookup key (IVS m) = key `seq` go m  where    go (E k v : es) = case compare key k of                        GT -> go es                        EQ -> Just v                        LT -> Nothing    go [] = Nothingcontaining :: (Interval k e) => IVS k v -> e -> [(k, v)](IVS m) `containing` p = p `seq` go m  where    go (E k v : es) | p `above` k = go es                    | p `below` k = []                    | otherwise   = (k,v) : go es    go [] = []fromList :: Ord k => [(k, v)] -> IVS k vfromList =  foldr (\(k,v) m -> insert k v m) empty . sortBy cmpKey  where cmpKey (k1,_) (k2,_) = compare k1 k2