Class: Packet::TimerStore
- Inherits:
-
Object
- Object
- Packet::TimerStore
- Defined in:
- lib/packet/timer_store.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
Returns the value of attribute order.
Instance Method Summary collapse
- #bin_search_for_key(lower_index, upper_index, key) ⇒ Object
- #delete(timer) ⇒ Object
- #each ⇒ Object
-
#initialize ⇒ TimerStore
constructor
A new instance of TimerStore.
- #store(timer) ⇒ Object
Constructor Details
#initialize ⇒ TimerStore
Returns a new instance of TimerStore.
12 13 14 15 |
# File 'lib/packet/timer_store.rb', line 12 def initialize @order = [] @container = { } end |
Instance Attribute Details
#order ⇒ Object
Returns the value of attribute order.
11 12 13 |
# File 'lib/packet/timer_store.rb', line 11 def order @order end |
Instance Method Details
#bin_search_for_key(lower_index, upper_index, key) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/packet/timer_store.rb', line 40 def bin_search_for_key(lower_index,upper_index,key) return upper_index if(upper_index - lower_index <= 1) pivot = (lower_index + upper_index)/2 if @order[pivot] == key return pivot elsif @order[pivot] < key bin_search_for_key(pivot,upper_index,key) else bin_search_for_key(lower_index,pivot,key) end end |
#delete(timer) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/packet/timer_store.rb', line 62 def delete(timer) int_time = timer.scheduled_time.to_i @container[int_time] && @container[int_time].delete(timer) if(!@container[int_time] || @container[int_time].empty?) @order.delete(timer) end end |
#each ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/packet/timer_store.rb', line 52 def each @order.each_with_index do |x,i| @container[x].each do |timer| yield timer end # @container.delete(x) if @container[x].empty? # @order.delete_at(i) end end |
#store(timer) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/packet/timer_store.rb', line 17 def store(timer) int_time = timer.scheduled_time.to_i @container[int_time] ||= [] @container[int_time] << timer if @container.empty? or @order.empty? @order << int_time return end if @order.last <= int_time @order << int_time else index = bin_search_for_key(0,@order.length - 1,int_time) if(int_time < @order[index-1] && index != 0) @order.insert(index-1,int_time) else @order.insert(index,int_time) end end end |