module Data.Repa.Array.Material.Boxed
( B (..)
, Name (..)
, Array (..)
, Buffer (..)
, fromBoxed, toBoxed
, decimate)
where
import Data.Repa.Array.Meta.Window as A
import Data.Repa.Array.Generic.Index as A
import Data.Repa.Array.Internals.Bulk as A
import Data.Repa.Array.Internals.Target as A
import Data.Word
import Control.Monad
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
#include "repa-array.h"
data B = Boxed { boxedLength :: !Int }
deriving (Show, Eq)
instance Layout B where
data Name B = B
type Index B = Int
name = B
create B len = Boxed len
extent (Boxed len) = len
toIndex _ ix = ix
fromIndex _ ix = ix
deriving instance Eq (Name B)
deriving instance Show (Name B)
instance Bulk B a where
data Array B a = BArray !(V.Vector a)
layout (BArray vec) = Boxed (V.length vec)
index (BArray vec) ix = V.unsafeIndex vec ix
deriving instance Show a => Show (Array B a)
instance Eq a => Eq (Array B a) where
(==) (BArray arr1) (BArray arr2) = arr1 == arr2
instance Windowable B a where
window st len (BArray vec)
= BArray (V.unsafeSlice st len vec)
instance Target B a where
data Buffer B a
= BBuffer !(VM.IOVector a)
unsafeNewBuffer (Boxed len)
= liftM BBuffer (VM.unsafeNew len)
unsafeReadBuffer (BBuffer mvec) ix
= VM.unsafeRead mvec ix
unsafeWriteBuffer (BBuffer mvec) ix
= VM.unsafeWrite mvec ix
unsafeGrowBuffer (BBuffer mvec) bump
= liftM BBuffer (VM.unsafeGrow mvec bump)
unsafeFreezeBuffer (BBuffer mvec)
= liftM BArray (V.unsafeFreeze mvec)
unsafeThawBuffer (BArray vec)
= liftM BBuffer (V.unsafeThaw vec)
unsafeSliceBuffer start len (BBuffer mvec)
= let mvec' = VM.unsafeSlice start len mvec
in return $ BBuffer mvec'
touchBuffer _
= return ()
bufferLayout (BBuffer mvec)
= Boxed (VM.length mvec)
fromBoxed :: V.Vector a -> Array B a
fromBoxed vec = BArray vec
toBoxed :: Array B a -> V.Vector a
toBoxed (BArray vec) = vec
decimate
:: (a -> a -> Bool)
-> Array B a -> Array B a
decimate f arr
| A.length arr == 0
= A.fromList B []
| otherwise
= fromBoxed
$ V.cons (arr `A.index` 0)
(V.map snd
$ V.filter (\(prev, here) -> not $ f prev here)
$ V.zip (toBoxed arr) (V.tail $ toBoxed arr))