pykinos-containers-0.1.1.0: src/Pykinos/Containers/Map/Strict.hs
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Module: Pykinos.Containers.Map.Strict
-- Description: Drop-in Data.Map.Strict façade backed by cache-blocked B+ trees
--
-- Provides drop-in API parity with @Data.Map.Strict@ backed by cache-conscious
-- serried B+ tree nodes ('BPlus.BPlusMap').
--
-- = Two-Constructor GADT Mechanics
--
-- Standard B+ trees require ordering constraints for construction and insertion,
-- which normally impedes polymorphic value transformations.
-- This module employs a two-constructor GADT ('BlockedMap' and 'LiftedValMap')
-- allowing 'Functor', 'Foldable', and 'Traversable' instances to operate without
-- requiring @Ord k@ constraints on mapped types.
module Pykinos.Containers.Map.Strict
( -- * Map Type
Map(..)
-- * Construction
, empty
, singleton
, fromList
-- * Queries
, null
, size
, member
, notMember
, lookup
, findWithDefault
-- * Modifications
, insert
, delete
-- * Combinators
, union
, intersection
-- * Conversions & Projections
, toAscList
, toList
, keys
, elems
) where
import Prelude hiding (lookup, null)
import Control.DeepSeq (NFData(..))
import Data.Maybe (fromMaybe)
import qualified Pykinos.Tree.BPlus as BPlus
-- | Strict ordered map backed by cache-blocked @Pykinos.Tree.BPlus@ nodes.
--
-- 'BlockedMap' represents newly constructed or updated unboxed maps.
-- 'LiftedValMap' represents maps whose values have been transformed via 'fmap'
-- or 'traverse', preserving key block layouts zero-copy without @Ord k@ constraints.
data Map k v where
BlockedMap :: !(BPlus.BPlusMap k v) -> Map k v
LiftedValMap :: !(BPlus.BPlusMap k v) -> Map k v
instance (Eq k, Eq v) => Eq (Map k v) where
m1 == m2 = toAscList m1 == toAscList m2
instance (Show k, Show v) => Show (Map k v) where
showsPrec p m = showParen (p > 10) $
showString "fromList " . showsPrec 11 (toAscList m)
instance Ord k => Semigroup (Map k v) where
(<>) = union
{-# INLINE (<>) #-}
instance Ord k => Monoid (Map k v) where
mempty = empty
{-# INLINE mempty #-}
instance (NFData k, NFData v) => NFData (Map k v) where
rnf (BlockedMap t) = rnf (BPlus.toAscListBPlus t)
rnf (LiftedValMap t) = rnf (BPlus.toAscListBPlus t)
{-# INLINE rnf #-}
instance Functor (Map k) where
fmap f (BlockedMap t) = LiftedValMap (fmap f t)
fmap f (LiftedValMap t) = LiftedValMap (fmap f t)
{-# INLINE fmap #-}
instance Foldable (Map k) where
foldMap f (BlockedMap t) = foldMap f t
foldMap f (LiftedValMap t) = foldMap f t
{-# INLINE foldMap #-}
instance Traversable (Map k) where
traverse f (BlockedMap t) = LiftedValMap <$> traverse f t
traverse f (LiftedValMap t) = LiftedValMap <$> traverse f t
{-# INLINE traverse #-}
-- | Construct the empty map.
--
-- Running time: \(O(1)\).
empty :: Map k v
empty = BlockedMap BPlus.emptyBPlus
{-# INLINE empty #-}
-- | Construct a map containing a single key-value association.
--
-- Running time: \(O(1)\).
singleton :: Ord k => k -> v -> Map k v
singleton k !v = BlockedMap (BPlus.insertBPlus k v BPlus.emptyBPlus)
{-# INLINE singleton #-}
-- | Test whether the map is empty.
--
-- Running time: \(O(1)\).
null :: Map k v -> Bool
null m = size m == 0
{-# INLINE null #-}
-- | Number of associations in the map.
--
-- Running time: \(O(1)\).
size :: Map k v -> Int
size (BlockedMap t) = BPlus.bpmSize t
size (LiftedValMap t) = BPlus.bpmSize t
{-# INLINE size #-}
-- | Query key membership.
--
-- Running time: \(O(\log_B N)\) where \(B = 16\).
member :: Ord k => k -> Map k v -> Bool
member k m = case lookup k m of
Just _ -> True
Nothing -> False
{-# INLINE member #-}
-- | Query key non-membership.
notMember :: Ord k => k -> Map k v -> Bool
notMember k m = not (member k m)
{-# INLINE notMember #-}
-- | Lookup the value associated with a key.
--
-- Running time: \(O(\log_B N)\).
lookup :: Ord k => k -> Map k v -> Maybe v
lookup k (BlockedMap t) = BPlus.lookupBPlus k t
lookup k (LiftedValMap t) = BPlus.lookupBPlus k t
{-# INLINE lookup #-}
-- | Lookup with fallback default value.
findWithDefault :: Ord k => v -> k -> Map k v -> v
findWithDefault def k m = fromMaybe def (lookup k m)
{-# INLINE findWithDefault #-}
-- | Insert a key-value pair, overwriting any previous binding for that key.
insert :: Ord k => k -> v -> Map k v -> Map k v
insert k !v (BlockedMap t) = BlockedMap (BPlus.insertBPlus k v t)
insert k !v (LiftedValMap t) = LiftedValMap (BPlus.insertBPlus k v t)
{-# INLINE insert #-}
-- | Delete a key from the map.
delete :: Ord k => k -> Map k v -> Map k v
delete k m = fromList [ (k', v) | (k', v) <- toAscList m, k' /= k ]
{-# INLINE delete #-}
-- | Construct a map from an association list.
fromList :: Ord k => [(k, v)] -> Map k v
fromList = foldl' (\acc (k, !v) -> insert k v acc) empty
{-# INLINE fromList #-}
-- | Convert map associations to a list in ascending key order.
toAscList :: Map k v -> [(k, v)]
toAscList (BlockedMap t) = BPlus.toAscListBPlus t
toAscList (LiftedValMap t) = BPlus.toAscListBPlus t
{-# INLINE toAscList #-}
-- | Alias for 'toAscList'.
toList :: Map k v -> [(k, v)]
toList = toAscList
{-# INLINE toList #-}
-- | List of all keys in ascending order.
keys :: Map k v -> [k]
keys = map fst . toAscList
{-# INLINE keys #-}
-- | List of all values in ascending key order.
elems :: Map k v -> [v]
elems = map snd . toAscList
{-# INLINE elems #-}
-- | Left-biased union of two maps.
union :: Ord k => Map k v -> Map k v -> Map k v
union m1 m2 = foldl' (\acc (k, v) -> if member k acc then acc else insert k v acc) m1 (toAscList m2)
{-# INLINE union #-}
-- | Intersection of two maps, retaining values from the first map.
intersection :: Ord k => Map k a -> Map k b -> Map k a
intersection m1 m2 =
fromList [ (k, v) | (k, v) <- toAscList m1, member k m2 ]
{-# INLINE intersection #-}