Skip to content

Commit 506c84b

Browse files
committed
Refactor 'RunRes'
1 parent a815ed4 commit 506c84b

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

Data/HashMap/Array.hs

+7-11
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

@@ -325,22 +324,19 @@ unsafeThaw ary
325324
(# s', mary #) -> (# s', marray mary (length ary) #)
326325
{-# INLINE unsafeThaw #-}
327326

328-
-- | Helper datatype used in 'runInternal' and 'updateWithInternal'
329-
data RunRes f e = RunRes {-# UNPACK #-} !Size !(f e)
327+
data RunResA e = RunResA !Size !(Array e)
330328

331-
type RunResA e = RunRes Array e
332-
333-
type RunResM s e = RunRes (MArray s) e
329+
data RunResM s e = RunResM !Size !(MArray s e)
334330

335331
run :: (forall s . ST s (MArray s e)) -> Array e
336332
run act = runST $ act >>= unsafeFreeze
337333
{-# INLINE run #-}
338334

339335
runInternal :: (forall s . ST s (RunResM s e)) -> RunResA e
340336
runInternal act = runST $ do
341-
RunRes s mary <- act
337+
RunResM s mary <- act
342338
ary <- unsafeFreeze mary
343-
return (RunRes s ary)
339+
return (RunResA s ary)
344340
{-# INLINE runInternal #-}
345341

346342
run2 :: (forall s. ST s (MArray s e, a)) -> (Array e, a)
@@ -440,7 +436,7 @@ data Sized a = Sized {-# UNPACK #-} !Size !a
440436
updateWithInternal' :: Array e -> Int -> (e -> Sized e) -> RunResA e
441437
updateWithInternal' ary idx f =
442438
let Sized sz e = f (index ary idx)
443-
in RunRes sz (update ary idx e)
439+
in RunResA sz (update ary idx e)
444440
{-# INLINE updateWithInternal' #-}
445441

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

Data/HashMap/Base.hs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
15501550
-- branch vs. branch
15511551
go s !sz (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
15521552
let b' = b1 .|. b2
1553-
A.RunRes dsz ary' =
1553+
A.RunResA dsz ary' =
15541554
unionArrayByInternal sz
15551555
(go (s+bitsPerSubkey))
15561556
b1
@@ -1559,7 +1559,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
15591559
ary2
15601560
in A.Sized dsz (bitmapIndexedOrFull b' ary')
15611561
go s !sz (BitmapIndexed b1 ary1) (Full ary2) =
1562-
let A.RunRes dsz ary' =
1562+
let A.RunResA dsz ary' =
15631563
unionArrayByInternal sz
15641564
(go (s+bitsPerSubkey))
15651565
b1
@@ -1568,7 +1568,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
15681568
ary2
15691569
in A.Sized dsz (Full ary')
15701570
go s !sz (Full ary1) (BitmapIndexed b2 ary2) =
1571-
let A.RunRes dsz ary' =
1571+
let A.RunResA dsz ary' =
15721572
unionArrayByInternal sz
15731573
(go (s+bitsPerSubkey))
15741574
fullNodeMask
@@ -1577,7 +1577,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
15771577
ary2
15781578
in A.Sized dsz (Full ary')
15791579
go s !sz (Full ary1) (Full ary2) =
1580-
let A.RunRes dsz ary' =
1580+
let A.RunResA dsz ary' =
15811581
unionArrayByInternal sz
15821582
(go (s+bitsPerSubkey))
15831583
fullNodeMask
@@ -1590,7 +1590,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
15901590
| b1 .&. m2 == 0 = let ary' = A.insert ary1 i t2
15911591
b' = b1 .|. m2
15921592
in A.Sized sz (bitmapIndexedOrFull b' ary')
1593-
| otherwise = let A.RunRes dsz ary' =
1593+
| otherwise = let A.RunResA dsz ary' =
15941594
A.updateWithInternal' ary1 i $ \st1 ->
15951595
go (s+bitsPerSubkey) sz st1 t2
15961596
in A.Sized dsz (BitmapIndexed b1 ary')
@@ -1602,7 +1602,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
16021602
| b2 .&. m1 == 0 = let ary' = A.insert ary2 i $! t1
16031603
b' = b2 .|. m1
16041604
in A.Sized sz (bitmapIndexedOrFull b' ary')
1605-
| otherwise = let A.RunRes dsz ary'=
1605+
| otherwise = let A.RunResA dsz ary'=
16061606
A.updateWithInternal' ary2 i $ \st2 ->
16071607
go (s+bitsPerSubkey) sz t1 st2
16081608
in A.Sized dsz (BitmapIndexed b2 ary')
@@ -1613,14 +1613,14 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
16131613
go s !sz (Full ary1) t2 =
16141614
let h2 = leafHashCode t2
16151615
i = index h2 s
1616-
A.RunRes dsz ary' =
1616+
A.RunResA dsz ary' =
16171617
update16WithInternal' ary1 i $ \st1 ->
16181618
go (s+bitsPerSubkey) sz st1 t2
16191619
in A.Sized dsz (Full ary')
16201620
go s !sz t1 (Full ary2) =
16211621
let h1 = leafHashCode t1
16221622
i = index h1 s
1623-
A.RunRes dsz ary' =
1623+
A.RunResA dsz ary' =
16241624
update16WithInternal' ary2 i $ \st2 ->
16251625
go (s+bitsPerSubkey) sz t1 st2
16261626
in A.Sized dsz (Full ary')
@@ -1698,7 +1698,7 @@ unionArrayByInternal siz f b1 b2 ary1 ary2 = A.runInternal $ do
16981698
A.write mary i =<< A.indexM ary2 i2
16991699
go sz (i+1) (i1 ) (i2+1) (m `unsafeShiftL` 1)
17001700
d <- go siz 0 0 0 (b' .&. negate b') -- XXX: b' must be non-zero
1701-
return (A.RunRes d mary)
1701+
return (A.RunResM d mary)
17021702
-- TODO: For the case where b1 .&. b2 == b1, i.e. when one is a
17031703
-- subset of the other, we could use a slightly simpler algorithm,
17041704
-- where we copy one array, and then update.
@@ -2253,7 +2253,7 @@ update16With' ary idx f
22532253
update16WithInternal' :: A.Array e -> Int -> (e -> A.Sized e) -> A.RunResA e
22542254
update16WithInternal' ary idx f =
22552255
let A.Sized s x = f $! A.index ary idx
2256-
in A.RunRes s (update16 ary idx x)
2256+
in A.RunResA s (update16 ary idx x)
22572257
{-# INLINE update16WithInternal' #-}
22582258

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

Data/HashMap/Strict/Base.hs

+8-8
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
503503
-- branch vs. branch
504504
go s !sz (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
505505
let b' = b1 .|. b2
506-
A.RunRes dsz ary' =
506+
A.RunResA dsz ary' =
507507
unionArrayByInternal sz
508508
(go (s+bitsPerSubkey))
509509
b1
@@ -512,7 +512,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
512512
ary2
513513
in A.Sized dsz (bitmapIndexedOrFull b' ary')
514514
go s !sz (BitmapIndexed b1 ary1) (Full ary2) =
515-
let A.RunRes dsz ary' =
515+
let A.RunResA dsz ary' =
516516
unionArrayByInternal sz
517517
(go (s+bitsPerSubkey))
518518
b1
@@ -521,7 +521,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
521521
ary2
522522
in A.Sized dsz (Full ary')
523523
go s !sz (Full ary1) (BitmapIndexed b2 ary2) =
524-
let A.RunRes dsz ary' =
524+
let A.RunResA dsz ary' =
525525
unionArrayByInternal sz
526526
(go (s+bitsPerSubkey))
527527
fullNodeMask
@@ -530,7 +530,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
530530
ary2
531531
in A.Sized dsz (Full ary')
532532
go s !sz (Full ary1) (Full ary2) =
533-
let A.RunRes dsz ary' =
533+
let A.RunResA dsz ary' =
534534
unionArrayByInternal sz
535535
(go (s+bitsPerSubkey))
536536
fullNodeMask
@@ -543,7 +543,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
543543
| b1 .&. m2 == 0 = let ary' = A.insert ary1 i t2
544544
b' = b1 .|. m2
545545
in A.Sized sz (bitmapIndexedOrFull b' ary')
546-
| otherwise = let A.RunRes dsz ary' =
546+
| otherwise = let A.RunResA dsz ary' =
547547
A.updateWithInternal' ary1 i $ \st1 ->
548548
go (s+bitsPerSubkey) sz st1 t2
549549
in A.Sized dsz (BitmapIndexed b1 ary')
@@ -555,7 +555,7 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
555555
| b2 .&. m1 == 0 = let ary' = A.insert ary2 i $! t1
556556
b' = b2 .|. m1
557557
in A.Sized sz (bitmapIndexedOrFull b' ary')
558-
| otherwise = let A.RunRes dsz ary' =
558+
| otherwise = let A.RunResA dsz ary' =
559559
A.updateWithInternal' ary2 i $ \st2 ->
560560
go (s+bitsPerSubkey) sz t1 st2
561561
in A.Sized dsz (BitmapIndexed b2 ary')
@@ -566,14 +566,14 @@ unionWithKeyInternal f hm1 (HashMap siz hm2) = go 0 siz hm1 hm2
566566
go s !sz (Full ary1) t2 =
567567
let h2 = leafHashCode t2
568568
i = index h2 s
569-
A.RunRes dsz ary' =
569+
A.RunResA dsz ary' =
570570
update16WithInternal' ary1 i $ \st1 ->
571571
go (s+bitsPerSubkey) sz st1 t2
572572
in A.Sized dsz (Full ary')
573573
go s !sz t1 (Full ary2) =
574574
let h1 = leafHashCode t1
575575
i = index h1 s
576-
A.RunRes dsz ary' =
576+
A.RunResA dsz ary' =
577577
update16WithInternal' ary2 i $ \st2 ->
578578
go (s+bitsPerSubkey) sz t1 st2
579579
in A.Sized dsz (Full ary')

0 commit comments

Comments
 (0)