packages feed

monoidmap-internal 0.5.1.0 → 0.5.2.0

raw patch · 5 files changed

+210/−5 lines, 5 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Data.MonoidMap.Internal.NonNullMap: adjust :: (Ord k, MonoidNull v) => (v -> v) -> k -> NonNullMap k v -> NonNullMap k v
+ Data.MonoidMap.Internal.NonNullMap.Adjust: adjust_alter :: (Ord k, MonoidNull v) => (v -> v) -> k -> NonNullMap k v -> NonNullMap k v
+ Data.MonoidMap.Internal.NonNullMap.Adjust: adjust_lookup :: (Ord k, MonoidNull v) => (v -> v) -> k -> NonNullMap k v -> NonNullMap k v

Files

CHANGELOG.md view
@@ -1,3 +1,12 @@+# 0.5.2.0++- Moved the implementation of `MonoidMap.adjust` from the `monoidmap` package+  into a new `adjust` function in `Data.MonoidMap.Internal.NonNullMap`. The+  public `MonoidMap.adjust` function now delegates to this internal function.++- Added the `Data.MonoidMap.Internal.NonNullMap.Adjust` module, which provides+  alternative implementations of `adjust` for benchmarking.+ # 0.5.1.0  - Added the `Data.MonoidMap.Internal.NonNullMap.Merge` module, which provides
components/monoidmap-internal-benchmark/Main.hs view
@@ -19,11 +19,11 @@ import Data.Eq     ( Eq ((==)) ) import Data.Function-    ( const, ($) )+    ( const, flip, ($) ) import Data.Int     ( Int ) import Data.List-    ( filter, foldl' )+    ( concat, filter, foldl', transpose ) import Data.Monoid     ( Monoid (mempty), Sum (Sum) ) import Data.Semigroup@@ -31,7 +31,7 @@ import Data.String     ( String ) import Prelude-    ( even, odd, (^) )+    ( Num ((+), (-)), even, odd, (^) ) import System.IO     ( IO ) import Test.Tasty.Bench@@ -39,6 +39,7 @@  import qualified Data.Map.Strict as OMap import qualified Data.MonoidMap.Internal.NonNullMap as MMap+import qualified Data.MonoidMap.Internal.NonNullMap.Adjust as MMap import qualified Data.MonoidMap.Internal.NonNullMap.FromMap as MMap import qualified Data.MonoidMap.Internal.NonNullMap.FromMapWith as MMap import qualified Data.MonoidMap.Internal.NonNullMap.Get as MMap@@ -80,6 +81,50 @@         , keys_odd         ] +    -- Cases for 'adjust', which together cover every kind of transition that+    -- a single call can make, from an old value v0 to a new value v1:+    --+    -- +----------+----------+-----------+-----------------------------++    -- | Old      | New      | Map       | Detectable?                 |+    -- | value    | value    | changed?  |                             |+    -- | v0       | v1       |           |                             |+    -- +----------+----------+-----------+-----------------------------++    -- |     null |     null | unchanged | yes (with 'null')           |+    -- |     null | non-null |   changed | yes (with 'null')           |+    -- | non-null |     null |   changed | yes (with 'null')           |+    -- | non-null | v1 is v0 | unchanged | yes (with pointer equality) |+    -- | non-null | v1 == v0 | unchanged | no  (no `Eq` context)       |+    -- | non-null | v1 /= v0 |   changed | no  (no `Eq` context)       |+    -- +----------+----------+-----------+-----------------------------++    --+    -- In each case, every key is paired with a 'Change', which determines the+    -- transition.+    --+    let -- The input that produces each transition in the table above, for the+        -- given keys, given:+        --+        -- m0: a map in which those keys are associated with 'Sum 0'; and+        -- m1: a map in which those keys are associated with 'Sum 1':+        --+        transitions ks m0 m1 =+            [ ("null to null",          (withChange ks Identity, m0))+            , ("null to non-null",      (withChange ks (Add 1) , m0))+            , ("non-null to null",      (withChange ks (Sub 1) , m1))+            , ("non-null to identical", (withChange ks Identity, m1))+            , ("non-null to equal",     (withChange ks (Add 0) , m1))+            , ("non-null to unequal",   (withChange ks (Add 1) , m1))+            ]++    let adjustCases = concat $ transpose+            [ withSuffix " (even)" adjustCasesEven+            , withSuffix " (odd)"  adjustCasesOdd+            ]+          where+            adjustCasesEven = transitions keys_even mm_keys_odd  mm_keys_even+            adjustCasesOdd  = transitions keys_odd  mm_keys_even mm_keys_odd++    evaluate $ rnf adjustCases+     let fromMapCases =             [ ("no nulls",           om_null_none)             , ("one null (min key)", om_null_min)@@ -98,6 +143,15 @@      defaultMain         [ compareImplementations+            ("adjust", adjustWith MMap.adjust) adjustCases+            [ ( "adjust_alter"+              , adjustWith MMap.adjust_alter+              )+            , ( "adjust_lookup"+              , adjustWith MMap.adjust_lookup+              )+            ]+        , compareImplementations             ("fromMap", MMap.fromMap) fromMapCases             [ ( "fromMap_filter"               , MMap.fromMap_filter@@ -135,8 +189,11 @@     minNatural :: Int     minNatural = 1 +    -- This value was chosen experimentally. It's high enough to generate maps+    -- of a reasonable depth, but low enough for timings to be stable from one+    -- run to the next.     maxNatural :: Int-    maxNatural = 2 ^ (16 :: Int)+    maxNatural = 2 ^ (10 :: Int)      -- A map from keys [minNatural .. maxNatural], with a null value for each     -- key that satisfies the given predicate, and a non-null value otherwise.@@ -168,6 +225,67 @@         -> Sum Int     getAll get (ks, m) = foldl' (\v k -> v <> get k m) mempty ks     {-# NOINLINE getAll #-}++    -- Pairs every one of the given keys with the given change.+    withChange :: [Int] -> Change -> [(Int, Change)]+    withChange ks c = [(k, c) | k <- ks]++    -- Appends the given suffix to the label of every case.+    withSuffix :: String -> [(String, i)] -> [(String, i)]+    withSuffix s cases = [(label <> s, input) | (label, input) <- cases]++    -- Makes the given change to a value.+    change :: Change -> Sum Int -> Sum Int+    change Identity v = v+    change (Add n)  v = v + Sum n+    change (Sub n)  v = v - Sum n++    -- Adjusts the values associated with all of the given keys in turn, with+    -- the given implementation of 'adjust', by making the change that+    -- accompanies each key.+    --+    adjustWith+        :: ((Sum Int -> Sum Int)+            -> Int+            -> MMap.NonNullMap Int (Sum Int)+            -> MMap.NonNullMap Int (Sum Int))+        -> ([(Int, Change)], MMap.NonNullMap Int (Sum Int))+        -> MMap.NonNullMap Int (Sum Int)+    adjustWith adjust = adjustAll (\(k, c) m -> adjust (change c) k m)+    {-# INLINE adjustWith #-}++    -- Applies the given function to each of the given keys in turn, together+    -- with its change, threading the map through each call.+    --+    adjustAll+        :: ((Int, Change)+            -> MMap.NonNullMap Int (Sum Int)+            -> MMap.NonNullMap Int (Sum Int))+        -> ([(Int, Change)], MMap.NonNullMap Int (Sum Int))+        -> MMap.NonNullMap Int (Sum Int)+    adjustAll step (kcs, m) = foldl' (flip step) m kcs+    {-# NOINLINE adjustAll #-}++-- | A change to make to a value, with the function passed to 'adjust'.+--+-- The change is chosen for each key separately, and is only known at run time.+-- So the function passed to 'adjust' captures a value that can differ from one+-- call to the next, as in typical use (for example, with `(x :)`). And the+-- compiler can't simplify 'Add 0' to 'Identity': 'Add' always builds a new+-- value.+--+data Change+    = Identity+      -- ^ Returns the value itself.+    | Add Int+      -- ^ Adds to the value.+    | Sub Int+      -- ^ Subtracts from the value.++instance NFData Change where+    rnf Identity = ()+    rnf (Add n)  = rnf n+    rnf (Sub n)  = rnf n  -- | Compares implementations of a public function with the public function --   itself, for each of the given cases.
components/monoidmap-internal/Data/MonoidMap/Internal/NonNullMap.hs view
@@ -25,6 +25,9 @@      -- * Lookup     , get++    -- * Modification+    , adjust     )     where @@ -37,6 +40,8 @@     ( MonoidNull (null) ) import Data.MonoidMap.Internal.NonNull     ( NonNull )+import Data.MonoidMap.Internal.NonNullMap.Adjust+    ( adjust_alter ) import Data.MonoidMap.Internal.NonNullMap.FromMap     ( fromMap_filter_if_any_null ) import Data.MonoidMap.Internal.NonNullMap.FromMapWith@@ -86,3 +91,24 @@ get :: (Ord k, Monoid v) => k -> NonNullMap k v -> v get = get_lookup {-# INLINE get #-}++--------------------------------------------------------------------------------+-- Modification+--------------------------------------------------------------------------------++-- | Applies the given function to the value associated with the given key (or+--   to 'mempty' if the key is not present), removing the key if the result is+--   'null'.+--+-- This function selects which of the implementations in+-- "Data.MonoidMap.Internal.NonNullMap.Adjust" is used by the public @adjust@+-- function of the @monoidmap@ package.+--+adjust+    :: (Ord k, MonoidNull v)+    => (v -> v)+    -> k+    -> NonNullMap k v+    -> NonNullMap k v+adjust = adjust_alter+{-# INLINE adjust #-}
+ components/monoidmap-internal/Data/MonoidMap/Internal/NonNullMap/Adjust.hs view
@@ -0,0 +1,51 @@+{- HLINT ignore "Use camelCase" -}++-- |+-- Copyright: © 2022–2026 Jonathan Knowles+-- License: Apache-2.0+--+-- Provides alternative implementations of @adjust@.+--+module Data.MonoidMap.Internal.NonNullMap.Adjust+    ( adjust_alter+    , adjust_lookup+    )+    where++import Prelude++import Data.Monoid.Null+    ( MonoidNull )+import Data.MonoidMap.Internal.NonNullMap.Unsafe+    ( NonNullMap (..) )++import qualified Data.Map.Strict as Map+import qualified Data.MonoidMap.Internal.NonNull as NonNull++adjust_alter+    :: (Ord k, MonoidNull v)+    => (v -> v)+    -> k+    -> NonNullMap k v+    -> NonNullMap k v+adjust_alter f k (NonNullMap m) = NonNullMap $+    Map.alter (NonNull.nonNull . maybe (f mempty) (NonNull.apply f)) k m+{-# INLINE adjust_alter #-}++adjust_lookup+    :: (Ord k, MonoidNull v)+    => (v -> v)+    -> k+    -> NonNullMap k v+    -> NonNullMap k v+adjust_lookup f k (NonNullMap m) = NonNullMap $+    case Map.lookup k m of+        Nothing ->+            case NonNull.nonNull (f mempty) of+                Just v1 -> Map.insert k v1 m+                Nothing -> m+        Just v0 ->+            case NonNull.nonNull (NonNull.apply f v0) of+                Just v1 -> Map.insert k v1 m+                Nothing -> Map.delete k    m+{-# INLINE adjust_lookup #-}
monoidmap-internal.cabal view
@@ -1,6 +1,6 @@ cabal-version:  3.0 name:           monoidmap-internal-version:        0.5.1.0+version:        0.5.2.0 homepage:       https://github.com/jonathanknowles/monoidmap/tree/HEAD/packages/monoidmap-internal bug-reports:    https://github.com/jonathanknowles/monoidmap/issues license:        Apache-2.0@@ -64,6 +64,7 @@     exposed-modules:         Data.MonoidMap.Internal.NonNull         Data.MonoidMap.Internal.NonNullMap+        Data.MonoidMap.Internal.NonNullMap.Adjust         Data.MonoidMap.Internal.NonNullMap.FromMap         Data.MonoidMap.Internal.NonNullMap.FromMapWith         Data.MonoidMap.Internal.NonNullMap.Get