Commits

Aleksey Khudyakov committed decaf49

Add indexing functions

Comments (0)

Files changed (2)

Data/Vector/Fixed.hs

     -- ** Transformations
   , head
   , tail
+  , index
     -- ** Comparison
   , eq
     -- ** Maps
 {-# INLINE tail #-}
 tail = C.vector . C.tail . C.cvec
 
+index :: (Vector v a) => v a -> Int -> a
+{-# INLINE index #-}
+index v n = C.runContVec (C.index n)
+          $ C.cvec v
+
 -- | Left fold over vector
 foldl :: Vector v a => (b -> a -> b) -> b -> v a -> b
 {-# INLINE foldl #-}

Data/Vector/Fixed/Cont.hs

   , runContVec
     -- ** Getters
   , head
+  , index
     -- ** Vector construction
   , vector
   , vectorM
 
 data T_head a n = T_head (Maybe a)
 
+-- | /O(n)/ Get value at specified index.
+index :: forall n a. Arity n => Int -> Fun n a a
+index n
+  | n < 0     = error "Data.Vector.Fixed.Cont.index: index out of range"
+  | otherwise = Fun $ accum
+     (\(T_Index x) a -> T_Index $ case x of
+                          Left  0 -> Right a
+                          Left  i -> Left (i - 1)
+                          r       -> r
+     )
+     (\(T_Index x) -> case x of
+                        Left  _ -> error "Data.Vector.Fixed.index: index out of range"
+                        Right a -> a
+     )
+     ( T_Index (Left n) :: T_Index a n)
+
+newtype T_Index a n = T_Index (Either Int a)
+
 
 -- | Left fold over continuation vector.
 foldl :: forall n a b. Arity n