File: FindIndexR.hs

package info (click to toggle)
haskell-vector 0.13.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 996 kB
  • sloc: haskell: 11,004; ansic: 6; makefile: 3
file content (24 lines) | stat: -rw-r--r-- 854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Algo.FindIndexR (findIndexR, findIndexR_naive, findIndexR_manual)
where

import Data.Vector.Unboxed (Vector)
import qualified Data.Vector.Generic as V

findIndexR :: (Double -> Bool, Vector Double) -> Maybe Int
{-# NOINLINE findIndexR #-}
findIndexR = uncurry V.findIndexR

findIndexR_naive :: (Double -> Bool, Vector Double) -> Maybe Int
{-# NOINLINE findIndexR_naive #-}
findIndexR_naive (pred, v) = fmap (V.length v - 1 -)
    $ V.foldl (\a x -> if pred x
                        then Just 1
                        else succ<$>a) Nothing v

findIndexR_manual :: (Double -> Bool, Vector Double) -> Maybe Int
{-# NOINLINE findIndexR_manual #-}
findIndexR_manual (pred, v) = go $ V.length v - 1
 where go i | i < 0                     = Nothing
            | pred (V.unsafeIndex v i)  = Just i
            | otherwise                 = go $ i-1