Created
August 1, 2012 18:29
-
-
Save henrik/3229544 to your computer and use it in GitHub Desktop.
Scheduler - cleaned up version of Evgeny Kluev's solution http://stackoverflow.com/a/11761192/6962
This file contains 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
class Scheduler | |
def initialize | |
@schedule = [] | |
@word = 0 | |
end | |
def next_slot | |
if @schedule.empty? | |
slot = 0 | |
else | |
bit = 32 | |
while (((@word ^= bit) & bit) == 0) do | |
bit >>= 1; | |
end | |
slot = (@word * 60) / 64 | |
end | |
@schedule << slot | |
@schedule.sort! | |
slot | |
end | |
def schedule | |
@schedule | |
end | |
end | |
scheduler = Scheduler.new | |
60.times do | |
scheduler.next_slot | |
p scheduler.schedule | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment