Skip to content

Commit 53ba2ee

Browse files
committed
Replace all remaining usages of Data.Array with Data.Vector
1 parent 1095f26 commit 53ba2ee

File tree

5 files changed

+55
-57
lines changed

5 files changed

+55
-57
lines changed

Math/NumberTheory/Primes/Counting/Impl.hs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import Math.NumberTheory.Roots
2828
import Math.NumberTheory.Utils.FromIntegral
2929

3030
import Control.Monad.ST
31-
import Data.Array.Base
32-
import Data.Array.ST
3331
import Data.Bit
3432
import Data.Bits
3533
import Data.Int
@@ -269,8 +267,8 @@ treat end n old new = do
269267
!val <- MU.unsafeRead old (qi+1)
270268
let !q0 = key `quot` n
271269
!r0 = int64ToInt (q0 `rem` 30030)
272-
!nkey = q0 - int8ToInt64 (cpDfAr `unsafeAt` r0)
273-
nk0 = q0 + int8ToInt64 (cpGpAr `unsafeAt` (r0+1) + 1)
270+
!nkey = q0 - int8ToInt64 (cpDfAr `U.unsafeIndex` r0)
271+
nk0 = q0 + int8ToInt64 (cpGpAr `U.unsafeIndex` (r0+1) + 1)
274272
!nlim = n*nk0
275273
(wi1,ci1) <- copyTo end nkey old ci new wi
276274
ckey <- MU.unsafeRead old ci1
@@ -332,27 +330,27 @@ cp6 :: Int64 -> Integer
332330
cp6 k =
333331
case k `quotRem` 30030 of
334332
(q,r) -> 5760*int64ToInteger q +
335-
int16ToInteger (cpCtAr `unsafeAt` int64ToInt r)
333+
int16ToInteger (cpCtAr `U.unsafeIndex` int64ToInt r)
336334

337335
cop :: Int64 -> Int64
338-
cop m = m - int8ToInt64 (cpDfAr `unsafeAt` int64ToInt (m `rem` 30030))
336+
cop m = m - int8ToInt64 (cpDfAr `U.unsafeIndex` int64ToInt (m `rem` 30030))
339337

340338

341339
--------------------------------------------------------------------------------
342340
-- Ugly helper arrays --
343341
--------------------------------------------------------------------------------
344342

345-
cpCtAr :: UArray Int Int16
346-
cpCtAr = runSTUArray $ do
347-
ar <- newArray (0,30029) 1
343+
cpCtAr :: U.Vector Int16
344+
cpCtAr = runST $ do
345+
ar <- MU.replicate 30030 1
348346
let zilch s i
349-
| i < 30030 = unsafeWrite ar i 0 >> zilch s (i+s)
347+
| i < 30030 = MU.unsafeWrite ar i 0 >> zilch s (i+s)
350348
| otherwise = return ()
351349
accumulate ct i
352350
| i < 30030 = do
353-
v <- unsafeRead ar i
351+
v <- MU.unsafeRead ar i
354352
let !ct' = ct+v
355-
unsafeWrite ar i ct'
353+
MU.unsafeWrite ar i ct'
356354
accumulate ct' (i+1)
357355
| otherwise = return ar
358356
zilch 2 0
@@ -361,20 +359,21 @@ cpCtAr = runSTUArray $ do
361359
zilch 14 7
362360
zilch 22 11
363361
zilch 26 13
364-
accumulate 1 2
362+
vec <- accumulate 1 2
363+
U.unsafeFreeze vec
365364

366-
cpDfAr :: UArray Int Int8
367-
cpDfAr = runSTUArray $ do
368-
ar <- newArray (0,30029) 0
365+
cpDfAr :: U.Vector Int8
366+
cpDfAr = runST $ do
367+
ar <- MU.replicate 30030 0
369368
let note s i
370-
| i < 30029 = unsafeWrite ar i 1 >> note s (i+s)
369+
| i < 30029 = MU.unsafeWrite ar i 1 >> note s (i+s)
371370
| otherwise = return ()
372371
accumulate d i
373372
| i < 30029 = do
374-
v <- unsafeRead ar i
373+
v <- MU.unsafeRead ar i
375374
if v == 0
376375
then accumulate 2 (i+2)
377-
else do unsafeWrite ar i d
376+
else do MU.unsafeWrite ar i d
378377
accumulate (d+1) (i+1)
379378
| otherwise = return ar
380379
note 2 0
@@ -383,30 +382,32 @@ cpDfAr = runSTUArray $ do
383382
note 14 7
384383
note 22 11
385384
note 26 13
386-
accumulate 2 3
385+
vec <- accumulate 2 3
386+
U.unsafeFreeze vec
387387

388-
cpGpAr :: UArray Int Int8
389-
cpGpAr = runSTUArray $ do
390-
ar <- newArray (0,30030) 0
391-
unsafeWrite ar 30030 1
388+
cpGpAr :: U.Vector Int8
389+
cpGpAr = runST $ do
390+
ar <- MU.replicate 30031 0
391+
MU.unsafeWrite ar 30030 1
392392
let note s i
393-
| i < 30029 = unsafeWrite ar i 1 >> note s (i+s)
393+
| i < 30029 = MU.unsafeWrite ar i 1 >> note s (i+s)
394394
| otherwise = return ()
395395
accumulate d i
396396
| i < 1 = return ar
397397
| otherwise = do
398-
v <- unsafeRead ar i
398+
v <- MU.unsafeRead ar i
399399
if v == 0
400400
then accumulate 2 (i-2)
401-
else do unsafeWrite ar i d
401+
else do MU.unsafeWrite ar i d
402402
accumulate (d+1) (i-1)
403403
note 2 0
404404
note 6 3
405405
note 10 5
406406
note 14 7
407407
note 22 11
408408
note 26 13
409-
accumulate 2 30027
409+
vec <- accumulate 2 30027
410+
U.unsafeFreeze vec
410411

411412
-------------------------------------------------------------------------------
412413
-- Prime counting

Math/NumberTheory/Primes/Sieve/Eratosthenes.hs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ module Math.NumberTheory.Primes.Sieve.Eratosthenes
2626

2727
import Control.Monad (when)
2828
import Control.Monad.ST
29-
import Data.Array.Base
3029
import Data.Bit
3130
import Data.Bits
3231
import Data.Coerce
@@ -374,17 +373,17 @@ psieveFrom n = makeSieves plim sqlim bitOff valOff cache
374373

