Skip to content

Commit 102d35b

Browse files
oberblastmeistertreeowl
authored andcommitted
refactor to use snoc
1 parent e13717a commit 102d35b

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

Data/HashMap/Internal.hs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -819,17 +819,9 @@ insertNewKey !h0 !k0 x0 !m0 = go h0 k0 x0 0 m0
819819
in Full (update32 ary i st')
820820
where i = index h s
821821
go h k x s t@(Collision hy v)
822-
| h == hy = Collision h (snocNewLeaf (L k x) v)
822+
| h == hy = Collision h (A.snoc v (L k x))
823823
| otherwise =
824824
go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
825-
where
826-
snocNewLeaf :: Leaf k v -> A.Array (Leaf k v) -> A.Array (Leaf k v)
827-
snocNewLeaf leaf ary = A.run $ do
828-
let n = A.length ary
829-
mary <- A.new_ (n + 1)
830-
A.copy ary 0 mary 0 n
831-
A.write mary n leaf
832-
return mary
833825
{-# NOINLINE insertNewKey #-}
834826

835827

@@ -1008,12 +1000,8 @@ insertModifyingArr :: Eq k => v -> (v -> (# v #)) -> k -> A.Array (Leaf k v)
10081000
insertModifyingArr x f k0 ary0 = go k0 ary0 0 (A.length ary0)
10091001
where
10101002
go !k !ary !i !n
1011-
| i >= n = A.run $ do
1012-
-- Not found, append to the end.
1013-
mary <- A.new_ (n + 1)
1014-
A.copy ary 0 mary 0 n
1015-
A.write mary n (L k x)
1016-
return mary
1003+
-- Not found, append to the end.
1004+
| i >= n = A.snoc ary $ L k x
10171005
| otherwise = case A.index ary i of
10181006
(L kx y) | k == kx -> case f y of
10191007
(# y' #) -> if ptrEq y y'
@@ -2164,12 +2152,8 @@ updateOrSnocWithKey :: Eq k => (k -> v -> v -> (# v #)) -> k -> v -> A.Array (Le
21642152
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
21652153
where
21662154
go !k v !ary !i !n
2167-
| i >= n = A.run $ do
2168-
-- Not found, append to the end.
2169-
mary <- A.new_ (n + 1)
2170-
A.copy ary 0 mary 0 n
2171-
A.write mary n (L k v)
2172-
return mary
2155+
-- Not found, append to the end.
2156+
| i >= n = A.snoc ary $ L k v
21732157
| L kx y <- A.index ary i
21742158
, k == kx
21752159
, (# v2 #) <- f k v y

Data/HashMap/Internal/Array.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Data.HashMap.Internal.Array
3434
, new_
3535
, singleton
3636
, singletonM
37+
, snoc
3738
, pair
3839

3940
-- * Basic interface
@@ -212,6 +213,15 @@ singletonM :: a -> ST s (Array a)
212213
singletonM x = new 1 x >>= unsafeFreeze
213214
{-# INLINE singletonM #-}
214215

216+
snoc :: Array a -> a -> Array a
217+
snoc ary x = run $ do
218+
mary <- new (n + 1) x
219+
copy ary 0 mary 0 n
220+
pure mary
221+
where
222+
n = length ary
223+
{-# INLINE snoc #-}
224+
215225
pair :: a -> a -> Array a
216226
pair x y = run $ do
217227
ary <- new 2 x

0 commit comments

Comments
 (0)