public
Created

Scheduler - cleaned up version of Evgeny Kluev's solution http://stackoverflow.com/a/11761192/6962

  • Download Gist
scheduler.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.