Class: Ztimer::SortedStore

Inherits:
Object
  • Object
show all
Defined in:
lib/ztimer/sorted_store.rb

Instance Method Summary collapse

Constructor Details

#initializeSortedStore

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

#clearObject



70
71
72
# File 'lib/ztimer/sorted_store.rb', line 70

def clear
  return @store.clear
end

#countObject



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

Returns:

  • (Boolean)


66
67
68
# File 'lib/ztimer/sorted_store.rb', line 66

def empty?
  return @store.empty?
end

#firstObject



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

#lastObject



31
32
33
# File 'lib/ztimer/sorted_store.rb', line 31

def last
  return @store.last
end

#popObject



39
40
41
# File 'lib/ztimer/sorted_store.rb', line 39

def pop
  return @store.pop
end

#shiftObject



35
36
37
# File 'lib/ztimer/sorted_store.rb', line 35

def shift
  return @store.shift
end

#sizeObject



62
63
64
# File 'lib/ztimer/sorted_store.rb', line 62

def size
  return @store.size
end

#to_aObject



74
75
76
# File 'lib/ztimer/sorted_store.rb', line 74

def to_a
  return @store.dup
end