Skip to content

Commit 900dcff

Browse files
Shimuuarlehins
andauthored
Add accessors for Vector/MVector fields
Co-authored-by: Alexey Kuleshevich <alexey@kuleshevi.ch>
1 parent 173efb6 commit 900dcff

6 files changed

Lines changed: 26 additions & 14 deletions

File tree

vector/src/Data/Vector/Mutable/Unsafe.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ import Prelude
3131
type role MVector nominal representational
3232

3333
-- | Mutable boxed vectors keyed on the monad they live in ('IO' or @'ST' s@).
34-
data MVector s a = MVector { _offset :: {-# UNPACK #-} !Int
34+
data MVector s a = MVector { unsafeOffset :: {-# UNPACK #-} !Int
3535
-- ^ Offset in underlying array
36-
, _size :: {-# UNPACK #-} !Int
36+
, unsafeSize :: {-# UNPACK #-} !Int
3737
-- ^ Size of slice
38-
, _array :: {-# UNPACK #-} !(MutableArray s a)
39-
-- ^ Underlying array
38+
, unsafeMutableArray :: {-# UNPACK #-} !(MutableArray s a)
39+
-- ^ Underlying mutable array
40+
}
4041
}
4142

4243

vector/src/Data/Vector/Primitive/Mutable/Unsafe.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ import Unsafe.Coerce
4343
type role MVector nominal nominal
4444

4545
-- | Mutable vectors of primitive types.
46-
data MVector s a = MVector {-# UNPACK #-} !Int -- ^ offset
47-
{-# UNPACK #-} !Int -- ^ length
48-
{-# UNPACK #-} !(MutableByteArray s) -- ^ underlying mutable byte array
46+
data MVector s a = MVector { unsafeOffset :: {-# UNPACK #-} !Int
47+
-- ^ Offset into the `unsafeMutableByteArray` in number of elements, not bytes
48+
, unsafeSize :: {-# UNPACK #-} !Int
49+
-- ^ Size of the mutable vector in number of elements, not bytes
50+
, unsafeMutableByteArray :: {-# UNPACK #-} !(MutableByteArray s)
51+
-- ^ Underlying mutable byte array
52+
}
4953

5054
-- | /O(1)/ Unsafely coerce a mutable vector from one element type to another,
5155
-- representationally equal type. The operation just changes the type of the

vector/src/Data/Vector/Primitive/Unsafe.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ import Data.Vector.Primitive.Mutable.Unsafe (MVector(..))
4848
type role Vector nominal
4949

5050
-- | Unboxed vectors of primitive types.
51-
data Vector a = Vector {-# UNPACK #-} !Int -- ^ offset
52-
{-# UNPACK #-} !Int -- ^ length
53-
{-# UNPACK #-} !ByteArray -- ^ underlying byte array
51+
data Vector a = Vector { unsafeOffset :: {-# UNPACK #-} !Int
52+
-- ^ Offset into `unsafeByteArray` in number of elements, not bytes
53+
, unsafeSize :: {-# UNPACK #-} !Int
54+
-- ^ Number of elements in number of elements, not bytes
55+
, unsafeByteArray :: {-# UNPACK #-} !ByteArray
56+
-- ^ Underlying byte array
57+
}
5458

5559
type instance G.Mutable Vector = MVector
5660

vector/src/Data/Vector/Storable/Mutable/Unsafe.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ import Unsafe.Coerce
5858
type role MVector nominal nominal
5959

6060
-- | Mutable 'Storable'-based vectors.
61-
data MVector s a = MVector {-# UNPACK #-} !Int
62-
{-# UNPACK #-} !(ForeignPtr a)
61+
data MVector s a = MVector { unsafeSize :: {-# UNPACK #-} !Int
62+
-- ^ Number of elements in a mutable vector
63+
, unsafeForeignPtr :: {-# UNPACK #-} !(ForeignPtr a)
64+
-- ^ Underlying buffer as a `ForeignPtr`, which is allowed to be mutated
65+
}
6366

6467
type IOVector = MVector RealWorld
6568
type STVector s = MVector s

vector/src/Data/Vector/Strict/Mutable/Unsafe.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Prelude ( Monad(..), return )
2828
type role MVector nominal representational
2929

3030
-- | Mutable boxed vectors keyed on the monad they live in ('IO' or @'ST' s@).
31-
newtype MVector s a = MVector (MV.MVector s a)
31+
newtype MVector s a = MVector { unsafeLazyMVector :: MV.MVector s a }
3232

3333
instance G.MVector MVector a where
3434
{-# INLINE basicLength #-}

vector/src/Data/Vector/Strict/Unsafe.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import qualified Data.Traversable as Traversable
5454
import qualified GHC.Exts as Exts (IsList(..))
5555

5656
-- | Strict boxed vectors, supporting efficient slicing.
57-
newtype Vector a = Vector (V.Vector a)
57+
newtype Vector a = Vector { unsafeLazyVector :: V.Vector a }
5858
deriving (Foldable.Foldable, Semigroup, Monoid)
5959

6060
-- NOTE: [GND for strict vector]

0 commit comments

Comments
 (0)