compactmap 0.1.2 → 0.1.3.1
raw patch · 2 files changed
+29/−33 lines, 2 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
Files
- compactmap.cabal +1/−1
- src/Data/CompactMap.hs +28/−32
compactmap.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: compactmap-version: 0.1.2+version: 0.1.3.1 synopsis: A read-only memory-efficient key-value store. -- description: license: BSD3
src/Data/CompactMap.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} module Data.CompactMap ( CompactMap , fromList@@ -7,57 +8,52 @@ , getIndex ) where +import qualified Data.CompactMap.Generic as CM import qualified Data.Vector as V-import GHC.Exts (sortWith) import Data.Foldable as F-import Control.Applicative import Prelude hiding (lookup) -data CompactMap k v = CompactMap (V.Vector v) (v -> k)+newtype CompactMap k v = CompactMap { getCM :: CM.CompactMap V.Vector k v } instance Show v => Show (CompactMap k v) where show = show . V.toList . getMap instance Foldable (CompactMap k) where- foldr f i = F.foldr f i . getMap- foldMap f = F.foldMap f . getMap+ fold = F.fold . getMap+ foldr f i = V.foldr f i . getMap+ foldr' f i = V.foldr' f i . getMap+ foldr1 f = V.foldr1 f . getMap+ foldl f i = V.foldl f i . getMap+ foldl' f i = V.foldl' f i . getMap+ foldl1 f = V.foldl1 f . getMap+ foldMap f = F.foldMap f . getMap+#if MIN_VERSION_base(4,8,0)+ toList = F.toList . getMap+ elem e = F.elem e . getMap+ null = F.null . getMap+ minimum = F.minimum . getMap+ maximum = F.maximum . getMap+ length = F.length . getMap+ sum = F.sum . getMap+ product = F.product . getMap+#endif getMap :: CompactMap k v -> V.Vector v-getMap (CompactMap lst _) = lst+getMap = CM.toVector . getCM {-# INLINE getMap #-} fromList :: Ord k => [v] -> (v -> k) -> CompactMap k v-fromList lst f = CompactMap (V.fromList $ sortWith f lst) f+fromList lst f = CompactMap (CM.fromList lst f) toVector :: CompactMap k v -> V.Vector v-toVector (CompactMap v _) = v+toVector = getMap+{-# INLINE toVector #-} lookup :: Ord k => k -> CompactMap k v -> Maybe v-lookup k cm@(CompactMap _ f) = getLE k cm >>= \(_,r) -> if f r == k- then Just r- else Nothing+lookup k (CompactMap cm) = CM.lookup k cm getLE :: Ord k => k -> CompactMap k v -> Maybe (Int, v)-getLE k (CompactMap lst f) = go 0 (sz - 1)- where- sz = V.length lst- go l h- | m < l = Nothing- | m > h = Nothing- | k' == k = Just (m, x)- | l >= h = checkLower <|> Just (m, x)- | k < k' = checkLower <|> go l (m - 1)- | otherwise = go (m + 1) h- where- checkLower- | m < 1 = Nothing- | k > k_1 = Just (m - 1, x_1)- | otherwise = Nothing- m = (l + h) `div` 2- x = lst V.! m- k' = f x- x_1 = lst V.! (m - 1)- k_1 = f x_1+getLE k (CompactMap cm) = CM.getLE k cm getIndex :: Int -> CompactMap k v -> Maybe v-getIndex i (CompactMap lst _) = lst V.!? i+getIndex i (CompactMap cm) = CM.getIndex i cm