| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Data.Trie.Binary.Magma
Contents
Synopsis
- data Trie (h :: Nat) a b
- magmaAllWith :: forall b a (h :: Nat). (Word -> b -> a) -> Trie h a b -> Maybe a
- binWith :: forall a b (h :: Nat). Magma a => (Word -> b -> a) -> Trie h a b -> Trie h a b -> Trie h a b
- alterWith :: forall (h :: Nat) a b. (Magma a, KnownNat h) => (Word -> b -> a) -> (Maybe b -> Maybe b) -> Word -> Trie h a b -> Trie h a b
- class Magma a where
- magma :: a -> a -> a
- newtype WrappedSemigroup m = WrapSemigroup m
Examples
ABC308G Minimum Xor Pair Query
data A = Done !Word | Partial !Word
-- non-associative
instance Magma A where
magma = m
where
m (Done x) (Done y) = Done (min x y)
m (Done x) Partial{} = Done x
m Partial{} (Done y) = Done y
m (Partial x) (Partial y) = Done (xor x y)
{-# INLINE magma #-}
type B = Int
tip :: Word -> B -> A
tip w b
| b >= 2 = Done 0
| otherwise = Partial w
{-# INLINE tip #-}
ins :: Maybe B -> Maybe B
ins = Just . maybe 1 (+ 1)
{-# INLINE ins #-}
del :: Maybe B -> Maybe B
del ma = case ma of
Just a | a > 1 -> Just (a - 1)
_ -> Nothing
{-# INLINE del #-}
insert :: (KnownNat h) => Word -> Trie h A B -> Trie h A B
insert = alterWith tip ins
{-# INLINE insert #-}
delete :: (KnownNat h) => Word -> Trie h A B -> Trie h A B
delete = alterWith tip del
{-# INLINE delete #-}
magmaAll :: Trie h A B -> Maybe A
magmaAll = magmaAllWith tip
{-# INLINE magmaAll #-}Binary Trie
Magma
Instances
| Semigroup m => Magma (WrappedSemigroup m) Source # | |
Defined in Data.Trie.Binary.Magma Methods magma :: WrappedSemigroup m -> WrappedSemigroup m -> WrappedSemigroup m Source # | |
newtype WrappedSemigroup m Source #
Constructors
| WrapSemigroup m |
Instances
| Semigroup m => Magma (WrappedSemigroup m) Source # | |
Defined in Data.Trie.Binary.Magma Methods magma :: WrappedSemigroup m -> WrappedSemigroup m -> WrappedSemigroup m Source # | |