"'query' must be a 'CompressedIRangesList' object"
## return as.matrix as on Hits, with indices adjusted
## count up the matches for each query in every matching
"'query' must be a RangesList of length equal to that of 'x'"
"'subject' must be a RangesList of length equal to that of 'x'"

prototype = prototype(elementType = "Hits",
rep.int(space, sapply(as.list(x, use.names = FALSE), length))
subjectOffsets <- c(0L, head(cumsum(sapply(subject, length)), -1))
newList("HitsList", list_of_hits, subjectOffsets = subjectOffsets)
stop("'query' must be a 'CompressedIRangesList' object")
partitioning <- PartitioningByEnd(hspace, names=names(query@partitioning), NG=length(names(query@partitioning)))
rows <- c(0L, head(cumsum(sapply(x, queryLength)), -1))
mat + cbind(rep.int(rows, nr), rep.int(x@subjectOffsets, nr))
setMethod("as.table", "HitsList", function(x, ...) {
as.table(array(counts, length(counts), list(range = seq_along(counts))))
x@elements <- lapply(as.list(x, use.names = FALSE), t)
setMethod("ranges", "HitsList", function(x, query, subject) {
if (!is(query, "RangesList") || length(query) != length(x))
stop("'query' must be a RangesList of length equal to that of 'x'")
if (!is(subject, "RangesList") || length(subject) != length(x))
stop("'subject' must be a RangesList of length equal to that of 'x'")
queries <- as.list(query, use.names = FALSE)
subjects <- as.list(subject, use.names = FALSE)
ans <- do.call(RangesList, lapply(seq_len(length(x)), function(i) {

### Some low-level (not exported) utility functions to operate on integer
### Returns 'sum(x)', or an error if 'x' contains NAs or negative values or if
### an integer overflow occurs while summing.
### Equivalent to (but much faster than):
"'last' must be a single, non-NA integer"
### We want this ordering to be "stable".
"'x' must be an integer vector or a factor"
## At this point 'x' is guaranteed to contain at least one non NA value.
## Uses _get_order_of_int_array() at the C level which is stable.
## At this point 'x' has NAs and we must keep them ('na.last' is not NA).
## We can't use sort.list() with method="quick" or method="shell" here
## because they are not stable algorithms (and in addition method="quick"
## is only supported when 'na.last' is NA). So we use order() with an
## extra vector to break ties, which is a trick to make it stable.
## Unfortunately this is very inefficient (about twice slower than
## using sort.list() with method="shell").
## TODO: Modify .Call entry point Integer_order to support 'na.last' arg.
### Fast ordering/comparing of integer pairs.
### For 'a' and 'b' integer vectors of equal length with no NAs,
### 'orderIntegerPairs(a, b)' is equivalent to (but faster than):
###   # Generating random pairs (representing ranges).
###   W <- 40L        # average width of the ranges
###   a <- sample(max_end - W - 2L, N, replace=TRUE)
###   b <- W + sample(-3:3, N, replace=TRUE)
###   oo <- IRanges:::orderIntegerPairs(a, b)
### For efficiency reasons, we don't support (and don't even check) for NAs.
"'a1' and 'b1' must have the same length"
"'a2' and 'b2' must have the same length"
"'nomatch' must be a single number or NA"
### 'duplicatedIntegerPairs(a, b)' is equivalent to (but much faster than):
## This is a temporary (and inefficient) workaround until "quick"
## and "hash" methods can natively support fromLast=TRUE.
## TODO: Add support for fromLast=TRUE to "quick" and "hash" methods.
### 'runEndsOfIntegerPairs(a, b)' finds the runs of identical rows in
### 'cbind(a, b)' and returns the indices of the last row in each run.
### In other words, it's equivalent to (but much faster than):
### Note that, if the rows in 'cbind(a, b)' are already sorted, then
### 'runEndsOfIntegerPairs(a, b)' returns the indices of the unique rows.
### In other words, 'runEndsOfIntegerPairs()' could be used to efficiently
### extract the unique pairs of integers from a presorted set of pairs.
### However, at the moment (April 2011) using 'duplicatedIntegerPairs()'
### is still faster than using 'runEndsOfIntegerPairs()' for finding the
### duplicated or unique pairs of integers in a presorted set of pairs.
### But this only because 'runEndsOfIntegerPairs()' is not as fast as it
### could/should be (an all-in-C implementation would probably solve this).
### TODO: What happens if 'a' and 'b' don't have the same length? Shouldn't
### Fast ordering/comparing of integer quadruplets.
### For 'a', 'b', 'c' and 'd' integer vectors of equal length with no NAs,
### 'orderIntegerQuads(a, b, c, d)' is equivalent to (but faster than):
"'a1', 'b1', 'c1' and 'd1' must have the same length"
"'a2', 'b2', 'c2' and 'd2' must have the same length"
"'a', 'b', 'c' and 'd' must have the same length"
### An enhanced version of base::tabulate() that: (1) handles integer weights
### (NA and negative weights are OK), and (2) throws an error if 'strict' is
### TRUE and if 'x' contains NAs or values not in the [1, 'nbins'] interval.
### Unlike with base::tabulate(), 'nbins' needs to be specified (no default
### value). Also for now, it only works if 'x' is an integer vector.
### The bitwise operations in this section don't treat the integer NA (aka
### NA_integer_) in any particular way: at the C level an NA_integer_ is
### just a 32-bit pattern like any other int in C.
"'nbit' must be a single non-negative integer"
### Returns an integer matrix with 'length(x)' rows and 'length(bitpos)' cols.
## Old implementation: not very efficient and also broken on NAs and
#    stop("'bitpos' must contain potive values only")
## In some circumstances (e.g. if 'tx' has 0 col), the "dim" attribute
## gets lost during the above multiplication.
#                  tmp <- seq_len(lengths[i]) + offset[i]
### Reverse an injection from 1:M to 1:N.
### The injection is represented by an integer vector of length M (eventually
### Can be used to efficiently reverse the result of a call to 'order()'.