Created
July 18, 2013 19:50
-
-
Save stephanh42/6032461 to your computer and use it in GitHub Desktop.
A collection of small utility functions to scratch some common itches.
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
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