11{-# LANGUAGE ConstraintKinds #-}
2- {-# LANGUAGE ExplicitNamespaces #-}
32{-# LANGUAGE GADTs #-}
43{-# LANGUAGE InstanceSigs #-}
54{-# LANGUAGE OverloadedStrings #-}
@@ -30,7 +29,6 @@ import qualified Data.Vector.Unboxed as VU
3029import qualified Data.Vector.Unboxed.Mutable as VUM
3130
3231import Control.Monad.ST (runST )
33- import DataFrame.Internal.Function
3432import DataFrame.Internal.Types
3533import DataFrame.Internal.Parsing
3634import Data.Int
@@ -221,44 +219,38 @@ type UnboxIf a = When (Unboxable a) (VU.Unbox a)
221219
222220-- | Generic column transformation (no index).
223221transform
224- :: forall b c . -- element types
222+ :: forall b c .
225223 ( Columnable b
226224 , Columnable c
227- , UnboxIf c -- only required when ‘c’ is unboxable
225+ , UnboxIf c
228226 , Typeable b
229227 , Typeable c )
230228 => (b -> c )
231229 -> Column
232230 -> Maybe Column
233231transform f = \ case
234-
235232 BoxedColumn (col :: VB. Vector a )
236233 | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
237234 -> Just (toColumn' @ c (VB. map f col))
238235 | otherwise -> Nothing
239-
240236 OptionalColumn (col :: VB. Vector a )
241237 | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
242238 -> Just (toColumn' @ c (VB. map f col))
243239 | otherwise -> Nothing
244-
245240 UnboxedColumn (col :: VU. Vector a )
246241 | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
247242 -> Just $ case sUnbox @ c of
248- STrue -> UnboxedColumn (VU. map f col) -- needs VU.Unbox c
243+ STrue -> UnboxedColumn (VU. map f col)
249244 SFalse -> toColumn' @ c (VB. map f (VB. convert col))
250245 | otherwise -> Nothing
251-
252246 GroupedBoxedColumn (col :: VB. Vector (VB. Vector a ))
253247 | Just Refl <- testEquality (typeRep @ (VB. Vector a )) (typeRep @ b )
254248 -> Just (toColumn' @ c (VB. map f col))
255249 | otherwise -> Nothing
256-
257250 GroupedUnboxedColumn (col :: VB. Vector (VU. Vector a ))
258251 | Just Refl <- testEquality (typeRep @ (VU. Vector a )) (typeRep @ b )
259252 -> Just (toColumn' @ c (VB. map f col))
260253 | otherwise -> Nothing
261-
262254 GroupedOptionalColumn (col :: VB. Vector (VB. Vector a ))
263255 | Just Refl <- testEquality (typeRep @ (VB. Vector a )) (typeRep @ b )
264256 -> Just (toColumn' @ c (VB. map f col))
@@ -365,79 +357,34 @@ sortedIndexes asc (GroupedOptionalColumn column) = runST $ do
365357 return $ VU. generate (VG. length column) (\ i -> fst (sorted VG. ! i))
366358{-# INLINE sortedIndexes #-}
367359
368- -- Operations on a column that may change its type.
369-
370- -- instance Transformable Column where
371- -- transform :: forall b c . (Columnable b, ColumnifyRep (KindOf b) b, Columnable c, ColumnifyRep (KindOf c) c) => (b -> c) -> Column -> Maybe Column
372- -- transform f (BoxedColumn (column :: VB.Vector a)) = do
373- -- Refl <- testEquality (typeRep @a) (typeRep @b)
374- -- return (toColumn' @c (VB.map f column))
375- -- transform f (OptionalColumn (column :: VB.Vector a)) = do
376- -- Refl <- testEquality (typeRep @a) (typeRep @b)
377- -- return (toColumn' @c (VB.map f column))
378- -- transform f (UnboxedColumn (column :: VU.Vector a)) = do
379- -- Refl <- testEquality (typeRep @a) (typeRep @b)
380- -- return $ if testUnboxable (typeRep @c) then transformUnboxed f column else toColumn' (VB.map f (VB.convert column))
381- -- transform f (GroupedBoxedColumn (column :: VB.Vector (VB.Vector a))) = do
382- -- Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
383- -- return (toColumn' @c (VB.map f column))
384- -- transform f (GroupedUnboxedColumn (column :: VB.Vector (VU.Vector a))) = do
385- -- Refl <- testEquality (typeRep @(VU.Vector a)) (typeRep @b)
386- -- return (toColumn' @c (VB.map f column))
387- -- transform f (GroupedOptionalColumn (column :: VB.Vector (VB.Vector a))) = do
388- -- Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
389- -- return (toColumn' @c (VB.map f column))
390-
391360-- | Applies a function that returns an unboxed result to an unboxed vector, storing the result in a column.
392- transformUnboxed :: forall a b . (Columnable a , ColumnifyRep (KindOf a ) a , VU. Unbox a , Columnable b , ColumnifyRep (KindOf b ) b ) => (a -> b ) -> VU. Vector a -> Column
393- transformUnboxed f = itransformUnboxed (const f)
394-
395- -- TODO: Make a type class with incoherent instances.
396- itransformUnboxed :: forall a b . (Columnable a , ColumnifyRep (KindOf a ) a , VU. Unbox a , Columnable b , ColumnifyRep (KindOf b ) b ) => (Int -> a -> b ) -> VU. Vector a -> Column
397- itransformUnboxed f column = case testEquality (typeRep @ b ) (typeRep @ Int ) of
398- Just Refl -> UnboxedColumn $ VU. imap f column
399- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Int8 ) of
400- Just Refl -> UnboxedColumn $ VU. imap f column
401- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Int16 ) of
402- Just Refl -> UnboxedColumn $ VU. imap f column
403- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Int32 ) of
404- Just Refl -> UnboxedColumn $ VU. imap f column
405- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Int64 ) of
406- Just Refl -> UnboxedColumn $ VU. imap f column
407- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Word8 ) of
408- Just Refl -> UnboxedColumn $ VU. imap f column
409- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Word16 ) of
410- Just Refl -> UnboxedColumn $ VU. imap f column
411- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Word32 ) of
412- Just Refl -> UnboxedColumn $ VU. imap f column
413- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Word64 ) of
414- Just Refl -> UnboxedColumn $ VU. imap f column
415- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Char ) of
416- Just Refl -> UnboxedColumn $ VU. imap f column
417- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Bool ) of
418- Just Refl -> UnboxedColumn $ VU. imap f column
419- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Float ) of
420- Just Refl -> UnboxedColumn $ VU. imap f column
421- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Double ) of
422- Just Refl -> UnboxedColumn $ VU. imap f column
423- Nothing -> case testEquality (typeRep @ b ) (typeRep @ Word ) of
424- Just Refl -> UnboxedColumn $ VU. imap f column
425- Nothing -> error " Result type is unboxed" -- since we only call this after confirming
426-
427- -- | transform with index.
428- itransform :: forall b c . (Columnable b , ColumnifyRep (KindOf b ) b , Columnable b , Columnable c , ColumnifyRep (KindOf c ) c ) => (Int -> b -> c ) -> Column -> Maybe Column
429- itransform f (BoxedColumn (column :: VB. Vector a )) = do
430- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
431- return (toColumn' (VB. imap f column))
432- itransform f (UnboxedColumn (column :: VU. Vector a )) = do
433- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
434- return $ if testUnboxable (typeRep @ c ) then itransformUnboxed f column else toColumn' (VB. imap f (VB. convert column))
435- itransform f (GroupedBoxedColumn (column :: VB. Vector (VB. Vector a ))) = do
436- Refl <- testEquality (typeRep @ (VB. Vector a )) (typeRep @ b )
437- return (toColumn' (VB. imap f column))
438- itransform f (GroupedUnboxedColumn (column :: VB. Vector (VU. Vector a ))) = do
439- Refl <- testEquality (typeRep @ (VU. Vector a )) (typeRep @ b )
440- return (toColumn' (VB. imap f column))
361+ itransform
362+ :: forall b c . (Typeable b , Typeable c , Columnable b , Columnable c )
363+ => (Int -> b -> c ) -> Column -> Maybe Column
364+ itransform f = \ case
365+ BoxedColumn (col :: VB. Vector a )
366+ | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
367+ -> Just (toColumn' @ c (VB. imap f col))
368+ | otherwise -> Nothing
369+ UnboxedColumn (col :: VU. Vector a )
370+ | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
371+ -> Just $
372+ case sUnbox @ c of
373+ STrue -> UnboxedColumn (VU. imap f col)
374+ SFalse -> toColumn' @ c (VB. imap f (VB. convert col))
375+ | otherwise -> Nothing
376+ OptionalColumn (col :: VB. Vector a )
377+ | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
378+ -> Just (toColumn' @ c (VB. imap f col))
379+ | otherwise -> Nothing
380+ GroupedBoxedColumn (col :: VB. Vector a )
381+ | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
382+ -> Just (toColumn' @ c (VB. imap f col))
383+ | otherwise -> Nothing
384+ GroupedUnboxedColumn (col :: VB. Vector a )
385+ | Just Refl <- testEquality (typeRep @ a ) (typeRep @ b )
386+ -> Just (toColumn' @ c (VB. imap f col))
387+ | otherwise -> Nothing
441388
442389-- | Filter column with index.
443390ifilterColumn :: forall a . (Columnable a ) => (Int -> a -> Bool ) -> Column -> Maybe Column
@@ -454,23 +401,6 @@ ifilterColumn f c@(GroupedUnboxedColumn (column :: VB.Vector b)) = do
454401 Refl <- testEquality (typeRep @ a ) (typeRep @ b )
455402 return $ GroupedUnboxedColumn $ VG. ifilter f column
456403
457- -- TODO: Expand this to use more predicates.
458- ifilterColumnF :: Function -> Column -> Maybe Column
459- ifilterColumnF (ICond (f :: Int -> a -> Bool )) c@ (BoxedColumn (column :: VB. Vector b )) = do
460- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
461- return $ BoxedColumn $ VG. ifilter f column
462- ifilterColumnF (ICond (f :: Int -> a -> Bool )) c@ (UnboxedColumn (column :: VU. Vector b )) = do
463- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
464- return $ UnboxedColumn $ VG. ifilter f column
465- ifilterColumnF (ICond (f :: Int -> a -> Bool )) c@ (OptionalColumn (column :: VB. Vector b )) = do
466- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
467- return $ OptionalColumn $ VG. ifilter f column
468- ifilterColumnF (ICond (f :: Int -> a -> Bool )) c@ (GroupedBoxedColumn (column :: VB. Vector b )) = do
469- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
470- return $ GroupedBoxedColumn $ VG. ifilter f column
471- ifilterColumnF (ICond (f :: Int -> a -> Bool )) c@ (GroupedUnboxedColumn (column :: VB. Vector b )) = do
472- Refl <- testEquality (typeRep @ a ) (typeRep @ b )
473- return $ GroupedUnboxedColumn $ VG. ifilter f column
474404
475405ifoldrColumn :: forall a b . (Columnable a , Columnable b ) => (Int -> a -> b -> b ) -> b -> Column -> Maybe b
476406ifoldrColumn f acc c@ (BoxedColumn (column :: VB. Vector d )) = do
@@ -540,23 +470,16 @@ zipColumns (UnboxedColumn column) (BoxedColumn other) = BoxedColumn (VB.generate
540470zipColumns (UnboxedColumn column) (UnboxedColumn other) = UnboxedColumn (VG. zip column other)
541471{-# INLINE zipColumns #-}
542472
543- zipWithColumns :: forall a b c . (Columnable a , ColumnifyRep (KindOf a ) a , Columnable b , ColumnifyRep (KindOf b ) b , Columnable c , ColumnifyRep (KindOf c ) c ) => (a -> b -> c ) -> Column -> Column -> Column
544- zipWithColumns f (BoxedColumn (column :: VB. Vector d )) (BoxedColumn (other :: VB. Vector e )) = case testEquality (typeRep @ a ) (typeRep @ d ) of
545- Just Refl -> case testEquality (typeRep @ b ) (typeRep @ e ) of
546- Just Refl -> toColumn' (VG. zipWith f column other)
547- Nothing -> throw $ TypeMismatchException' (typeRep @ b ) (show $ typeRep @ e ) " " " zipWithColumns"
548- Nothing -> throw $ TypeMismatchException' (typeRep @ a ) (show $ typeRep @ d ) " " " zipWithColumns"
549- zipWithColumns f (BoxedColumn (column :: VB. Vector d )) (UnboxedColumn (other :: VU. Vector e )) = case testEquality (typeRep @ a ) (typeRep @ d ) of
550- Just Refl -> case testEquality (typeRep @ b ) (typeRep @ e ) of
551- Just Refl -> toColumn' (VG. zipWith f column (VB. convert other))
552- Nothing -> throw $ TypeMismatchException' (typeRep @ b ) (show $ typeRep @ e ) " " " zipWithColumns"
553- Nothing -> throw $ TypeMismatchException' (typeRep @ a ) (show $ typeRep @ d ) " " " zipWithColumns"
554- zipWithColumns f left@ (UnboxedColumn (column :: VU. Vector d )) right@ (BoxedColumn (other :: VB. Vector e )) = zipWithColumns f right left
473+ zipWithColumns :: forall a b c . (Columnable a , Columnable b , Columnable c ) => (a -> b -> c ) -> Column -> Column -> Column
555474zipWithColumns f (UnboxedColumn (column :: VU. Vector d )) (UnboxedColumn (other :: VU. Vector e )) = case testEquality (typeRep @ a ) (typeRep @ d ) of
556475 Just Refl -> case testEquality (typeRep @ b ) (typeRep @ e ) of
557476 Just Refl -> toColumn' $ VB. zipWith f (VG. convert column) (VG. convert other)
558477 Nothing -> throw $ TypeMismatchException' (typeRep @ b ) (show $ typeRep @ e ) " " " zipWithColumns"
559478 Nothing -> throw $ TypeMismatchException' (typeRep @ a ) (show $ typeRep @ d ) " " " zipWithColumns"
479+ zipWithColumns f left right = let
480+ left' = toVector @ a left
481+ right' = toVector @ b right
482+ in toColumn' $ VB. zipWith f left' right'
560483{-# INLINE zipWithColumns #-}
561484
562485-- Functions for mutable columns (intended for IO).
0 commit comments