@@ -36,7 +36,6 @@ module Data.HashMap.Array
36
36
, unsafeThaw
37
37
, unsafeSameArray
38
38
, run
39
- , run2
40
39
, copy
41
40
, copyM
42
41
@@ -59,6 +58,11 @@ import Control.Applicative (Applicative (..), (<$>))
59
58
import Control.Applicative (liftA2 )
60
59
import Control.DeepSeq
61
60
import GHC.Exts (Int (.. ), Int #, reallyUnsafePtrEquality #, tagToEnum #, unsafeCoerce #, State #)
61
+ #if __GLASGOW_HASKELL__ >= 802
62
+ import GHC.Exts (runRW #)
63
+ #else
64
+ import GHC.Base (runRW #)
65
+ #endif
62
66
import GHC.ST (ST (.. ))
63
67
import Control.Monad.ST (stToIO )
64
68
@@ -256,13 +260,17 @@ new_ :: Int -> ST s (MArray s a)
256
260
new_ n = new n undefinedElem
257
261
258
262
singleton :: a -> Array a
259
- singleton x = runST (singletonM x)
263
+ singleton x = run (singletonMut x)
260
264
{-# INLINE singleton #-}
261
265
262
266
singletonM :: a -> ST s (Array a )
263
267
singletonM x = new 1 x >>= unsafeFreeze
264
268
{-# INLINE singletonM #-}
265
269
270
+ singletonMut :: a -> ST s (MArray s a )
271
+ singletonMut x = new 1 x
272
+ {-# INLINE singletonMut #-}
273
+
266
274
pair :: a -> a -> Array a
267
275
pair x y = run $ do
268
276
ary <- new 2 x
@@ -314,15 +322,13 @@ unsafeThaw ary
314
322
{-# INLINE unsafeThaw #-}
315
323
316
324
run :: (forall s . ST s (MArray s e )) -> Array e
317
- run act = runST $ act >>= unsafeFreeze
325
+ run (ST act) =
326
+ case runRW# $ \ s ->
327
+ case act s of { (# s', MArray mary # ) ->
328
+ unsafeFreezeArray# mary s' } of
329
+ (# _, ary # ) -> Array ary
318
330
{-# INLINE run #-}
319
331
320
- run2 :: (forall s . ST s (MArray s e , a )) -> (Array e , a )
321
- run2 k = runST (do
322
- (marr,b) <- k
323
- arr <- unsafeFreeze marr
324
- return (arr,b))
325
-
326
332
-- | Unsafely copy the elements of an array. Array bounds are not checked.
327
333
copy :: Array e -> Int -> MArray s e -> Int -> Int -> ST s ()
328
334
copy ! src ! _sidx@ (I # sidx# ) ! dst ! _didx@ (I # didx# ) _n@ (I # n# ) =
@@ -357,34 +363,38 @@ trim mary n = cloneM mary 0 n >>= unsafeFreeze
357
363
-- | /O(n)/ Insert an element at the given position in this array,
358
364
-- increasing its size by one.
359
365
insert :: Array e -> Int -> e -> Array e
360
- insert ary idx b = runST (insertM ary idx b)
366
+ insert ary idx b = run (insertMut ary idx b)
361
367
{-# INLINE insert #-}
362
368
363
369
-- | /O(n)/ Insert an element at the given position in this array,
364
370
-- increasing its size by one.
365
371
insertM :: Array e -> Int -> e -> ST s (Array e )
366
- insertM ary idx b =
372
+ insertM ary idx b = insertMut ary idx b >>= unsafeFreeze
373
+ {-# INLINE insertM #-}
374
+
375
+ insertMut :: Array e -> Int -> e -> ST s (MArray s e )
376
+ insertMut ary idx b =
367
377
CHECK_BOUNDS (" insertM" , count + 1 , idx)
368
378
do mary <- new_ (count+ 1 )
369
379
copy ary 0 mary 0 idx
370
380
write mary idx b
371
381
copy ary idx mary (idx+ 1 ) (count- idx)
372
- unsafeFreeze mary
382
+ return mary
373
383
where ! count = length ary
374
- {-# INLINE insertM #-}
384
+ {-# INLINE insertMut #-}
375
385
376
386
-- | /O(n)/ Update the element at the given position in this array.
377
387
update :: Array e -> Int -> e -> Array e
378
- update ary idx b = runST (updateM ary idx b)
388
+ update ary idx b = run (updateM ary idx b)
379
389
{-# INLINE update #-}
380
390
381
391
-- | /O(n)/ Update the element at the given position in this array.
382
- updateM :: Array e -> Int -> e -> ST s (Array e )
392
+ updateM :: Array e -> Int -> e -> ST s (MArray s e )
383
393
updateM ary idx b =
384
394
CHECK_BOUNDS (" updateM" , count, idx)
385
395
do mary <- thaw ary 0 count
386
396
write mary idx b
387
- unsafeFreeze mary
397
+ return mary
388
398
where ! count = length ary
389
399
{-# INLINE updateM #-}
390
400
@@ -442,18 +452,18 @@ thaw !ary !_o@(I# o#) !n@(I# n#) =
442
452
-- | /O(n)/ Delete an element at the given position in this array,
443
453
-- decreasing its size by one.
444
454
delete :: Array e -> Int -> Array e
445
- delete ary idx = runST (deleteM ary idx)
455
+ delete ary idx = run (deleteM ary idx)
446
456
{-# INLINE delete #-}
447
457
448
458
-- | /O(n)/ Delete an element at the given position in this array,
449
459
-- decreasing its size by one.
450
- deleteM :: Array e -> Int -> ST s (Array e )
460
+ deleteM :: Array e -> Int -> ST s (MArray s e )
451
461
deleteM ary idx = do
452
462
CHECK_BOUNDS (" deleteM" , count, idx)
453
463
do mary <- new_ (count- 1 )
454
464
copy ary 0 mary 0 idx
455
465
copy ary (idx+ 1 ) mary idx (count- (idx+ 1 ))
456
- unsafeFreeze mary
466
+ return mary
457
467
where ! count = length ary
458
468
{-# INLINE deleteM #-}
459
469
@@ -463,9 +473,10 @@ map f = \ ary ->
463
473
in run $ do
464
474
mary <- new_ n
465
475
go ary mary 0 n
476
+ return mary
466
477
where
467
478
go ary mary i n
468
- | i >= n = return mary
479
+ | i >= n = return ()
469
480
| otherwise = do
470
481
x <- indexM ary i
471
482
write mary i $ f x
@@ -479,9 +490,10 @@ map' f = \ ary ->
479
490
in run $ do
480
491
mary <- new_ n
481
492
go ary mary 0 n
493
+ return mary
482
494
where
483
495
go ary mary i n
484
- | i >= n = return mary
496
+ | i >= n = return ()
485
497
| otherwise = do
486
498
x <- indexM ary i
487
499
write mary i $! f x
@@ -494,8 +506,9 @@ fromList n xs0 =
494
506
run $ do
495
507
mary <- new_ n
496
508
go xs0 mary 0
509
+ return mary
497
510
where
498
- go [] ! mary ! _ = return mary
511
+ go [] ! _ ! _ = return ()
499
512
go (x: xs) mary i = do write mary i x
500
513
go xs mary (i+ 1 )
501
514
0 commit comments