Created
November 4, 2015 10:39
-
-
Save PeteHaitch/ba3accc8db0c453ede37 to your computer and use it in GitHub Desktop.
Check whether a list of atomic vectors is sorted (see https://github.com/PeteHaitch/S4Vectors/issues/1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pseudocode | |
isUnsortedListOfAtomics <- function(..., na.rm = FALSE, strictly = FALSE) { | |
args <- list(...) | |
m <- length(args) | |
n <- length(args[[1]]) | |
for (i in (seq_along(args[[1]]) - 1)) { | |
for (j in seq_len(m)) { | |
# All vectors except the last are checked in the same way | |
if (j < m) { | |
if (args[[j]][i] > args[[j]][i + 1]) { | |
return(TRUE) | |
} else if (args[[j]][i] == args[[j]][i + 1]) { | |
# Need to check next i and (i+1) elements of next vector | |
increment_j | |
} else { | |
# No need to check remaining vectors, so increment i and start back at the first vector | |
increment_i_and_reset_j | |
} | |
} else { | |
# The last vector needs to be checked differently depending the value of 'strictly' | |
# Probably want to move this conditional outside the main loop so that it isn't repeatedly evaluated? | |
if (strictly) { | |
if (args[[j]][i] >= args[[j]][i + 1]) { | |
return(TRUE) | |
} else { | |
# No more vectors to check, so increment i and start back at the first vector | |
increment_i_and_reset_j | |
} | |
} else { | |
if (args[[j]][i] > args[[j]][i + 1]) { | |
return(TRUE) | |
} else { | |
# No more vectors to check, so increment i and start back at the first vector | |
increment_i_and_reset_j | |
} | |
} | |
} | |
} | |
} | |
return(FALSE) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment