@@ -118,6 +118,7 @@ module Data.HashMap.Internal
118
118
, index
119
119
, bitsPerSubkey
120
120
, fullBitmap
121
+ , nextShift
121
122
, sparseIndex
122
123
, two
123
124
, unionArrayBy
@@ -689,10 +690,10 @@ lookupCont absent present !h0 !k0 !s0 !m0 = go h0 k0 s0 m0
689
690
go h k s (BitmapIndexed b v)
690
691
| b .&. m == 0 = absent (# # )
691
692
| otherwise =
692
- go h k (s + bitsPerSubkey ) (A. index v (sparseIndex b m))
693
+ go h k (nextShift s ) (A. index v (sparseIndex b m))
693
694
where m = mask h s
694
695
go h k s (Full v) =
695
- go h k (s + bitsPerSubkey ) (A. index v (index h s))
696
+ go h k (nextShift s ) (A. index v (index h s))
696
697
go h k _ (Collision hx v)
697
698
| h == hx = lookupInArrayCont absent present k v
698
699
| otherwise = absent (# # )
@@ -786,15 +787,15 @@ insert' h0 k0 v0 m0 = go h0 k0 v0 0 m0
786
787
in bitmapIndexedOrFull (b .|. m) ary'
787
788
| otherwise =
788
789
let ! st = A. index ary i
789
- ! st' = go h k x (s + bitsPerSubkey ) st
790
+ ! st' = go h k x (nextShift s ) st
790
791
in if st' `ptrEq` st
791
792
then t
792
793
else BitmapIndexed b (A. update ary i st')
793
794
where m = mask h s
794
795
i = sparseIndex b m
795
796
go h k x s t@ (Full ary) =
796
797
let ! st = A. index ary i
797
- ! st' = go h k x (s + bitsPerSubkey ) st
798
+ ! st' = go h k x (nextShift s ) st
798
799
in if st' `ptrEq` st
799
800
then t
800
801
else Full (update32 ary i st')
@@ -824,13 +825,13 @@ insertNewKey !h0 !k0 x0 !m0 = go h0 k0 x0 0 m0
824
825
in bitmapIndexedOrFull (b .|. m) ary'
825
826
| otherwise =
826
827
let ! st = A. index ary i
827
- ! st' = go h k x (s + bitsPerSubkey ) st
828
+ ! st' = go h k x (nextShift s ) st
828
829
in BitmapIndexed b (A. update ary i st')
829
830
where m = mask h s
830
831
i = sparseIndex b m
831
832
go h k x s (Full ary) =
832
833
let ! st = A. index ary i
833
- ! st' = go h k x (s + bitsPerSubkey ) st
834
+ ! st' = go h k x (nextShift s ) st
834
835
in Full (update32 ary i st')
835
836
where i = index h s
836
837
go h k x s t@ (Collision hy v)
@@ -860,13 +861,13 @@ insertKeyExists !collPos0 !h0 !k0 x0 !m0 = go collPos0 h0 k0 x0 0 m0
860
861
in bitmapIndexedOrFull (b .|. m) ary'
861
862
| otherwise =
862
863
let ! st = A. index ary i
863
- ! st' = go collPos h k x (s + bitsPerSubkey ) st
864
+ ! st' = go collPos h k x (nextShift s ) st
864
865
in BitmapIndexed b (A. update ary i st')
865
866
where m = mask h s
866
867
i = sparseIndex b m
867
868
go collPos h k x s (Full ary) =
868
869
let ! st = A. index ary i
869
- ! st' = go collPos h k x (s + bitsPerSubkey ) st
870
+ ! st' = go collPos h k x (nextShift s ) st
870
871
in Full (update32 ary i st')
871
872
where i = index h s
872
873
go collPos h k x _s (Collision _hy v)
@@ -903,14 +904,14 @@ unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
903
904
return $! bitmapIndexedOrFull (b .|. m) ary'
904
905
| otherwise = do
905
906
st <- A. indexM ary i
906
- st' <- go h k x (s + bitsPerSubkey ) st
907
+ st' <- go h k x (nextShift s ) st
907
908
A. unsafeUpdateM ary i st'
908
909
return t
909
910
where m = mask h s
910
911
i = sparseIndex b m
911
912
go h k x s t@ (Full ary) = do
912
913
st <- A. indexM ary i
913
- st' <- go h k x (s + bitsPerSubkey ) st
914
+ st' <- go h k x (nextShift s ) st
914
915
A. unsafeUpdateM ary i st'
915
916
return t
916
917
where i = index h s
@@ -932,7 +933,7 @@ two = go
932
933
where
933
934
go s h1 k1 v1 h2 t2
934
935
| bp1 == bp2 = do
935
- st <- go (s + bitsPerSubkey ) h1 k1 v1 h2 t2
936
+ st <- go (nextShift s ) h1 k1 v1 h2 t2
936
937
ary <- A. singletonM st
937
938
return $ BitmapIndexed bp1 ary
938
939
| otherwise = do
@@ -985,7 +986,7 @@ insertModifying x f k0 m0 = go h0 k0 0 m0
985
986
in bitmapIndexedOrFull (b .|. m) ary'
986
987
| otherwise =
987
988
let ! st = A. index ary i
988
- ! st' = go h k (s + bitsPerSubkey ) st
989
+ ! st' = go h k (nextShift s ) st
989
990
ary' = A. update ary i $! st'
990
991
in if ptrEq st st'
991
992
then t
@@ -994,7 +995,7 @@ insertModifying x f k0 m0 = go h0 k0 0 m0
994
995
i = sparseIndex b m
995
996
go h k s t@ (Full ary) =
996
997
let ! st = A. index ary i
997
- ! st' = go h k (s + bitsPerSubkey ) st
998
+ ! st' = go h k (nextShift s ) st
998
999
ary' = update32 ary i $! st'
999
1000
in if ptrEq st st'
1000
1001
then t
@@ -1052,14 +1053,14 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
1052
1053
return $! bitmapIndexedOrFull (b .|. m) ary'
1053
1054
| otherwise = do
1054
1055
st <- A. indexM ary i
1055
- st' <- go h k x (s + bitsPerSubkey ) st
1056
+ st' <- go h k x (nextShift s ) st
1056
1057
A. unsafeUpdateM ary i st'
1057
1058
return t
1058
1059
where m = mask h s
1059
1060
i = sparseIndex b m
1060
1061
go h k x s t@ (Full ary) = do
1061
1062
st <- A. indexM ary i
1062
- st' <- go h k x (s + bitsPerSubkey ) st
1063
+ st' <- go h k x (nextShift s ) st
1063
1064
A. unsafeUpdateM ary i st'
1064
1065
return t
1065
1066
where i = index h s
@@ -1085,7 +1086,7 @@ delete' h0 k0 m0 = go h0 k0 0 m0
1085
1086
| b .&. m == 0 = t
1086
1087
| otherwise =
1087
1088
let ! st = A. index ary i
1088
- ! st' = go h k (s + bitsPerSubkey ) st
1089
+ ! st' = go h k (nextShift s ) st
1089
1090
in if st' `ptrEq` st
1090
1091
then t
1091
1092
else case st' of
@@ -1104,7 +1105,7 @@ delete' h0 k0 m0 = go h0 k0 0 m0
1104
1105
i = sparseIndex b m
1105
1106
go h k s t@ (Full ary) =
1106
1107
let ! st = A. index ary i
1107
- ! st' = go h k (s + bitsPerSubkey ) st
1108
+ ! st' = go h k (nextShift s ) st
1108
1109
in if st' `ptrEq` st
1109
1110
then t
1110
1111
else case st' of
@@ -1141,7 +1142,7 @@ deleteKeyExists !collPos0 !h0 !k0 !m0 = go collPos0 h0 k0 0 m0
1141
1142
go ! _collPos ! _h ! _k ! _s (Leaf _ _) = Empty
1142
1143
go collPos h k s (BitmapIndexed b ary) =
1143
1144
let ! st = A. index ary i
1144
- ! st' = go collPos h k (s + bitsPerSubkey ) st
1145
+ ! st' = go collPos h k (nextShift s ) st
1145
1146
in case st' of
1146
1147
Empty | A. length ary == 1 -> Empty
1147
1148
| A. length ary == 2 ->
@@ -1158,7 +1159,7 @@ deleteKeyExists !collPos0 !h0 !k0 !m0 = go collPos0 h0 k0 0 m0
1158
1159
i = sparseIndex b m
1159
1160
go collPos h k s (Full ary) =
1160
1161
let ! st = A. index ary i
1161
- ! st' = go collPos h k (s + bitsPerSubkey ) st
1162
+ ! st' = go collPos h k (nextShift s ) st
1162
1163
in case st' of
1163
1164
Empty ->
1164
1165
let ary' = A. delete ary i
@@ -1202,7 +1203,7 @@ adjust# f k0 m0 = go h0 k0 0 m0
1202
1203
go h k s t@ (BitmapIndexed b ary)
1203
1204
| b .&. m == 0 = t
1204
1205
| otherwise = let ! st = A. index ary i
1205
- ! st' = go h k (s + bitsPerSubkey ) st
1206
+ ! st' = go h k (nextShift s ) st
1206
1207
ary' = A. update ary i $! st'
1207
1208
in if ptrEq st st'
1208
1209
then t
@@ -1212,7 +1213,7 @@ adjust# f k0 m0 = go h0 k0 0 m0
1212
1213
go h k s t@ (Full ary) =
1213
1214
let i = index h s
1214
1215
! st = A. index ary i
1215
- ! st' = go h k (s + bitsPerSubkey ) st
1216
+ ! st' = go h k (nextShift s ) st
1216
1217
ary' = update32 ary i $! st'
1217
1218
in if ptrEq st st'
1218
1219
then t
@@ -1459,21 +1460,21 @@ isSubmapOfBy comp !m1 !m2 = go 0 m1 m2
1459
1460
go s t1@ (Collision h1 _) (BitmapIndexed b ls2)
1460
1461
| b .&. m == 0 = False
1461
1462
| otherwise =
1462
- go (s + bitsPerSubkey ) t1 (A. index ls2 (sparseIndex b m))
1463
+ go (nextShift s ) t1 (A. index ls2 (sparseIndex b m))
1463
1464
where m = mask h1 s
1464
1465
1465
1466
-- Similar to the previous case we need to traverse l2 at the index for the hash h1.
1466
1467
go s t1@ (Collision h1 _) (Full ls2) =
1467
- go (s + bitsPerSubkey ) t1 (A. index ls2 (index h1 s))
1468
+ go (nextShift s ) t1 (A. index ls2 (index h1 s))
1468
1469
1469
1470
-- In cases where the first and second map are BitmapIndexed or Full,
1470
1471
-- traverse down the tree at the appropriate indices.
1471
1472
go s (BitmapIndexed b1 ls1) (BitmapIndexed b2 ls2) =
1472
- submapBitmapIndexed (go (s + bitsPerSubkey )) b1 ls1 b2 ls2
1473
+ submapBitmapIndexed (go (nextShift s )) b1 ls1 b2 ls2
1473
1474
go s (BitmapIndexed b1 ls1) (Full ls2) =
1474
- submapBitmapIndexed (go (s + bitsPerSubkey )) b1 ls1 fullBitmap ls2
1475
+ submapBitmapIndexed (go (nextShift s )) b1 ls1 fullBitmap ls2
1475
1476
go s (Full ls1) (Full ls2) =
1476
- submapBitmapIndexed (go (s + bitsPerSubkey )) fullBitmap ls1 fullBitmap ls2
1477
+ submapBitmapIndexed (go (nextShift s )) fullBitmap ls1 fullBitmap ls2
1477
1478
1478
1479
-- Collision and Full nodes always contain at least two entries. Hence it
1479
1480
-- cannot be a map of a leaf.
@@ -1559,16 +1560,16 @@ unionWithKey f = go 0
1559
1560
-- branch vs. branch
1560
1561
go s (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
1561
1562
let b' = b1 .|. b2
1562
- ary' = unionArrayBy (go (s + bitsPerSubkey )) b1 b2 ary1 ary2
1563
+ ary' = unionArrayBy (go (nextShift s )) b1 b2 ary1 ary2
1563
1564
in bitmapIndexedOrFull b' ary'
1564
1565
go s (BitmapIndexed b1 ary1) (Full ary2) =
1565
- let ary' = unionArrayBy (go (s + bitsPerSubkey )) b1 fullBitmap ary1 ary2
1566
+ let ary' = unionArrayBy (go (nextShift s )) b1 fullBitmap ary1 ary2
1566
1567
in Full ary'
1567
1568
go s (Full ary1) (BitmapIndexed b2 ary2) =
1568
- let ary' = unionArrayBy (go (s + bitsPerSubkey )) fullBitmap b2 ary1 ary2
1569
+ let ary' = unionArrayBy (go (nextShift s )) fullBitmap b2 ary1 ary2
1569
1570
in Full ary'
1570
1571
go s (Full ary1) (Full ary2) =
1571
- let ary' = unionArrayBy (go (s + bitsPerSubkey )) fullBitmap fullBitmap
1572
+ let ary' = unionArrayBy (go (nextShift s )) fullBitmap fullBitmap
1572
1573
ary1 ary2
1573
1574
in Full ary'
1574
1575
-- leaf vs. branch
@@ -1577,7 +1578,7 @@ unionWithKey f = go 0
1577
1578
b' = b1 .|. m2
1578
1579
in bitmapIndexedOrFull b' ary'
1579
1580
| otherwise = let ary' = A. updateWith' ary1 i $ \ st1 ->
1580
- go (s + bitsPerSubkey ) st1 t2
1581
+ go (nextShift s ) st1 t2
1581
1582
in BitmapIndexed b1 ary'
1582
1583
where
1583
1584
h2 = leafHashCode t2
@@ -1588,7 +1589,7 @@ unionWithKey f = go 0
1588
1589
b' = b2 .|. m1
1589
1590
in bitmapIndexedOrFull b' ary'
1590
1591
| otherwise = let ary' = A. updateWith' ary2 i $ \ st2 ->
1591
- go (s + bitsPerSubkey ) t1 st2
1592
+ go (nextShift s ) t1 st2
1592
1593
in BitmapIndexed b2 ary'
1593
1594
where
1594
1595
h1 = leafHashCode t1
@@ -1597,20 +1598,20 @@ unionWithKey f = go 0
1597
1598
go s (Full ary1) t2 =
1598
1599
let h2 = leafHashCode t2
1599
1600
i = index h2 s
1600
- ary' = update32With' ary1 i $ \ st1 -> go (s + bitsPerSubkey ) st1 t2
1601
+ ary' = update32With' ary1 i $ \ st1 -> go (nextShift s ) st1 t2
1601
1602
in Full ary'
1602
1603
go s t1 (Full ary2) =
1603
1604
let h1 = leafHashCode t1
1604
1605
i = index h1 s
1605
- ary' = update32With' ary2 i $ \ st2 -> go (s + bitsPerSubkey ) t1 st2
1606
+ ary' = update32With' ary2 i $ \ st2 -> go (nextShift s ) t1 st2
1606
1607
in Full ary'
1607
1608
1608
1609
leafHashCode (Leaf h _) = h
1609
1610
leafHashCode (Collision h _) = h
1610
1611
leafHashCode _ = error " leafHashCode"
1611
1612
1612
1613
goDifferentHash s h1 h2 t1 t2
1613
- | m1 == m2 = BitmapIndexed m1 (A. singleton $! goDifferentHash (s + bitsPerSubkey ) h1 h2 t1 t2)
1614
+ | m1 == m2 = BitmapIndexed m1 (A. singleton $! goDifferentHash (nextShift s ) h1 h2 t1 t2)
1614
1615
| m1 < m2 = BitmapIndexed (m1 .|. m2) (A. pair t1 t2)
1615
1616
| otherwise = BitmapIndexed (m1 .|. m2) (A. pair t2 t1)
1616
1617
where
@@ -1812,30 +1813,30 @@ intersectionWithKey# f = go 0
1812
1813
go _ (Collision h1 ls1) (Collision h2 ls2) = intersectionCollisions f h1 h2 ls1 ls2
1813
1814
-- branch vs. branch
1814
1815
go s (BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) =
1815
- intersectionArrayBy (go (s + bitsPerSubkey )) b1 b2 ary1 ary2
1816
+ intersectionArrayBy (go (nextShift s )) b1 b2 ary1 ary2
1816
1817
go s (BitmapIndexed b1 ary1) (Full ary2) =
1817
- intersectionArrayBy (go (s + bitsPerSubkey )) b1 fullBitmap ary1 ary2
1818
+ intersectionArrayBy (go (nextShift s )) b1 fullBitmap ary1 ary2
1818
1819
go s (Full ary1) (BitmapIndexed b2 ary2) =
1819
- intersectionArrayBy (go (s + bitsPerSubkey )) fullBitmap b2 ary1 ary2
1820
+ intersectionArrayBy (go (nextShift s )) fullBitmap b2 ary1 ary2
1820
1821
go s (Full ary1) (Full ary2) =
1821
- intersectionArrayBy (go (s + bitsPerSubkey )) fullBitmap fullBitmap ary1 ary2
1822
+ intersectionArrayBy (go (nextShift s )) fullBitmap fullBitmap ary1 ary2
1822
1823
-- collision vs. branch
1823
1824
go s (BitmapIndexed b1 ary1) t2@ (Collision h2 _ls2)
1824
1825
| b1 .&. m2 == 0 = Empty
1825
- | otherwise = go (s + bitsPerSubkey ) (A. index ary1 i) t2
1826
+ | otherwise = go (nextShift s ) (A. index ary1 i) t2
1826
1827
where
1827
1828
m2 = mask h2 s
1828
1829
i = sparseIndex b1 m2
1829
1830
go s t1@ (Collision h1 _ls1) (BitmapIndexed b2 ary2)
1830
1831
| b2 .&. m1 == 0 = Empty
1831
- | otherwise = go (s + bitsPerSubkey ) t1 (A. index ary2 i)
1832
+ | otherwise = go (nextShift s ) t1 (A. index ary2 i)
1832
1833
where
1833
1834
m1 = mask h1 s
1834
1835
i = sparseIndex b2 m1
1835
- go s (Full ary1) t2@ (Collision h2 _ls2) = go (s + bitsPerSubkey ) (A. index ary1 i) t2
1836
+ go s (Full ary1) t2@ (Collision h2 _ls2) = go (nextShift s ) (A. index ary1 i) t2
1836
1837
where
1837
1838
i = index h2 s
1838
- go s t1@ (Collision h1 _ls1) (Full ary2) = go (s + bitsPerSubkey ) t1 (A. index ary2 i)
1839
+ go s t1@ (Collision h1 _ls1) (Full ary2) = go (nextShift s ) t1 (A. index ary2 i)
1839
1840
where
1840
1841
i = index h1 s
1841
1842
{-# INLINE intersectionWithKey# #-}
@@ -2435,6 +2436,11 @@ fullBitmap :: Bitmap
2435
2436
fullBitmap = complement (complement 0 `shiftL` maxChildren)
2436
2437
{-# INLINE fullBitmap #-}
2437
2438
2439
+ -- | Increment a 'Shift' for use at the next deeper level.
2440
+ nextShift :: Shift -> Shift
2441
+ nextShift s = s + bitsPerSubkey
2442
+ {-# INLINE nextShift #-}
2443
+
2438
2444
------------------------------------------------------------------------
2439
2445
-- Pointer equality
2440
2446
0 commit comments