Skip to content

Instantly share code, notes, and snippets.

@stephanh42
Created July 18, 2013 19:50
Show Gist options
  • Save stephanh42/6032461 to your computer and use it in GitHub Desktop.
Save stephanh42/6032461 to your computer and use it in GitHub Desktop.
A collection of small utility functions to scratch some common itches.
package libscratch
object `package` {
def floorDiv(p : Int, q : Int) : Int =
if (p < 0) {
if (q < 0) {
p / q
} else {
(p - q + 1) / q
}
} else {
if (q < 0) {
(p - q - 1) / q
} else {
p / q
}
}
def ceilDiv(p : Int, q : Int) : Int =
if (p < 0) {
if (q < 0) {
(p + q + 1) / q
} else {
p / q
}
} else {
if (q < 0) {
p / q
} else {
(p + q - 1) / q
}
}
def tiledRange(lo : Int, hi : Int, tileSize : Int) : Range =
floorDiv(lo, tileSize) until ceilDiv(hi, tileSize)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment