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 2
2
module Quicksort (quicksort ) where
3
3
4
4
import 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 ))
20
6
21
7
{-# INLINEABLE quicksort #-}
22
8
quicksort :: Ord a => Int -> Int -> Array a -> Array a
23
9
quicksort ! l ! r ! xs
24
10
| r - l <= 1 = xs
25
11
| 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
28
14
(xs, m) -> quicksort l m (quicksort (m + 1 ) r (swap (r - 1 ) m xs))
29
15
30
16
{-# INLINEABLE partition #-}
Original file line number Diff line number Diff line change @@ -121,9 +121,9 @@ toList (A v) = unsafeDupablePerformIO $ do
121
121
--
122
122
-- > swap !i !j !xs =
123
123
-- > 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)
127
127
--
128
128
-- In the future, we hope to write a GHC plugin that can automatically detect
129
129
-- when pseq is necessary in common cases.
You can’t perform that action at this time.
0 commit comments