File tree 3 files changed +22
-1
lines changed 3 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 47
47
* Add ` groupBy ` and ` group ` for ` Data.Vector.Generic ` and the specialized
48
48
version in ` Data.Vector ` , ` Data.Vector.Unboxed ` , ` Data.Vector.Storable ` and
49
49
` Data.Vector.Primitive ` .
50
+ * Add ` unsafeToArray ` function for getting the underlying boxed ` Array ` .
50
51
51
52
# Changes in version 0.12.3.1
52
53
Original file line number Diff line number Diff line change @@ -163,7 +163,7 @@ module Data.Vector (
163
163
toList , Data.Vector. fromList , Data.Vector. fromListN ,
164
164
165
165
-- ** Arrays
166
- fromArray , toArray ,
166
+ fromArray , toArray , unsafeToArray ,
167
167
168
168
-- ** Other vector types
169
169
G. convert ,
@@ -2147,6 +2147,17 @@ toArray (Vector offset size arr)
2147
2147
| offset == 0 && size == sizeofArray arr = arr
2148
2148
| otherwise = cloneArray arr offset size
2149
2149
2150
+ -- | /O(1)/ Extract the underlying `Array`, offset where vector starts and the
2151
+ -- total number of elements in the vector. Below property always holds:
2152
+ --
2153
+ -- > let (array, offset, size) = unsafeToArray v
2154
+ -- > v === unsafeTake size (unsafeDrop offset (`fromArray` array))
2155
+ --
2156
+ -- @since 0.13.0.0
2157
+ unsafeToArray :: Vector a -> (Array a , Int , Int )
2158
+ {-# INLINE unsafeToArray #-}
2159
+ unsafeToArray (Vector offset size arr) = (arr, offset, size)
2160
+
2150
2161
-- Conversions - Mutable vectors
2151
2162
-- -----------------------------
2152
2163
Original file line number Diff line number Diff line change @@ -91,6 +91,7 @@ tests =
91
91
, testGroup " Data.Vector"
92
92
[ testCase " MonadFix" checkMonadFix
93
93
, testCase " toFromArray" toFromArray
94
+ , testCase " toFromArrayUnsafe" toFromArrayUnsafe
94
95
, testCase " toFromMutableArray" toFromMutableArray
95
96
]
96
97
]
@@ -202,6 +203,14 @@ toFromArray =
202
203
mkArrayRoundtrip $ \ name v ->
203
204
assertEqual name v $ Boxed. fromArray (Boxed. toArray v)
204
205
206
+ toFromArrayUnsafe :: Assertion
207
+ toFromArrayUnsafe =
208
+ mkArrayRoundtrip $ \ name v ->
209
+ case Boxed. unsafeToArray v of
210
+ (arr, off, n) ->
211
+ assertEqual name v $
212
+ Boxed. unsafeTake n (Boxed. unsafeDrop off (Boxed. fromArray arr))
213
+
205
214
toFromMutableArray :: Assertion
206
215
toFromMutableArray = mkArrayRoundtrip assetRoundtrip
207
216
where
You can’t perform that action at this time.
0 commit comments