Mercurial > repos > prog > lcmsmatching
diff search.R @ 6:f86fec07f392 draft default tip
planemo upload commit c397cd8a93953798d733fd62653f7098caac30ce
author | prog |
---|---|
date | Fri, 22 Feb 2019 16:04:22 -0500 |
parents | fb9c0409d85c |
children |
line wrap: on
line diff
--- a/search.R Wed Apr 19 10:00:05 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -if ( ! exists('binary.search')) { # Do not load again if already loaded - - # Run a binary search on a sorted array. - # val The value to search. - # tab The array of values, sorted in ascending order. - # lower If set to NA, then search for the first value found by the binary search. If set to TRUE, find the value with the lowest index in the array. If set to FALSE, find the value with the highest index in the array. - # first The index of the array from which to start (1 by default). - # last The index of the array where to stop searching (end of the array by default). - # Returns the index of the found value, or NA. - binary.search <- function(val, tab, lower = NA, first = 1L, last = length(tab)) - { - # Check array & value - if (is.null(tab)) - stop('Argument "tab" is NULL.') - if (is.null(val)) - stop('Argument "val" is NULL.') - - # Wrong arguments - if (is.na(val) || last < first || length(tab) == 0) - return(NA_integer_) - - # Find value - l <- first - h <- last - while (h >= l) { - - # Take middle point - m <- (h + l) %/% 2 - # Found value - if (tab[m] == val) { - if (is.na(lower)) - return(m) - if (lower && m > first) { - for (i in (m-1):first) - if (tab[i] != val) - return(i+1) - } - else if ( ! lower && m < last) - for (i in (m+1):last) - if (tab[i] != val) - return(i-1) - return(m) - } - - # Decrease higher bound - else if (tab[m] > val) h <- m - 1 - - # Increase lower bound - else l <- m + 1 - } - - # Value not found - if ( ! is.na(lower)) { - # Look for lower or higher bound - if (lower) - return(if (h < first) NA_integer_ else h) - else - return(if (l > last) NA_integer_ else l) - } - - return(NA_integer_) - } - -} # end of load safe guard