Skip to content

Commit 4550cd0

Browse files
committed
Refactor 'RunRes'
1 parent 2b07cc7 commit 4550cd0

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

Data/HashMap/Array.hs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
module Data.HashMap.Array
99
( Array
1010
, MArray
11-
, RunRes (..)
12-
, RunResA
13-
, RunResM
11+
, RunResA (..)
12+
, RunResM (..)
1413
, Size (..)
1514
, Sized (..)
1615

@@ -321,22 +320,19 @@ unsafeThaw ary
321320
(# s', mary #) -> (# s', marray mary (length ary) #)
322321
{-# INLINE unsafeThaw #-}
323322

324-
-- | Helper datatype used in 'runInternal' and 'updateWithInternal'
325-
data RunRes f e = RunRes {-# UNPACK #-} !Size !(f e)
323+
data RunResA e = RunResA !Size !(Array e)
326324

327-
type RunResA e = RunRes Array e
328-
329-
type RunResM s e = RunRes (MArray s) e
325+
data RunResM s e = RunResM !Size !(MArray s e)
330326

331327
run :: (forall s . ST s (MArray s e)) -> Array e
332328
run act = runST $ act >>= unsafeFreeze
333329
{-# INLINE run #-}
334330

335331
runInternal :: (forall s . ST s (RunResM s e)) -> RunResA e
336332
runInternal act = runST $ do
337-
RunRes s mary <- act
333+
RunResM s mary <- act
338334
ary <- unsafeFreeze mary
339-
return (RunRes s ary)
335+
return (RunResA s ary)
340336
{-# INLINE runInternal #-}
341337

342338
run2 :: (forall s. ST s (MArray s e, a)) -> (Array e, a)
@@ -436,7 +432,7 @@ data Sized a = Sized {-# UNPACK #-} !Size !a
436432
updateWithInternal' :: Array e -> Int -> (e -> Sized e) -> RunResA e
437433
updateWithInternal' ary idx f =
438434
let Sized sz e = f (index ary idx)
439-
in RunRes sz (update ary idx e)
435+
in RunResA sz (update ary idx e)
440436
{-# INLINE updateWithInternal' #-}
441437

442438
-- | /O(1)/ Update the element at the given position in this array,

Data/HashMap/Base.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14351435
-- branch vs. branch
14361436
go s !sz (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
14371437
let b' = b1 .|. b2
1438-
A.RunRes dsz ary' =
1438+
A.RunResA dsz ary' =
14391439
unionArrayByInternal sz
14401440
(go (s+bitsPerSubkey))
14411441
b1
@@ -1444,7 +1444,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14441444
ary2
14451445
in A.Sized dsz (bitmapIndexedOrFull b' ary')
14461446
go s !sz (BitmapIndexed b1 ary1) (Full ary2) =
1447-
let A.RunRes dsz ary' =
1447+
let A.RunResA dsz ary' =
14481448
unionArrayByInternal sz
14491449
(go (s+bitsPerSubkey))
14501450
b1
@@ -1453,7 +1453,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14531453
ary2
14541454
in A.Sized dsz (Full ary')
14551455
go s !sz (Full ary1) (BitmapIndexed b2 ary2) =
1456-
let A.RunRes dsz ary' =
1456+
let A.RunResA dsz ary' =
14571457
unionArrayByInternal sz
14581458
(go (s+bitsPerSubkey))
14591459
fullNodeMask
@@ -1462,7 +1462,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14621462
ary2
14631463
in A.Sized dsz (Full ary')
14641464
go s !sz (Full ary1) (Full ary2) =
1465-
let A.RunRes dsz ary' =
1465+
let A.RunResA dsz ary' =
14661466
unionArrayByInternal sz
14671467
(go (s+bitsPerSubkey))
14681468
fullNodeMask
@@ -1475,7 +1475,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14751475
| b1 .&. m2 == 0 = let ary' = A.insert ary1 i t2
14761476
b' = b1 .|. m2
14771477
in A.Sized sz (bitmapIndexedOrFull b' ary')
1478-
| otherwise = let A.RunRes dsz ary' =
1478+
| otherwise = let A.RunResA dsz ary' =
14791479
A.updateWithInternal' ary1 i $ \st1 ->
14801480
go (s+bitsPerSubkey) sz st1 t2
14811481
in A.Sized dsz (BitmapIndexed b1 ary')
@@ -1487,7 +1487,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14871487
| b2 .&. m1 == 0 = let ary' = A.insert ary2 i $! t1
14881488
b' = b2 .|. m1
14891489
in A.Sized sz (bitmapIndexedOrFull b' ary')
1490-
| otherwise = let A.RunRes dsz ary'=
1490+
| otherwise = let A.RunResA dsz ary'=
14911491
A.updateWithInternal' ary2 i $ \st2 ->
14921492
go (s+bitsPerSubkey) sz t1 st2
14931493
in A.Sized dsz (BitmapIndexed b2 ary')
@@ -1498,14 +1498,14 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
14981498
go s !sz (Full ary1) t2 =
14991499
let h2 = leafHashCode t2
15001500
i = index h2 s
1501-
A.RunRes dsz ary' =
1501+
A.RunResA dsz ary' =
15021502
update16WithInternal' ary1 i $ \st1 ->
15031503
go (s+bitsPerSubkey) sz st1 t2
15041504
in A.Sized dsz (Full ary')
15051505
go s !sz t1 (Full ary2) =
15061506
let h1 = leafHashCode t1
15071507
i = index h1 s
1508-
A.RunRes dsz ary' =
1508+
A.RunResA dsz ary' =
15091509
update16WithInternal' ary2 i $ \st2 ->
15101510
go (s+bitsPerSubkey) sz t1 st2
15111511
in A.Sized dsz (Full ary')
@@ -1583,7 +1583,7 @@ unionArrayByInternal siz f b1 b2 ary1 ary2 = A.runInternal $ do
15831583
A.write mary i =<< A.indexM ary2 i2
15841584
go sz (i+1) (i1 ) (i2+1) (m `unsafeShiftL` 1)
15851585
d <- go siz 0 0 0 (b' .&. negate b') -- XXX: b' must be non-zero
1586-
return (A.RunRes d mary)
1586+
return (A.RunResM d mary)
15871587
-- TODO: For the case where b1 .&. b2 == b1, i.e. when one is a
15881588
-- subset of the other, we could use a slightly simpler algorithm,
15891589
-- where we copy one array, and then update.
@@ -2051,7 +2051,7 @@ update16With' ary idx f
20512051
update16WithInternal' :: A.Array e -> Int -> (e -> A.Sized e) -> A.RunResA e
20522052
update16WithInternal' ary idx f =
20532053
let A.Sized s x = f $! A.index ary idx
2054-
in A.RunRes s (update16 ary idx x)
2054+
in A.RunResA s (update16 ary idx x)
20552055
{-# INLINE update16WithInternal' #-}
20562056

20572057
-- | Unsafely clone an array of 16 elements. The length of the input

Data/HashMap/Strict/Base.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
496496
-- branch vs. branch
497497
go s !sz (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
498498
let b' = b1 .|. b2
499-
A.RunRes dsz ary' =
499+
A.RunResA dsz ary' =
500500
unionArrayByInternal sz
501501
(go (s+bitsPerSubkey))
502502
b1
@@ -505,7 +505,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
505505
ary2
506506
in A.Sized dsz (bitmapIndexedOrFull b' ary')
507507
go s !sz (BitmapIndexed b1 ary1) (Full ary2) =
508-
let A.RunRes dsz ary' =
508+
let A.RunResA dsz ary' =
509509
unionArrayByInternal sz
510510
(go (s+bitsPerSubkey))
511511
b1
@@ -514,7 +514,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
514514
ary2
515515
in A.Sized dsz (Full ary')
516516
go s !sz (Full ary1) (BitmapIndexed b2 ary2) =
517-
let A.RunRes dsz ary' =
517+
let A.RunResA dsz ary' =
518518
unionArrayByInternal sz
519519
(go (s+bitsPerSubkey))
520520
fullNodeMask
@@ -523,7 +523,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
523523
ary2
524524
in A.Sized dsz (Full ary')
525525
go s !sz (Full ary1) (Full ary2) =
526-
let A.RunRes dsz ary' =
526+
let A.RunResA dsz ary' =
527527
unionArrayByInternal sz
528528
(go (s+bitsPerSubkey))
529529
fullNodeMask
@@ -536,7 +536,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
536536
| b1 .&. m2 == 0 = let ary' = A.insert ary1 i t2
537537
b' = b1 .|. m2
538538
in A.Sized sz (bitmapIndexedOrFull b' ary')
539-
| otherwise = let A.RunRes dsz ary' =
539+
| otherwise = let A.RunResA dsz ary' =
540540
A.updateWithInternal' ary1 i $ \st1 ->
541541
go (s+bitsPerSubkey) sz st1 t2
542542
in A.Sized dsz (BitmapIndexed b1 ary')
@@ -548,7 +548,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
548548
| b2 .&. m1 == 0 = let ary' = A.insert ary2 i $! t1
549549
b' = b2 .|. m1
550550
in A.Sized sz (bitmapIndexedOrFull b' ary')
551-
| otherwise = let A.RunRes dsz ary' =
551+
| otherwise = let A.RunResA dsz ary' =
552552
A.updateWithInternal' ary2 i $ \st2 ->
553553
go (s+bitsPerSubkey) sz t1 st2
554554
in A.Sized dsz (BitmapIndexed b2 ary')
@@ -559,14 +559,14 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
559559
go s !sz (Full ary1) t2 =
560560
let h2 = leafHashCode t2
561561
i = index h2 s
562-
A.RunRes dsz ary' =
562+
A.RunResA dsz ary' =
563563
update16WithInternal' ary1 i $ \st1 ->
564564
go (s+bitsPerSubkey) sz st1 t2
565565
in A.Sized dsz (Full ary')
566566
go s !sz t1 (Full ary2) =
567567
let h1 = leafHashCode t1
568568
i = index h1 s
569-
A.RunRes dsz ary' =
569+
A.RunResA dsz ary' =
570570
update16WithInternal' ary2 i $ \st2 ->
571571
go (s+bitsPerSubkey) sz t1 st2
572572
in A.Sized dsz (Full ary')

0 commit comments

Comments
 (0)