Class: Ztimer::SortedStore
- Inherits:
-
Object
- Object
- Ztimer::SortedStore
- Defined in:
- lib/ztimer/sorted_store.rb
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #[](index) ⇒ Object
- #clear ⇒ Object
- #count ⇒ Object
- #delete(value) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
- #index_of(value, start = 0, stop = [@store.count - 1, 0].max) ⇒ Object
-
#initialize ⇒ SortedStore
constructor
A new instance of SortedStore.
- #last ⇒ Object
- #pop ⇒ Object
- #shift ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ SortedStore
Returns a new instance of SortedStore.
5 6 7 |
# File 'lib/ztimer/sorted_store.rb', line 5 def initialize @store = [] end |
Instance Method Details
#<<(value) ⇒ Object
9 10 11 12 |
# File 'lib/ztimer/sorted_store.rb', line 9 def <<(value) @store.insert(position_for(value), value) return self end |
#[](index) ⇒ Object
23 24 25 |
# File 'lib/ztimer/sorted_store.rb', line 23 def [](index) return @store[index] end |
#clear ⇒ Object
70 71 72 |
# File 'lib/ztimer/sorted_store.rb', line 70 def clear return @store.clear end |
#count ⇒ Object
58 59 60 |
# File 'lib/ztimer/sorted_store.rb', line 58 def count return @store.count end |
#delete(value) ⇒ Object
14 15 16 17 18 19 20 21 |
# 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? ⇒ Boolean
66 67 68 |
# File 'lib/ztimer/sorted_store.rb', line 66 def empty? return @store.empty? end |
#first ⇒ Object
27 28 29 |
# 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) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 ⇒ Object
31 32 33 |
# File 'lib/ztimer/sorted_store.rb', line 31 def last return @store.last end |
#pop ⇒ Object
39 40 41 |
# File 'lib/ztimer/sorted_store.rb', line 39 def pop return @store.pop end |
#shift ⇒ Object
35 36 37 |
# File 'lib/ztimer/sorted_store.rb', line 35 def shift return @store.shift end |
#size ⇒ Object
62 63 64 |
# File 'lib/ztimer/sorted_store.rb', line 62 def size return @store.size end |
#to_a ⇒ Object
74 75 76 |
# File 'lib/ztimer/sorted_store.rb', line 74 def to_a return @store.dup end |