Skip to content

Commit 8201efe

Browse files
committed
Use clone to trim arrays
`cloneSmallMutableArray#` looks like a much better fit for implementing `trim` than does `copySmallMutableArray#`.
1 parent 3b46fcb commit 8201efe

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

Data/HashMap/Array.hs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Data.HashMap.Array
2929
, insertM
3030
, delete
3131
, sameArray1
32+
, trim
3233

3334
, unsafeFreeze
3435
, unsafeThaw
@@ -70,13 +71,13 @@ import Prelude hiding (filter, foldr, length, map, read)
7071
import GHC.Exts (SmallArray#, newSmallArray#, readSmallArray#, writeSmallArray#,
7172
indexSmallArray#, unsafeFreezeSmallArray#, unsafeThawSmallArray#,
7273
SmallMutableArray#, sizeofSmallArray#, copySmallArray#, thawSmallArray#,
73-
sizeofSmallMutableArray#, copySmallMutableArray#)
74+
sizeofSmallMutableArray#, copySmallMutableArray#, cloneSmallMutableArray#)
7475

7576
#else
7677
import GHC.Exts (Array#, newArray#, readArray#, writeArray#,
7778
indexArray#, unsafeFreezeArray#, unsafeThawArray#,
7879
MutableArray#, sizeofArray#, copyArray#, thawArray#,
79-
sizeofMutableArray#, copyMutableArray#)
80+
sizeofMutableArray#, copyMutableArray#, cloneMutableArray#)
8081
#endif
8182

8283
#if defined(ASSERTS)
@@ -125,6 +126,13 @@ copyArray# :: SmallArray# a
125126
-> State# d
126127
copyArray# = copySmallArray#
127128

129+
cloneMutableArray# :: SmallMutableArray# s a
130+
-> Int#
131+
-> Int#
132+
-> State# s
133+
-> (# State# s, SmallMutableArray# s a #)
134+
cloneMutableArray# = cloneSmallMutableArray#
135+
128136
thawArray# :: SmallArray# a
129137
-> Int#
130138
-> Int#
@@ -329,6 +337,19 @@ copyM !src !_sidx@(I# sidx#) !dst !_didx@(I# didx#) _n@(I# n#) =
329337
case copyMutableArray# (unMArray src) sidx# (unMArray dst) didx# n# s# of
330338
s2 -> (# s2, () #)
331339

340+
cloneM :: MArray s a -> Int -> Int -> ST s (MArray s a)
341+
cloneM _mary@(MArray mary#) _off@(I# off#) _len@(I# len#) =
342+
CHECK_BOUNDS("cloneM_off", lengthM _mary, _off - 1)
343+
CHECK_BOUNDS("cloneM_end", lengthM _mary, _off + _len - 1)
344+
ST $ \ s ->
345+
case cloneMutableArray# mary# off# len# s of
346+
(# s', mary'# #) -> (# s', MArray mary'# #)
347+
348+
-- | Create a new array of the @n@ first elements of @mary@.
349+
trim :: MArray s a -> Int -> ST s (Array a)
350+
trim mary n = cloneM mary 0 n >>= unsafeFreeze
351+
{-# INLINE trim #-}
352+
332353
-- | /O(n)/ Insert an element at the given position in this array,
333354
-- increasing its size by one.
334355
insert :: Array e -> Int -> e -> Array e

Data/HashMap/Base.hs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,14 +1564,6 @@ foldrWithKey f = go
15641564
------------------------------------------------------------------------
15651565
-- * Filter
15661566

1567-
-- | Create a new array of the @n@ first elements of @mary@.
1568-
trim :: A.MArray s a -> Int -> ST s (A.Array a)
1569-
trim mary n = do
1570-
mary2 <- A.new_ n
1571-
A.copyM mary 0 mary2 0 n
1572-
A.unsafeFreeze mary2
1573-
{-# INLINE trim #-}
1574-
15751567
-- | /O(n)/ Transform this map by applying a function to every value
15761568
-- and retaining only some of them.
15771569
mapMaybeWithKey :: (k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
@@ -1634,9 +1626,9 @@ filterMapAux onLeaf onColl = go
16341626
ch <- A.read mary 0
16351627
case ch of
16361628
t | isLeafOrCollision t -> return t
1637-
_ -> BitmapIndexed b <$> trim mary 1
1629+
_ -> BitmapIndexed b <$> A.trim mary 1
16381630
_ -> do
1639-
ary2 <- trim mary j
1631+
ary2 <- A.trim mary j
16401632
return $! if j == maxChildren
16411633
then Full ary2
16421634
else BitmapIndexed b ary2
@@ -1663,7 +1655,7 @@ filterMapAux onLeaf onColl = go
16631655
return $! Leaf h l
16641656
_ | i == j -> do ary2 <- A.unsafeFreeze mary
16651657
return $! Collision h ary2
1666-
| otherwise -> do ary2 <- trim mary j
1658+
| otherwise -> do ary2 <- A.trim mary j
16671659
return $! Collision h ary2
16681660
| Just el <- onColl (A.index ary i)
16691661
= A.write mary j el >> step ary mary (i+1) (j+1) n

0 commit comments

Comments
 (0)