Skip to content

Commit c89ab67

Browse files
RyanGlScotterikd
authored andcommitted
Allow building with vector-0.13.*
Adapted from the patch in #39, with some minor tweaks: * Since the `HasCallStack` constraint isn't available on all versions of GHC that `vector-algorithms` supports, I decided to omit the additional `HasCallStack` constraints that #39 adds. * I used `stToPrim` to accommodate the generic `Vector` classes specializing all of the monads to `ST` instead of being polymorphic over `MonadPrim m`. * I also fixed the benchmark suite.
1 parent 4631197 commit c89ab67

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

bench/simple/Main.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import Data.Char
1212
import Data.Ord (comparing)
1313
import Data.List (maximumBy)
1414

15-
import Data.Vector.Unboxed.Mutable
15+
import qualified Data.Vector.Unboxed.Mutable as UVector
16+
import Data.Vector.Unboxed.Mutable (MVector, Unbox)
1617

1718
import qualified Data.Vector.Algorithms.Insertion as INS
1819
import qualified Data.Vector.Algorithms.Intro as INT
@@ -35,8 +36,8 @@ noalgo _ = return ()
3536
-- Allocates a temporary buffer, like mergesort for similar purposes as noalgo.
3637
alloc :: (Unbox e) => MVector RealWorld e -> IO ()
3738
alloc arr | len <= 4 = arr `seq` return ()
38-
| otherwise = (new (len `div` 2) :: IO (MVector RealWorld Int)) >> return ()
39-
where len = length arr
39+
| otherwise = (UVector.new (len `div` 2) :: IO (MVector RealWorld Int)) >> return ()
40+
where len = UVector.length arr
4041

4142
displayTime :: String -> Integer -> IO ()
4243
displayTime s elapsed = putStrLn $
@@ -47,7 +48,7 @@ run s t = t >>= displayTime s
4748

4849
sortSuite :: String -> GenIO -> Int -> (MVector RealWorld Int -> IO ()) -> IO ()
4950
sortSuite str g n sort = do
50-
arr <- new n
51+
arr <- UVector.new n
5152
putStrLn $ "Testing: " ++ str
5253
run "Random " $ speedTest arr n (rand g >=> modulo n) sort
5354
run "Sorted " $ speedTest arr n ascend sort

src/Data/Vector/Algorithms/Common.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ resizeVector !src !sz = do
121121
copyToSmaller
122122
:: (MVector v a, PrimMonad m)
123123
=> v (PrimState m) a -> v (PrimState m) a -> m ()
124-
copyToSmaller !dst !src = do_copy 0
124+
copyToSmaller !dst !src = stToPrim $ do_copy 0
125125
where
126126
!n = basicLength dst
127127

src/Data/Vector/Algorithms/Optimal.hs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ import Data.Vector.Generic.Mutable
4040

4141
import Data.Vector.Algorithms.Common (Comparison)
4242

43+
#if MIN_VERSION_vector(0,13,0)
44+
import qualified Data.Vector.Internal.Check as Ck
45+
# define CHECK_INDEX(name, i, n) Ck.checkIndex Ck.Unsafe (i) (n)
46+
#else
47+
# define CHECK_INDEX(name, i, n) UNSAFE_CHECK(checkIndex) name (i) (n)
48+
#endif
49+
4350
#include "vector.h"
4451

