diff --git a/Data/IntTrie.hs b/Data/IntTrie.hs index 865d717..50c8e17 100644 --- a/Data/IntTrie.hs +++ b/Data/IntTrie.hs @@ -17,7 +17,7 @@ ------------------------------------------------------------- module Data.IntTrie - ( IntTrie, identity, apply, modify, overwrite ) + ( IntTrie, identity, apply, modify, modify', overwrite ) where import Control.Applicative @@ -88,6 +88,22 @@ modifyPositive x f ~(BitTrie one even odd) | testBit x 0 = BitTrie one even (modifyPositive (x `shiftR` 1) f odd) | otherwise = BitTrie one (modifyPositive (x `shiftR` 1) f even) odd + +-- | Modify the function at one point (strict version) +modify' :: (Ord b, Num b, Bits b) => b -> (a -> a) -> IntTrie a -> IntTrie a +modify' x f (IntTrie neg z pos) = + case compare x 0 of + LT -> (IntTrie $! modifyPositive' (-x) f neg) z pos + EQ -> (IntTrie neg $! f z) pos + GT -> IntTrie neg z $! modifyPositive' x f pos + +modifyPositive' :: (Num b, Bits b) => b -> (a -> a) -> BitTrie a -> BitTrie a +modifyPositive' x f (BitTrie one even odd) + | x == 1 = (BitTrie $! f one) even odd + | testBit x 0 = BitTrie one even $! modifyPositive' (x `shiftR` 1) f odd + | otherwise = (BitTrie one $! modifyPositive' (x `shiftR` 1) f even) odd + + -- | Overwrite the function at one point -- -- > overwrite i x = modify i (const x) diff --git a/data-inttrie.cabal b/data-inttrie.cabal index 7ea1e06..4aa2adf 100644 --- a/data-inttrie.cabal +++ b/data-inttrie.cabal @@ -1,7 +1,7 @@ Name: data-inttrie Description: A simple lazy, infinite trie from integers. -Version: 0.0.8 +Version: 0.0.9 Stability: experimental Synopsis: A lazy, infinite trie of integers. License: BSD3