module Data.Sparse.Utils where
maxTup, minTup :: Ord t => (t, t) -> (t, t) -> (t, t)
maxTup (x1,y1) (x2,y2) = (max x1 x2, max y1 y2)
minTup (x1,y1) (x2,y2) = (min x1 x2, min y1 y2)
denseIxArray :: [b] -> [(Int, b)]
denseIxArray xs = zip [0..length xs1] xs
denseIxArray2 :: Int -> [c] -> [(Int, Int, c)]
denseIxArray2 m xs = zip3 (concat $ replicate n ii_) jj_ xs where
ii_ = [0 .. m1]
jj_ = concatMap (replicate m) [0 .. n1]
ln = length xs
n = ln `div` m
foldrMap :: (Foldable t, Functor t) => (a -> b) -> (b -> c -> c) -> c -> t a -> c
foldrMap ff gg x0 = foldr gg x0 . fmap ff
foldlStrict :: (a -> b -> a) -> a -> [b] -> a
foldlStrict f = go
where
go z [] = z
go z (x:xs) = let z' = f z x in z' `seq` go z' xs
ifoldr :: Num i =>
(a -> b -> b) -> b -> (i -> c -> d -> a) -> c -> [d] -> b
ifoldr mjoin mneutral f = go 0 where
go i z (x:xs) = mjoin (f i z x) (go (i+1) z xs)
go _ _ [] = mneutral
type LB = Int
type UB = Int
inBounds :: LB -> UB -> Int -> Bool
inBounds ibl ibu i = i>= ibl && i<ibu
inBounds2 :: (LB, UB) -> (Int, Int) -> Bool
inBounds2 (ibl,ibu) (ix,iy) = inBounds ibl ibu ix && inBounds ibl ibu iy
inBounds0 :: UB -> Int -> Bool
inBounds0 = inBounds 0
inBounds02 :: (UB, UB) -> (Int, Int) -> Bool
inBounds02 (bx,by) (i,j) = inBounds0 bx i && inBounds0 by j