Skip to content

Commit 2d12c48

Browse files
committed
move helper function clearLowest from global to local,
repair forw 0, remove work-around in powerSet
1 parent 6d61346 commit 2d12c48

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

containers/src/Data/Set/Internal.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,6 @@ splitRoot orig =
18281828
-- @since 0.5.11
18291829

18301830
powerSet :: Set a -> Set (Set a)
1831-
powerSet xs | null xs = singleton xs
18321831
powerSet xs =
18331832
let !w = length xs
18341833
!u = A.listArray (0, w-1) $ toList xs
@@ -1850,6 +1849,7 @@ generateA :: A.Ix i => (i,i) -> (i -> a) -> A.Array i a
18501849
generateA bnd f = A.listArray bnd $ fmap f $ A.range bnd
18511850

18521851
forw :: Int -> Int -> Int
1852+
forw 0 _ = 0
18531853
forw !width !n = fr_go_branch (bit $ width-1) n 0
18541854

18551855
fr_go_branch :: Int -> Int -> Int -> Int
@@ -1867,16 +1867,14 @@ data StrictTriple = ST !Int !Int !Int
18671867
-- index of middle bit, bitmask for lower half
18681868
splitBits :: Int -> StrictTriple
18691869
splitBits m =
1870-
let go 0 !x = x; go k !x = go (k-1) (clearLowest x)
1871-
!up_med = go (div (popCount m) 2) m
1872-
!lo = xor m up_med
1873-
!up = clearLowest up_med
1874-
!med = xor up_med up
1870+
let clearLowest !x = x .&. (x-1)
1871+
go 0 !x = x; go k !x = go (k-1) (clearLowest x)
1872+
up_med = go (div (popCount m) 2) m
1873+
lo = xor m up_med
1874+
up = clearLowest up_med
1875+
med = xor up_med up
18751876
in ST up (countTrailingZeros med) lo
18761877

1877-
clearLowest :: Int -> Int
1878-
clearLowest m = m .&. (m-1)
1879-
18801878
-- Proof of complexity: step executes n times. At the ith step,
18811879
-- "insertMin x `mapMonotonic` pxs" takes O(2^i log i) time since pxs has size
18821880
-- 2^i - 1 and we insertMin into its elements which are sets of size <= i.

0 commit comments

Comments
 (0)