Skip to content

Commit f3eb7f7

Browse files
committed
Lazy boxed: add Unsafe modules
1 parent 3719207 commit f3eb7f7

File tree

6 files changed

+530
-465
lines changed

6 files changed

+530
-465
lines changed

vector/src/Data/Vector.hs

Lines changed: 6 additions & 307 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{-# LANGUAGE MultiParamTypeClasses #-}
55
{-# LANGUAGE RankNTypes #-}
66
{-# LANGUAGE TypeFamilies #-}
7-
87
-- |
98
-- Module : Data.Vector
109
-- Copyright : (c) Roman Leshchinskiy 2008-2010
@@ -179,275 +178,21 @@ module Data.Vector (
179178
) where
180179

181180
import Control.Applicative (Applicative)
182-
import Data.Vector.Mutable ( MVector(..) )
183-
import Data.Primitive.Array
184-
import qualified Data.Vector.Fusion.Bundle as Bundle
181+
import Data.Vector.Mutable.Unsafe ( MVector )
182+
import Data.Vector.Unsafe
185183
import qualified Data.Vector.Generic as G
186184

187-
import Control.DeepSeq ( NFData(rnf), NFData1(liftRnf))
188-
189-
import Control.Monad ( MonadPlus(..), liftM, ap )
190-
import Control.Monad.ST ( ST, runST )
185+
import Control.Monad.ST ( ST )
191186
import Control.Monad.Primitive
192-
import qualified Control.Monad.Fail as Fail
193-
import Control.Monad.Fix ( MonadFix (mfix) )
194-
import Control.Monad.Zip
195-
import Data.Function ( fix )
196187

197188
import Prelude
198-
( Eq, Ord, Num, Enum, Monoid, Functor, Monad, Show, Bool, Ordering(..), Int, Maybe, Either
199-
, compare, mempty, mappend, mconcat, return, showsPrec, fmap, otherwise, id, flip, const
200-
, (>>=), (+), (-), (<), (<=), (>), (>=), (==), (/=), (&&), (.), ($) )
189+
( Eq, Ord, Num, Enum, Monoid, Monad, Bool, Ordering(..), Int, Maybe, Either
190+
, id, (==))
201191

202-
import Data.Functor.Classes (Eq1 (..), Ord1 (..), Read1 (..), Show1 (..))
203-
import Data.Data ( Data(..) )
204-
import Text.Read ( Read(..), readListPrecDefault )
205-
import Data.Semigroup ( Semigroup(..) )
206192

207-
import qualified Control.Applicative as Applicative
208-
import qualified Data.Foldable as Foldable
209193
import qualified Data.Traversable as Traversable
210194

211-
import qualified GHC.Exts as Exts (IsList(..))
212-
213-
214-
-- | Boxed vectors, supporting efficient slicing.
215-
data Vector a = Vector {-# UNPACK #-} !Int
216-
{-# UNPACK #-} !Int
217-
{-# UNPACK #-} !(Array a)
218-
219-
instance NFData a => NFData (Vector a) where
220-
rnf = liftRnf rnf
221-
{-# INLINEABLE rnf #-}
222-
223-
-- | @since 0.12.1.0
224-
instance NFData1 Vector where
225-
liftRnf elemRnf = foldl' (\_ -> elemRnf) ()
226-
{-# INLINEABLE liftRnf #-}
227-
228-
instance Show a => Show (Vector a) where
229-
showsPrec = G.showsPrec
230-
231-
instance Read a => Read (Vector a) where
232-
readPrec = G.readPrec
233-
readListPrec = readListPrecDefault
234-
235-
instance Show1 Vector where
236-
liftShowsPrec = G.liftShowsPrec
237-
238-
instance Read1 Vector where
239-
liftReadsPrec = G.liftReadsPrec
240-
241-
instance Exts.IsList (Vector a) where
242-
type Item (Vector a) = a
243-
fromList = Data.Vector.fromList
244-
fromListN = Data.Vector.fromListN
245-
toList = toList
246-
247-
instance Data a => Data (Vector a) where
248-
gfoldl = G.gfoldl
249-
toConstr _ = G.mkVecConstr "Data.Vector.Vector"
250-
gunfold = G.gunfold
251-
dataTypeOf _ = G.mkVecType "Data.Vector.Vector"
252-
dataCast1 = G.dataCast
253-
254-
type instance G.Mutable Vector = MVector
255-
256-
instance G.Vector Vector a where
257-
{-# INLINE basicUnsafeFreeze #-}
258-
basicUnsafeFreeze (MVector i n marr)
259-
= Vector i n `liftM` unsafeFreezeArray marr
260-
261-
{-# INLINE basicUnsafeThaw #-}
262-
basicUnsafeThaw (Vector i n arr)
263-
= MVector i n `liftM` unsafeThawArray arr
264-
265-
{-# INLINE basicLength #-}
266-
basicLength (Vector _ n _) = n
267-
268-
{-# INLINE basicUnsafeSlice #-}
269-
basicUnsafeSlice j n (Vector i _ arr) = Vector (i+j) n arr
270-
271-
{-# INLINE basicUnsafeIndexM #-}
272-
basicUnsafeIndexM (Vector i _ arr) j = indexArrayM arr (i+j)
273-
274-
{-# INLINE basicUnsafeCopy #-}
275-
basicUnsafeCopy (MVector i n dst) (Vector j _ src)
276-
= copyArray dst i src j n
277-
278-
-- See http://trac.haskell.org/vector/ticket/12
279-
instance Eq a => Eq (Vector a) where
280-
{-# INLINE (==) #-}
281-
xs == ys = Bundle.eq (G.stream xs) (G.stream ys)
282-
283-
-- See http://trac.haskell.org/vector/ticket/12
284-
instance Ord a => Ord (Vector a) where
285-
{-# INLINE compare #-}
286-
compare xs ys = Bundle.cmp (G.stream xs) (G.stream ys)
287-
288-
{-# INLINE (<) #-}
289-
xs < ys = Bundle.cmp (G.stream xs) (G.stream ys) == LT
290-
291-
{-# INLINE (<=) #-}
292-
xs <= ys = Bundle.cmp (G.stream xs) (G.stream ys) /= GT
293-
294-
{-# INLINE (>) #-}
295-
xs > ys = Bundle.cmp (G.stream xs) (G.stream ys) == GT
296-
297-
{-# INLINE (>=) #-}
298-
xs >= ys = Bundle.cmp (G.stream xs) (G.stream ys) /= LT
299-
300-
instance Eq1 Vector where
301-
{-# INLINE liftEq #-}
302-
liftEq = eqBy
303-
304-
instance Ord1 Vector where
305-
{-# INLINE liftCompare #-}
306-
liftCompare = cmpBy
307-
308-
instance Semigroup (Vector a) where
309-
{-# INLINE (<>) #-}
310-
(<>) = (++)
311-
312-
{-# INLINE sconcat #-}
313-
sconcat = G.concatNE
314-
315-
instance Monoid (Vector a) where
316-
{-# INLINE mempty #-}
317-
mempty = empty
318-
319-
{-# INLINE mappend #-}
320-
mappend = (<>)
321-
322-
{-# INLINE mconcat #-}
323-
mconcat = concat
324-
325-
instance Functor Vector where
326-
{-# INLINE fmap #-}
327-
fmap = map
328-
329-
{-# INLINE (<$) #-}
330-
(<$) = map . const
331-
332-
instance Monad Vector where
333-
{-# INLINE return #-}
334-
return = Applicative.pure
335-
336-
{-# INLINE (>>=) #-}
337-
(>>=) = flip concatMap
338-
339-
-- | @since 0.12.1.0
340-
instance Fail.MonadFail Vector where
341-
{-# INLINE fail #-}
342-
fail _ = empty
343-
344-
instance MonadPlus Vector where
345-
{-# INLINE mzero #-}
346-
mzero = empty
347-
348-
{-# INLINE mplus #-}
349-
mplus = (++)
350-
351-
instance MonadZip Vector where
352-
{-# INLINE mzip #-}
353-
mzip = zip
354-
355-
{-# INLINE mzipWith #-}
356-
mzipWith = zipWith
357195

358-
{-# INLINE munzip #-}
359-
munzip = unzip
360-
361-
-- | This instance has the same semantics as the one for lists.
362-
--
363-
-- @since 0.12.2.0
364-
instance MonadFix Vector where
365-
-- We take care to dispose of v0 as soon as possible (see headM docs).
366-
--
367-
-- It's perfectly safe to use non-monadic indexing within generate
368-
-- call since intermediate vector won't be created until result's
369-
-- value is demanded.
370-
{-# INLINE mfix #-}
371-
mfix f
372-
| null v0 = empty
373-
-- We take first element of resulting vector from v0 and create
374-
-- rest using generate. Note that cons should fuse with generate
375-
| otherwise = runST $ do
376-
h <- headM v0
377-
return $ cons h $
378-
generate (lv0 - 1) $
379-
\i -> fix (\a -> f a ! (i + 1))
380-
where
381-
-- Used to calculate size of resulting vector
382-
v0 = fix (f . head)
383-
!lv0 = length v0
384-
385-
instance Applicative.Applicative Vector where
386-
{-# INLINE pure #-}
387-
pure = singleton
388-
389-
{-# INLINE (<*>) #-}
390-
(<*>) = ap
391-
392-
instance Applicative.Alternative Vector where
393-
{-# INLINE empty #-}
394-
empty = empty
395-
396-
{-# INLINE (<|>) #-}
397-
(<|>) = (++)
398-
399-
instance Foldable.Foldable Vector where
400-
{-# INLINE foldr #-}
401-
foldr = foldr
402-
403-
{-# INLINE foldl #-}
404-
foldl = foldl
405-
406-
{-# INLINE foldr1 #-}
407-
foldr1 = foldr1
408-
409-
{-# INLINE foldl1 #-}
410-
foldl1 = foldl1
411-
412-
{-# INLINE foldr' #-}
413-
foldr' = foldr'
414-
415-
{-# INLINE foldl' #-}
416-
foldl' = foldl'
417-
418-
{-# INLINE toList #-}
419-
toList = toList
420-
421-
{-# INLINE length #-}
422-
length = length
423-
424-
{-# INLINE null #-}
425-
null = null
426-
427-
{-# INLINE elem #-}
428-
elem = elem
429-
430-
{-# INLINE maximum #-}
431-
maximum = maximum
432-
433-
{-# INLINE minimum #-}
434-
minimum = minimum
435-
436-
{-# INLINE sum #-}
437-
sum = sum
438-
439-
{-# INLINE product #-}
440-
product = product
441-
442-
instance Traversable.Traversable Vector where
443-
{-# INLINE traverse #-}
444-
traverse = traverse
445-
446-
{-# INLINE mapM #-}
447-
mapM = mapM
448-
449-
{-# INLINE sequence #-}
450-
sequence = sequence
451196

452197
-- Length information
453198
-- ------------------
@@ -2281,52 +2026,6 @@ iforA_ :: (Applicative f)
22812026
iforA_ = G.iforA_
22822027

22832028

2284-
-- Conversions - Arrays
2285-
-- -----------------------------
2286-
2287-
-- | /O(1)/ Convert an array to a vector.
2288-
--
2289-
-- @since 0.12.2.0
2290-
fromArray :: Array a -> Vector a
2291-
{-# INLINE fromArray #-}
2292-
fromArray arr = Vector 0 (sizeofArray arr) arr
2293-
2294-
-- | /O(n)/ Convert a vector to an array.
2295-
--
2296-
-- @since 0.12.2.0
2297-
toArray :: Vector a -> Array a
2298-
{-# INLINE toArray #-}
2299-
toArray (Vector offset len arr)
2300-
| offset == 0 && len == sizeofArray arr = arr
2301-
| otherwise = cloneArray arr offset len
2302-
2303-
-- | /O(1)/ Extract the underlying `Array`, offset where vector starts and the
2304-
-- total number of elements in the vector. Below property always holds:
2305-
--
2306-
-- > let (array, offset, len) = toArraySlice v
2307-
-- > v === unsafeFromArraySlice len offset array
2308-
--
2309-
-- @since 0.13.0.0
2310-
toArraySlice :: Vector a -> (Array a, Int, Int)
2311-
{-# INLINE toArraySlice #-}
2312-
toArraySlice (Vector offset len arr) = (arr, offset, len)
2313-
2314-
2315-
-- | /O(1)/ Convert an array slice to a vector. This function is very unsafe,
2316-
-- because constructing an invalid vector can yield almost all other safe
2317-
-- functions in this module unsafe. These are equivalent:
2318-
--
2319-
-- > unsafeFromArraySlice len offset === unsafeTake len . unsafeDrop offset . fromArray
2320-
--
2321-
-- @since 0.13.0.0
2322-
unsafeFromArraySlice ::
2323-
Array a -- ^ Immutable boxed array.
2324-
-> Int -- ^ Offset
2325-
-> Int -- ^ Length
2326-
-> Vector a
2327-
{-# INLINE unsafeFromArraySlice #-}
2328-
unsafeFromArraySlice arr offset len = Vector offset len arr
2329-
23302029
-- Conversions - Mutable vectors
23312030
-- -----------------------------
23322031

@@ -2389,4 +2088,4 @@ copy = G.copy
23892088

23902089
-- $setup
23912090
-- >>> :set -Wno-type-defaults
2392-
-- >>> import Prelude (Char, String, Bool(True, False), min, max, fst, even, undefined, Ord(..))
2091+
-- >>> import Prelude (Char, String, Bool(True, False), min, max, fst, even, undefined, Ord(..), ($), (<>), Num(..))

0 commit comments

Comments
 (0)