375374
{-# INLINE delta #-}
376375
delta :: Int -> Int
377-
delta = unsafeAt deltas
376+
delta = U.unsafeIndex deltas
378377

379-
deltas :: UArray Int Int
380-
deltas = listArray (0,7) [4,2,4,2,4,6,2,6]
378+
deltas :: U.Vector Int
379+
deltas = U.fromList [4,2,4,2,4,6,2,6]
381380

382381
{-# INLINE tau #-}
383382
tau :: Int -> Int
384-
tau = unsafeAt taus
383+
tau = U.unsafeIndex taus
385384

386-
taus :: UArray Int Int
387-
taus = listArray (0,63)
385+
taus :: U.Vector Int
386+
taus = U.fromList
388387
[ 7, 4, 7, 4, 7, 12, 3, 12
389388
, 12, 6, 11, 6, 12, 18, 5, 18
390389
, 14, 7, 13, 7, 14, 21, 7, 21
@@ -397,28 +396,28 @@ taus = listArray (0,63)
397396

398397
{-# INLINE byte #-}
399398
byte :: Int -> Int
400-
byte = unsafeAt startByte
399+
byte = U.unsafeIndex startByte
401400

402-
startByte :: UArray Int Int
403-
startByte = listArray (0,7) [1,3,5,9,11,17,27,31]
401+
startByte :: U.Vector Int
402+
startByte = U.fromList [1,3,5,9,11,17,27,31]
404403

405404
{-# INLINE idx #-}
406405
idx :: Int -> Int
407-
idx = unsafeAt startIdx
406+
idx = U.unsafeIndex startIdx
408407

409-
startIdx :: UArray Int Int
410-
startIdx = listArray (0,7) [4,7,4,4,7,4,7,7]
408+
startIdx :: U.Vector Int
409+
startIdx = U.fromList [4,7,4,4,7,4,7,7]
411410

412411
{-# INLINE mu #-}
413412
mu :: Int -> Int
414-
mu = unsafeAt mArr
413+
mu = U.unsafeIndex mArr
415414

416415
{-# INLINE nu #-}
417416
nu :: Int -> Int
418-
nu = unsafeAt nArr
417+
nu = U.unsafeIndex nArr
419418

420-
mArr :: UArray Int Int
421-
mArr = listArray (0,63)
419+
mArr :: U.Vector Int
420+
mArr = U.fromList
422421
[ 1, 2, 2, 3, 4, 5, 6, 7
423422
, 2, 3, 4, 6, 6, 8, 10, 11
424423
, 2, 4, 5, 7, 8, 9, 12, 13
@@ -429,8 +428,8 @@ mArr = listArray (0,63)
429428
, 7, 11, 13, 17, 19, 23, 29, 31
430429
]
431430

432-
nArr :: UArray Int Int
433-
nArr = listArray (0,63)
431+
nArr :: U.Vector Int
432+
nArr = U.fromList
434433
[ 4, 3, 7, 6, 2, 1, 5, 0
435434
, 3, 7, 5, 0, 6, 2, 4, 1
436435
, 7, 5, 4, 1, 0, 6, 3, 2

Math/NumberTheory/Primes/Sieve/Indexing.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module Math.NumberTheory.Primes.Sieve.Indexing
1212
, rho
1313
) where
1414

15-
import Data.Array.Base
1615
import Data.Bits
16+
import qualified Data.Vector.Unboxed as U
1717

1818
{-# INLINE idxPr #-}
1919
idxPr :: Integral a => a -> (Int,Int)
@@ -37,7 +37,7 @@ toPrim ix = 30*fromIntegral k + fromIntegral (rho i)
3737

3838
{-# INLINE rho #-}
3939
rho :: Int -> Int
40-
rho = unsafeAt residues
40+
rho = U.unsafeIndex residues
4141

42-
residues :: UArray Int Int
43-
residues = listArray (0,7) [7,11,13,17,19,23,29,31]
42+
residues :: U.Vector Int
43+
residues = U.fromList [7,11,13,17,19,23,29,31]

arithmoi.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ source-repository head
2828
library
2929
build-depends:
3030
base >=4.15 && <5,
31-
array >=0.5 && <0.6,
3231
bitvec >=1.1 && <1.2,
3332
containers >=0.5.11 && <0.9,
3433
chimera >=0.3 && <0.5,
@@ -177,7 +176,6 @@ benchmark arithmoi-bench
177176
build-depends:
178177
base,
179178
arithmoi,
180-
array,
181179
constraints,
182180
containers,
183181
deepseq,

benchmark/Math/NumberTheory/SequenceBench.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Math.NumberTheory.SequenceBench
66

77
import Test.Tasty.Bench
88

9-
import Data.Array.Unboxed
109
import Data.Bits
10+
import qualified Data.Vector.Unboxed as U
1111

1212
import Math.NumberTheory.Primes (Prime(..), nextPrime, precPrime)
1313
import Math.NumberTheory.Primes.Testing
@@ -43,10 +43,10 @@ benchSuite = bgroup "Sequence"
4343
-- Utils copypasted from internal modules
4444

4545
rho :: Int -> Int
46-
rho i = residues ! i
46+
rho i = residues U.! i
4747

48-
residues :: UArray Int Int
49-
residues = listArray (0,7) [7,11,13,17,19,23,29,31]
48+
residues :: U.Vector Int
49+
residues = U.fromList [7,11,13,17,19,23,29,31]
5050

5151
toIdx :: Integral a => a -> Int
5252
toIdx n = 8*fromIntegral q+r2

0 commit comments

Comments
 (0)