module Data.TrieMap.RadixTrie.Slice where
import Control.Exception (assert)
import Data.Vector.Generic
import Prelude hiding (length, zip, foldr)
splitSlice :: Vector v a => Int -> v a -> (v a, a, v a)
splitSlice !i !slice = (takeSlice i slice, slice !$ i, dropSlice (i+1) slice)
takeSlice :: Vector v a => Int -> v a -> v a
takeSlice !n xs = assert (n <= length xs) $ unsafeTake n xs
dropSlice :: Vector v a => Int -> v a -> v a
dropSlice !n xs = assert (n <= length xs) $ unsafeDrop n xs
unDropSlice :: Vector v a => Int -> v a -> v a
unDropSlice !n = unsafeDrop (n)
matchSlice :: (Vector v a, Vector v b) => (a -> b -> z -> z) -> (Int -> Int -> z) -> v a -> v b -> z
matchSlice f = iMatchSlice (const f)
iMatchSlice :: (Vector v a, Vector v b) => (Int -> a -> b -> z -> z) -> (Int -> Int -> z) -> v a -> v b -> z
iMatchSlice f z !xs !ys = matcher 0 where
!xLen = length xs
!yLen = length ys
!len = min xLen yLen
matcher i
| i < len = f i (xs !$ i) (ys !$ i) (matcher (i+1))
| otherwise = z xLen yLen
(!$) :: Vector v a => v a -> Int -> a
xs !$ j = assert (j >= 0 && j < length xs) $ unsafeIndex xs j