@@ -60,6 +60,7 @@ module Data.Sequence (
6060 fromList , -- :: [a] -> Seq a
6161 fromFunction , -- :: Int -> (Int -> a) -> Seq a
6262 fromArray , -- :: Ix i => Array i a -> Seq a
63+ fromArrayMonolithic , -- :: Ix i => Array i a -> Seq a
6364 -- ** Repetition
6465 replicate , -- :: Int -> a -> Seq a
6566 replicateA , -- :: Applicative f => Int -> f a -> f (Seq a)
@@ -177,6 +178,9 @@ import Data.Data
177178import Data.Array (Ix , Array )
178179#ifdef __GLASGOW_HASKELL__
179180import qualified GHC.Arr
181+ import qualified Data.Primitive.Array as PA
182+ import Data.STRef
183+ import Control.Monad.ST
180184#endif
181185
182186-- Coercion on GHC 7.8+
@@ -1643,6 +1647,7 @@ fromArray a = fromFunction (GHC.Arr.numElements a) (GHC.Arr.unsafeAt a)
16431647fromArray a = fromList2 (Data.Array. rangeSize (Data.Array. bounds a)) (Data.Array. elems a)
16441648#endif
16451649
1650+
16461651-- Splitting
16471652
16481653-- | /O(log(min(i,n-i)))/. The first @i@ elements of a sequence.
@@ -2335,14 +2340,38 @@ unstableSortBy cmp (Seq xs) =
23352340 toPQ cmp (\ (Elem x) -> PQueue x Nil ) xs
23362341
23372342-- | fromList2, given a list and its length, constructs a completely
2338- -- balanced Seq whose elements are that list using the applicativeTree
2343+ -- balanced Seq whose elements are that list using the replicateA
23392344-- generalization.
23402345fromList2 :: Int -> [a ] -> Seq a
23412346fromList2 n = execState (replicateA n (State ht))
23422347 where
23432348 ht (x: xs) = (xs, x)
23442349 ht [] = error " fromList2: short list"
23452350
2351+ -- | /O(n)/. Create a sequence consisting of the elements of an 'Array'. With
2352+ -- GHC, the result of 'fromArrayMonolithic' is guaranteed not to retain any
2353+ -- references to the array, unless individual array entries contain such. To
2354+ -- accomplish this, it reads each entry out of the array before returning. With
2355+ -- other implementations, this is identical to 'fromArray'.
2356+ fromArrayMonolithic :: Ix i => Array i a -> Seq a
2357+ #ifdef __GLASGOW_HASKELL__
2358+ fromArrayMonolithic (GHC.Arr. Array _ _ len ar)
2359+ = runST (fromArrayMonolithicST (PA. Array ar))
2360+ where
2361+ {-# INLINE fromArrayMonolithicST #-}
2362+ fromArrayMonolithicST :: PA. Array a -> ST s (Seq a )
2363+ fromArrayMonolithicST a =
2364+ do
2365+ i <- newSTRef (0 :: Int )
2366+ replicateA len (do
2367+ i' <- readSTRef i
2368+ x <- PA. indexArrayM a i'
2369+ writeSTRef i (i'+ 1 )
2370+ return x)
2371+ #else
2372+ fromArrayMonolithic = fromArray
2373+ #endif
2374+
23462375-- | A 'PQueue' is a simple pairing heap.
23472376data PQueue e = PQueue e (PQL e )
23482377data PQL e = Nil | {- # UNPACK #-} !(PQueue e ) :& PQL e
0 commit comments