wrap static method

int wrap(
  1. int index,
  2. int max
)

Computes an index which wraps around a given maximum value. For values >= 0, this is equals to val % max. For values < 0, this is equal to max - (-val) % max

@param index the value to wrap @param max the maximum value (or modulus) @return the wrapped index

Implementation

static int wrap(int index, int max) {
  if (index < 0) {
    return max - ((-index) % max);
  }
  return index % max;
}