4552
-- | Sorts the elements at the positions 'off' and 'off + 1' in the given
@@ -54,8 +61,8 @@ sort2ByOffset cmp a off = sort2ByIndex cmp a off (off + 1)
5461
-- be the 'lower' of the two.
5562
sort2ByIndex :: (PrimMonad m, MVector v e)
5663
=> Comparison e -> v (PrimState m) e -> Int -> Int -> m ()
57-
sort2ByIndex cmp a i j = UNSAFE_CHECK(checkIndex) "sort2ByIndex" i (length a)
58-
$ UNSAFE_CHECK(checkIndex) "sort2ByIndex" j (length a) $ do
64+
sort2ByIndex cmp a i j = CHECK_INDEX("sort2ByIndex", i, length a)
65+
$ CHECK_INDEX("sort2ByIndex", j, length a) $ do
5966
a0 <- unsafeRead a i
6067
a1 <- unsafeRead a j
6168
case cmp a0 a1 of
@@ -75,9 +82,9 @@ sort3ByOffset cmp a off = sort3ByIndex cmp a off (off + 1) (off + 2)
7582
-- lowest position in the array.
7683
sort3ByIndex :: (PrimMonad m, MVector v e)
7784
=> Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> m ()
78-
sort3ByIndex cmp a i j k = UNSAFE_CHECK(checkIndex) "sort3ByIndex" i (length a)
79-
$ UNSAFE_CHECK(checkIndex) "sort3ByIndex" j (length a)
80-
$ UNSAFE_CHECK(checkIndex) "sort3ByIndex" k (length a) $ do
85+
sort3ByIndex cmp a i j k = CHECK_INDEX("sort3ByIndex", i, length a)
86+
$ CHECK_INDEX("sort3ByIndex", j, length a)
87+
$ CHECK_INDEX("sort3ByIndex", k, length a) $ do
8188
a0 <- unsafeRead a i
8289
a1 <- unsafeRead a j
8390
a2 <- unsafeRead a k
@@ -114,10 +121,10 @@ sort4ByOffset cmp a off = sort4ByIndex cmp a off (off + 1) (off + 2) (off + 3)
114121
-- it can be used to sort medians into particular positions and so on.
115122
sort4ByIndex :: (PrimMonad m, MVector v e)
116123
=> Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> Int -> m ()
117-
sort4ByIndex cmp a i j k l = UNSAFE_CHECK(checkIndex) "sort4ByIndex" i (length a)
118-
$ UNSAFE_CHECK(checkIndex) "sort4ByIndex" j (length a)
119-
$ UNSAFE_CHECK(checkIndex) "sort4ByIndex" k (length a)
120-
$ UNSAFE_CHECK(checkIndex) "sort4ByIndex" l (length a) $ do
124+
sort4ByIndex cmp a i j k l = CHECK_INDEX("sort4ByIndex", i, length a)
125+
$ CHECK_INDEX("sort4ByIndex", j, length a)
126+
$ CHECK_INDEX("sort4ByIndex", k, length a)
127+
$ CHECK_INDEX("sort4ByIndex", l, length a) $ do
121128
a0 <- unsafeRead a i
122129
a1 <- unsafeRead a j
123130
a2 <- unsafeRead a k

tests/properties/Optimal.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Optimal where
88
import Control.Arrow
99
import Control.Monad
1010

11-
import Data.List
11+
import qualified Data.List as List
1212
import Data.Function
1313

1414
import Data.Vector.Generic hiding (map, zip, concatMap, (++), replicate, foldM)
@@ -32,18 +32,18 @@ monotones k = atLeastOne 0
3232
stability :: (Vector v (Int,Int)) => Int -> [v (Int, Int)]
3333
stability n = concatMap ( map fromList
3434
. foldM interleavings []
35-
. groupBy ((==) `on` fst)
35+
. List.groupBy ((==) `on` fst)
3636
. flip zip [0..])
3737
$ monotones (n-2) n
3838

3939
sort2 :: (Vector v Int) => [v Int]
40-
sort2 = map fromList $ permutations [0,1]
40+
sort2 = map fromList $ List.permutations [0,1]
4141

4242
stability2 :: (Vector v (Int,Int)) => [v (Int, Int)]
4343
stability2 = [fromList [(0, 0), (0, 1)]]
4444

4545
sort3 :: (Vector v Int) => [v Int]
46-
sort3 = map fromList $ permutations [0..2]
46+
sort3 = map fromList $ List.permutations [0..2]
4747

4848
{-
4949
stability3 :: [UArr (Int :*: Int)]
@@ -58,5 +58,5 @@ stability3 = map toU [ [0:*:0, 0:*:1, 0:*:2]
5858
-}
5959

6060
sort4 :: (Vector v Int) => [v Int]
61-
sort4 = map fromList $ permutations [0..3]
61+
sort4 = map fromList $ List.permutations [0..3]
6262

0 commit comments

Comments
 (0)