packages feed

primitive-extras 0.9 → 0.10

raw patch · 4 files changed

+47/−33 lines, 4 files

Files

library/PrimitiveExtras/Bitmap.hs view
@@ -1,6 +1,6 @@ module PrimitiveExtras.Bitmap (-  Bitmap,+  Bitmap(..),   empty,   singleton,   insert,
library/PrimitiveExtras/By6Bits.hs view
@@ -7,7 +7,7 @@   pair,   insert,   replace,-  update,+  adjust,   unset,   lookup,   focusAt,@@ -90,29 +90,31 @@     sparseIndex = Bitmap.populatedIndex i b     in By6Bits b (SmallArray.set sparseIndex e a) -{-# INLINE update #-}-update :: (e -> e) -> Int -> By6Bits e -> By6Bits e-update fn i (By6Bits b a) =+{-# INLINE adjust #-}+adjust :: (e -> e) -> Int -> By6Bits e -> By6Bits e+adjust fn i (By6Bits b a) =   let     sparseIndex = Bitmap.populatedIndex i b     in       By6Bits b-        (SmallArray.unsafeUpdate fn sparseIndex a)+        (SmallArray.unsafeAdjust fn sparseIndex a)  -- | -- Remove an element. {-# INLINE unset #-} unset :: Int -> By6Bits e -> By6Bits e-unset i (By6Bits b a) =+unset i (By6Bits (Bitmap b) a) =   {-# SCC "unset" #-}-  if Bitmap.isPopulated i b-    then-      let-        sparseIndex = Bitmap.populatedIndex i b-        b' = Bitmap.invert i b-        a' = SmallArray.unset sparseIndex a-        in By6Bits b' a'-    else By6Bits b a+  let+    bitAtIndex = bit i+    isPopulated = b .&. bitAtIndex /= 0+    in if isPopulated+      then let+        populatedIndex = popCount (b .&. pred bitAtIndex)+        updatedBitmap = xor b bitAtIndex+        updatedArray = SmallArray.unset populatedIndex a+        in By6Bits (Bitmap updatedBitmap) updatedArray+      else By6Bits (Bitmap b) a  -- | -- Lookup an item at the index.
library/PrimitiveExtras/SmallArray.hs view
@@ -48,12 +48,11 @@ set index a array =   {-# SCC "set" #-}    let-    !size = sizeofSmallArray array-    in runSmallArray $ do-      newMa <- newSmallArray size undefined-      copySmallArray newMa 0 array 0 size-      writeSmallArray newMa index a-      return newMa+    size = sizeofSmallArray array+    in runST $ do+      m <- thawSmallArray array 0 size+      writeSmallArray m index a+      unsafeFreezeSmallArray m  {-# INLINE insert #-} insert :: Int -> a -> SmallArray a -> SmallArray a@@ -70,23 +69,23 @@       copySmallArray newMa nextIndex array index amountOfFollowingElements       return newMa -{-# INLINE update #-}-update :: (a -> a) -> Int -> SmallArray a -> SmallArray a-update fn index array =+{-# INLINE adjust #-}+adjust :: (a -> a) -> Int -> SmallArray a -> SmallArray a+adjust fn index array =   let     size = sizeofSmallArray array     in if size > index && index >= 0-      then unsafeUpdateWithSize fn index size array+      then unsafeAdjustWithSize fn index size array       else array -{-# INLINE unsafeUpdate #-}-unsafeUpdate :: (a -> a) -> Int -> SmallArray a -> SmallArray a-unsafeUpdate fn index array =-  unsafeUpdateWithSize fn index (sizeofSmallArray array) array+{-# INLINE unsafeAdjust #-}+unsafeAdjust :: (a -> a) -> Int -> SmallArray a -> SmallArray a+unsafeAdjust fn index array =+  unsafeAdjustWithSize fn index (sizeofSmallArray array) array -{-# INLINE unsafeUpdateWithSize #-}-unsafeUpdateWithSize :: (a -> a) -> Int -> Int -> SmallArray a -> SmallArray a-unsafeUpdateWithSize fn index size array =+{-# INLINE unsafeAdjustWithSize #-}+unsafeAdjustWithSize :: (a -> a) -> Int -> Int -> SmallArray a -> SmallArray a+unsafeAdjustWithSize fn index size array =   runST $ do     m <- thawSmallArray array 0 size     element <- readSmallArray m index@@ -121,6 +120,19 @@     | otherwise -> do       a <- newSmallArray 1 e2       return a++{-# INLINE findAndReplace #-}+findAndReplace :: (a -> Maybe a) -> SmallArray a -> SmallArray a+findAndReplace f array =+  let+    size = sizeofSmallArray array+    iterate index =+      if index < size+        then case f (indexSmallArray array index) of+          Just newElement -> set index newElement array+          Nothing -> iterate (succ index)+        else array+    in iterate 0  {-# INLINE find #-} find :: (a -> Bool) -> SmallArray a -> Maybe a
primitive-extras.cabal view
@@ -1,5 +1,5 @@ name: primitive-extras-version: 0.9+version: 0.10 category: Primitive synopsis: Extras for the "primitive" library homepage: https://github.com/metrix-ai/primitive-extras