class Ztimer::SortedStore
Public Class Methods
new()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 5 def initialize @store = [] end
Public Instance Methods
<<(value)
click to toggle source
# File lib/ztimer/sorted_store.rb, line 9 def <<(value) @store.insert(position_for(value), value) return self end
[](index)
click to toggle source
# File lib/ztimer/sorted_store.rb, line 23 def [](index) return @store[index] end
clear()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 70 def clear return @store.clear end
count()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 58 def count return @store.count end
delete(value)
click to toggle source
# File lib/ztimer/sorted_store.rb, line 14 def delete(value) index = index_of(value) if index @store.delete_at(index) else return nil end end
empty?()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 66 def empty? return @store.empty? end
first()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 27 def first return @store.first end
index_of(value, start = 0, stop = [@store.count - 1, 0].max)
click to toggle source
# File lib/ztimer/sorted_store.rb, line 43 def index_of(value, start = 0, stop = [@store.count - 1, 0].max) if start > stop return nil elsif start == stop return value == @store[start] ? start : nil else position = ((stop + start)/ 2).to_i case value <=> @store[position] when -1 then return index_of(value, start, position) when 0 then return position when 1 then return index_of(value, position + 1, stop) end end end
last()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 31 def last return @store.last end
pop()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 39 def pop return @store.pop end
shift()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 35 def shift return @store.shift end
size()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 62 def size return @store.size end
to_a()
click to toggle source
# File lib/ztimer/sorted_store.rb, line 74 def to_a return @store.dup end
Protected Instance Methods
position_for(item, start = 0, stop = [@store.count - 1, 0].max)
click to toggle source
# File lib/ztimer/sorted_store.rb, line 81 def position_for(item, start = 0, stop = [@store.count - 1, 0].max) if start > stop raise "Invalid range (#{start}, #{stop})" elsif start == stop element = @store[start] return element.nil? || ((item <=> element) <= 0) ? start : start + 1 else position = ((stop + start)/ 2).to_i case item <=> @store[position] when -1 then return position_for(item, start, position) when 0 then return position when 1 then return position_for(item, position + 1, stop) end end end