Skip to content

Commit 738418a

Browse files
ShimuuarRyanGlScott
authored andcommitted
Add instances for strict boxed vector added in vector-0.13.2.0
1 parent 3c770f2 commit 738418a

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

CHANGELOG.markdown

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
NEXT VERSION
2+
------------------
3+
* `Ixed`, `Cons`, `Each`, `AsEmpty`, `Reversing`, `Rewrapped` instances are
4+
added for strict boxed vectors (`vector>=0.13.2`)
5+
* `AsEmpty` instance added for primitive vector.
6+
17
5.3.3 [2024.12.28]
28
------------------
39
* Add `makeFieldsId`, which generates overloaded field accessors using the

src/Control/Lens/At.hs

+15
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ import Data.Vector.Primitive (Prim)
8585
import qualified Data.Vector.Storable as Storable
8686
import qualified Data.Vector.Unboxed as Unboxed
8787
import Data.Vector.Unboxed (Unbox)
88+
#if MIN_VERSION_vector(0,13,2)
89+
import qualified Data.Vector.Strict as VectorStrict
90+
#endif
8891
import Data.Word
8992
import Foreign.Storable (Storable)
9093

@@ -113,6 +116,9 @@ type instance Index (Vector.Vector a) = Int
113116
type instance Index (Prim.Vector a) = Int
114117
type instance Index (Storable.Vector a) = Int
115118
type instance Index (Unboxed.Vector a) = Int
119+
#if MIN_VERSION_vector(0,13,2)
120+
type instance Index (VectorStrict.Vector a) = Int
121+
#endif
116122
type instance Index (Complex a) = Int
117123
type instance Index (Identity a) = ()
118124
type instance Index (Maybe a) = ()
@@ -396,6 +402,15 @@ instance Unbox a => Ixed (Unboxed.Vector a) where
396402
| otherwise = pure v
397403
{-# INLINE ix #-}
398404

405+
#if MIN_VERSION_vector(0,13,2)
406+
type instance IxValue (VectorStrict.Vector a) = a
407+
instance Ixed (VectorStrict.Vector a) where
408+
ix i f v
409+
| 0 <= i && i < VectorStrict.length v = f (v VectorStrict.! i) <&> \a -> v VectorStrict.// [(i, a)]
410+
| otherwise = pure v
411+
{-# INLINE ix #-}
412+
#endif
413+
399414
type instance IxValue StrictT.Text = Char
400415
instance Ixed StrictT.Text where
401416
ix e f s

src/Control/Lens/Cons.hs

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ import Data.Vector.Primitive (Prim)
6464
import qualified Data.Vector.Primitive as Prim
6565
import Data.Vector.Unboxed (Unbox)
6666
import qualified Data.Vector.Unboxed as Unbox
67+
#if MIN_VERSION_vector(0,13,2)
68+
import qualified Data.Vector.Strict as VectorStrict
69+
#endif
6770
import Data.Word
6871
import Control.Applicative (ZipList(..))
6972
import Control.Monad.State.Class as State
@@ -180,6 +183,15 @@ instance (Unbox a, Unbox b) => Cons (Unbox.Vector a) (Unbox.Vector b) a b where
180183
else Right (Unbox.unsafeHead v, Unbox.unsafeTail v)
181184
{-# INLINE _Cons #-}
182185

186+
#if MIN_VERSION_vector(0,13,2)
187+
instance Cons (VectorStrict.Vector a) (VectorStrict.Vector b) a b where
188+
_Cons = prism (uncurry VectorStrict.cons) $ \v ->
189+
if VectorStrict.null v
190+
then Left VectorStrict.empty
191+
else Right (VectorStrict.unsafeHead v, VectorStrict.unsafeTail v)
192+
{-# INLINE _Cons #-}
193+
#endif
194+
183195
-- | 'cons' an element onto a container.
184196
--
185197
-- This is an infix alias for 'cons'.

src/Control/Lens/Each.hs

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ import Data.Vector.Primitive (Prim)
5454
import qualified Data.Vector.Storable as Storable
5555
import Data.Vector.Storable (Storable)
5656
import qualified Data.Vector.Unboxed as Unboxed
57+
#if MIN_VERSION_vector(0,13,2)
58+
import qualified Data.Vector.Strict as VectorStrict
59+
#endif
5760
import Data.Vector.Unboxed (Unbox)
5861
import Data.Word
5962
import qualified Data.Strict as S
@@ -195,6 +198,13 @@ instance (Unbox a, Unbox b) => Each (Unboxed.Vector a) (Unboxed.Vector b) a b wh
195198
each = vectorTraverse
196199
{-# INLINE each #-}
197200

201+
#if MIN_VERSION_vector(0,13,2)
202+
-- | @'each' :: 'Traversal' ('Vector.Vector' a) ('Vector.Vector' b) a b@
203+
instance Each (VectorStrict.Vector a) (VectorStrict.Vector b) a b where
204+
each = vectorTraverse
205+
{-# INLINE each #-}
206+
#endif
207+
198208
-- | @'each' :: 'Traversal' 'StrictT.Text' 'StrictT.Text' 'Char' 'Char'@
199209
instance (a ~ Char, b ~ Char) => Each StrictT.Text StrictT.Text a b where
200210
each = text

src/Control/Lens/Empty.hs

+14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ import qualified Data.Vector as Vector
5353
import qualified Data.Vector.Unboxed as Unboxed
5454
import Data.Vector.Unboxed (Unbox)
5555
import qualified Data.Vector.Storable as Storable
56+
import qualified Data.Vector.Primitive as Prim
57+
#if MIN_VERSION_vector(0,13,2)
58+
import qualified Data.Vector.Strict as VectorStrict
59+
#endif
5660
import Foreign.Storable (Storable)
5761

5862
#if !defined(mingw32_HOST_OS) && !defined(ghcjs_HOST_OS)
@@ -161,10 +165,20 @@ instance Unbox a => AsEmpty (Unboxed.Vector a) where
161165
_Empty = nearly Unboxed.empty Unboxed.null
162166
{-# INLINE _Empty #-}
163167

168+
instance Prim.Prim a => AsEmpty (Prim.Vector a) where
169+
_Empty = nearly Prim.empty Prim.null
170+
{-# INLINE _Empty #-}
171+
164172
instance Storable a => AsEmpty (Storable.Vector a) where
165173
_Empty = nearly Storable.empty Storable.null
166174
{-# INLINE _Empty #-}
167175

176+
#if MIN_VERSION_vector(0,13,2)
177+
instance AsEmpty (VectorStrict.Vector a) where
178+
_Empty = nearly VectorStrict.empty VectorStrict.null
179+
{-# INLINE _Empty #-}
180+
#endif
181+
168182
instance AsEmpty (Seq.Seq a) where
169183
_Empty = nearly Seq.empty Seq.null
170184
{-# INLINE _Empty #-}

src/Control/Lens/Internal/Iso.hs

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import Data.Vector.Primitive (Prim)
3333
import qualified Data.Vector.Storable as Storable
3434
import qualified Data.Vector.Unboxed as Unbox
3535
import Data.Vector.Unboxed (Unbox)
36+
#if MIN_VERSION_vector(0,13,2)
37+
import qualified Data.Vector.Strict as VectorStrict
38+
#endif
3639
import qualified Data.Sequence as Seq
3740
import Data.Sequence (Seq)
3841
import Foreign.Storable (Storable)
@@ -100,3 +103,8 @@ instance Unbox a => Reversing (Unbox.Vector a) where
100103

101104
instance Storable a => Reversing (Storable.Vector a) where
102105
reversing = Storable.reverse
106+
107+
#if MIN_VERSION_vector(0,13,2)
108+
instance Reversing (VectorStrict.Vector a) where
109+
reversing = VectorStrict.reverse
110+
#endif

src/Control/Lens/Wrapped.hs

+11
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ import qualified Data.Set as Set
150150
import Data.Set (Set)
151151
import Data.Tagged
152152
import qualified Data.Vector as Vector
153+
#if MIN_VERSION_vector(0,13,2)
154+
import qualified Data.Vector.Strict as VectorStrict
155+
#endif
153156
import qualified Data.Vector.Primitive as Prim
154157
import Data.Vector.Primitive (Prim)
155158
import qualified Data.Vector.Unboxed as Unboxed
@@ -676,6 +679,14 @@ instance Storable a => Wrapped (Storable.Vector a) where
676679
_Wrapped' = iso Storable.toList Storable.fromList
677680
{-# INLINE _Wrapped' #-}
678681

682+
#if MIN_VERSION_vector(0,13,2)
683+
instance (t ~ Vector.Vector a') => Rewrapped (VectorStrict.Vector a) t
684+
instance Wrapped (VectorStrict.Vector a) where
685+
type Unwrapped (VectorStrict.Vector a) = [a]
686+
_Wrapped' = iso VectorStrict.toList VectorStrict.fromList
687+
{-# INLINE _Wrapped' #-}
688+
#endif
689+
679690
-- * semigroupoids
680691

681692
instance (t ~ WrappedApplicative f' a') => Rewrapped (WrappedApplicative f a) t

0 commit comments

Comments
 (0)