Class: APNS::InfiniteArray
- Inherits:
-
Object
- Object
- APNS::InfiniteArray
- Defined in:
- lib/apns/infinite_array.rb
Instance Method Summary collapse
- #buffer_index_from(index:) ⇒ Object
- #clear ⇒ Object
- #delete_where_index_less_than(index) ⇒ Object
-
#initialize(buffer_size:) ⇒ InfiniteArray
constructor
A new instance of InfiniteArray.
- #item_at(index) ⇒ Object
- #items_from(index) ⇒ Object
- #pop_front ⇒ Object
- #push(item) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(buffer_size:) ⇒ InfiniteArray
Returns a new instance of InfiniteArray.
13 14 15 16 |
# File 'lib/apns/infinite_array.rb', line 13 def initialize(buffer_size:) @buffer_size = buffer_size @buf = [] end |
Instance Method Details
#buffer_index_from(index:) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/apns/infinite_array.rb', line 55 def buffer_index_from(index:) return if index > @buf.last.index buf_index = index - @buf[0].index return if buf_index < 0 buf_index end |
#clear ⇒ Object
50 51 52 53 |
# File 'lib/apns/infinite_array.rb', line 50 def clear @buf = [] @hash = {} end |
#delete_where_index_less_than(index) ⇒ Object
44 45 46 47 48 |
# File 'lib/apns/infinite_array.rb', line 44 def delete_where_index_less_than index while @buf[0].index < index pop_front end end |
#item_at(index) ⇒ Object
28 29 30 31 32 |
# File 'lib/apns/infinite_array.rb', line 28 def item_at index buf_index = buffer_index_from(index: index) return unless buf_index @buf[buf_index].item end |
#items_from(index) ⇒ Object
34 35 36 37 38 |
# File 'lib/apns/infinite_array.rb', line 34 def items_from index buf_index = buffer_index_from(index: index) return [] unless buf_index @buf[buf_index..-1].map(&:item) end |
#pop_front ⇒ Object
40 41 42 |
# File 'lib/apns/infinite_array.rb', line 40 def pop_front @buf = @buf[1..-1] end |
#push(item) ⇒ Object
18 19 20 21 |
# File 'lib/apns/infinite_array.rb', line 18 def push item @buf << Element.new(index: size, item: item) pop_front if @buf.size > @buffer_size end |
#size ⇒ Object
23 24 25 26 |
# File 'lib/apns/infinite_array.rb', line 23 def size return 0 if @buf[0].nil? @buf.last.index + 1 end |