File tree Expand file tree Collapse file tree 2 files changed +6
-20
lines changed
Expand file tree Collapse file tree 2 files changed +6
-20
lines changed Original file line number Diff line number Diff line change 22module Quicksort (quicksort ) where
33
44import Fleet.Array
5- import Data.Tuple (Solo (.. ))
6-
7- -- swap :: Int -> Int -> Array a -> Array a
8- -- swap !i !j !xs = set i (xs ! j) (set j (xs ! i) xs)
9-
10- -- swap :: Int -> Int -> Array a -> Array a
11- -- swap !i !j !xs =
12- -- let
13- -- -- using this strict matching on MkSolo we
14- -- -- can ensure that the indexing happens
15- -- -- before the mutation (which would slow
16- -- -- down the indexing)
17- -- !(MkSolo x) = index i xs
18- -- !(MkSolo y) = index j xs
19- -- in set i y (set j x xs)
5+ import Data.Tuple (Solo (MkSolo ))
206
217{-# INLINEABLE quicksort #-}
228quicksort :: Ord a => Int -> Int -> Array a -> Array a
239quicksort ! l ! r ! xs
2410 | r - l <= 1 = xs
2511 | otherwise =
26- let ! (MkSolo x) = index (r - 1 ) xs in
27- case partition l (r - 1 ) xs x of
12+ let x @ (MkSolo x' ) = index (r - 1 ) xs in
13+ x `pseq` case partition l (r - 1 ) xs x' of
2814 (xs, m) -> quicksort l m (quicksort (m + 1 ) r (swap (r - 1 ) m xs))
2915
3016{-# INLINEABLE partition #-}
Original file line number Diff line number Diff line change @@ -121,9 +121,9 @@ toList (A v) = unsafeDupablePerformIO $ do
121121 --
122122 -- > swap !i !j !xs =
123123 -- > let
124- -- > x = index i xs
125- -- > y = index j xs
126- -- > in x `pseq` y `pseq` set i (getSolo y) (set j (getSolo x) xs)
124+ -- > x@(MkSolo x') = index i xs
125+ -- > y@(MkSolo y') = index j xs
126+ -- > in x `pseq` y `pseq` set i y' (set j x' xs)
127127 --
128128 -- In the future, we hope to write a GHC plugin that can automatically detect
129129 -- when pseq is necessary in common cases.
You can’t perform that action at this time.
0 commit comments