Class: RRList::List
- Inherits:
-
Object
- Object
- RRList::List
- Defined in:
- lib/rrlist/list.rb
Overview
Represents a Round Robin List. You set the max number of items and the size does not increse. While you add more items you lose the older items
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Adds a value to the end of the list.
-
#[](index) ⇒ Object
Returns the value for the given index.
-
#[]=(index, value) ⇒ Object
Sets a value to in the given index.
-
#add(value) ⇒ Object
Adds a value to the next index position and moves the cursor forward.
-
#add_at(index, value) ⇒ Object
Add an item to the specified postion and set the cursor to that position.
-
#clear ⇒ Object
Sets all values to nil, but it does not move the cursor.
-
#each(&blk) ⇒ Object
Iterates of over the values.
-
#each_with_index ⇒ Object
Iterates of over the values.
-
#get(index) ⇒ Object
The value for the specified index.
-
#higher?(index) ⇒ True
If the giving number is higher that the current max index.
-
#in_limits?(index) ⇒ True
If the given number is in the limits of the current max an min index of the list.
-
#index_size ⇒ Integer
The index size of this list.
-
#initialize(options = {}) {|index, old_value, new_value| ... } ⇒ List
constructor
Creates an object.
-
#lower?(index) ⇒ True
If the giving number is lower that the current min index.
-
#max_index ⇒ Integer
The max indes of current list.
-
#min_index ⇒ Integer
The min index of the current list.
-
#out_of_range?(index) ⇒ True
If the given number is out of the range and all numbers will be lost if set.
-
#ranges ⇒ Array<Integer>
The min_index and max_index as a list.
-
#remaining_in_slot ⇒ Integer
If range is used, returns the remaining numbers in the current position.
-
#reset ⇒ Object
Reset this list, set all values to nil and move cursor to position 0.
-
#set_at(index, value) ⇒ Object
Set the specified value in the given index.
-
#to_s ⇒ Object
Pretty print this object.
-
#values ⇒ Array<Object>
The values of this list.
Constructor Details
#initialize(options = {}) {|index, old_value, new_value| ... } ⇒ List
Creates an object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rrlist/list.rb', line 16 def initialize(={},&function_proc) raise ":size is required" unless [:size] raise ":range should be greater thant 0" if [:range] && [:range] < 1 @store = [:store] || RRList::Stores::InMemoryArray.new @size = [:size].to_i @range = [:range] || 1 reset @function_proc = function_proc if block_given? end |
Instance Method Details
#<<(value) ⇒ Object
Adds a value to the end of the list.
192 193 194 |
# File 'lib/rrlist/list.rb', line 192 def << value add(value) end |
#[](index) ⇒ Object
Returns the value for the given index
180 181 182 |
# File 'lib/rrlist/list.rb', line 180 def [](index) get(index) end |
#[]=(index, value) ⇒ Object
Sets a value to in the given index.
186 187 188 |
# File 'lib/rrlist/list.rb', line 186 def []=(index,value) add_at(index,value) end |
#add(value) ⇒ Object
Adds a value to the next index position and moves the cursor forward
98 99 100 101 |
# File 'lib/rrlist/list.rb', line 98 def add(value) @add_at_next = 0 unless @add_at_next add_at @add_at_next, value end |
#add_at(index, value) ⇒ Object
Add an item to the specified postion and set the cursor to that position
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rrlist/list.rb', line 106 def add_at(index,value) raise "Index is lower that current index" if index < max_index @add_at_next = index + 1 pos = position_for_index(index) if out_of_range?(index) @store.fill_with_nils elsif higher?(index) (pos-1).downto(@position) { |i| @store.put(i,nil) } if @current_index >= @size @store.put(pos,nil) # We clean the value, if not it gets used by @before_add end set_value(pos,value) @position = pos + 1 @current_index = index self end |
#clear ⇒ Object
Sets all values to nil, but it does not move the cursor
30 31 32 |
# File 'lib/rrlist/list.rb', line 30 def clear @store.fill(nil,0...@size) end |
#each(&blk) ⇒ Object
Iterates of over the values. Expects a block and passes the value
77 78 79 |
# File 'lib/rrlist/list.rb', line 77 def each(&blk) values.each(&blk) end |
#each_with_index ⇒ Object
Iterates of over the values. Expects a block and passes the value and index
82 83 84 85 86 87 |
# File 'lib/rrlist/list.rb', line 82 def each_with_index ordered_values = values ordered_values.each_with_index do |v,i| yield ordered_values[i], min_index + (i*@range) end end |
#get(index) ⇒ Object
Returns the value for the specified index.
90 91 92 93 94 |
# File 'lib/rrlist/list.rb', line 90 def get(index) return nil unless in_limits?(index) pos = ((index/@range) % @size) @store.get(pos) end |
#higher?(index) ⇒ True
Returns if the giving number is higher that the current max index.
66 67 68 |
# File 'lib/rrlist/list.rb', line 66 def higher?(index) index > (max_index + remaining_in_slot) end |
#in_limits?(index) ⇒ True
Returns if the given number is in the limits of the current max an min index of the list.
48 49 50 |
# File 'lib/rrlist/list.rb', line 48 def in_limits?(index) ( !higher?(index) && !lower?(index)) end |
#index_size ⇒ Integer
Returns the index size of this list.
140 141 142 |
# File 'lib/rrlist/list.rb', line 140 def index_size @size*@range end |
#lower?(index) ⇒ True
Returns if the giving number is lower that the current min index.
72 73 74 |
# File 'lib/rrlist/list.rb', line 72 def lower?(index) index < min_index end |
#max_index ⇒ Integer
Returns the max indes of current list.
154 155 156 |
# File 'lib/rrlist/list.rb', line 154 def max_index @current_index end |
#min_index ⇒ Integer
Returns The min index of the current list.
145 146 147 148 149 150 151 |
# File 'lib/rrlist/list.rb', line 145 def min_index if max_index <= index_size 0 else (max_index + 1) - index_size + remaining_in_slot end end |
#out_of_range?(index) ⇒ True
Returns if the given number is out of the range and all numbers will be lost if set.
54 55 56 57 58 59 60 61 62 |
# File 'lib/rrlist/list.rb', line 54 def out_of_range?(index) # refactor to out of limits? if in_limits?(index) return false elsif higher?(index) (index - max_index) >= (@size*@range) elsif lower?(index) (min_index - index) >= (@size*@range) end end |
#ranges ⇒ Array<Integer>
Returns The min_index and max_index as a list.
42 43 44 |
# File 'lib/rrlist/list.rb', line 42 def ranges return [min_index,max_index] end |
#remaining_in_slot ⇒ Integer
Returns if range is used, returns the remaining numbers in the current position.
135 136 137 |
# File 'lib/rrlist/list.rb', line 135 def remaining_in_slot (@range - (max_index % @range)) - 1 end |
#reset ⇒ Object
Reset this list, set all values to nil and move cursor to position 0
35 36 37 38 39 |
# File 'lib/rrlist/list.rb', line 35 def reset clear @position = 0 @current_index = 0 end |
#set_at(index, value) ⇒ Object
Set the specified value in the given index. The index must be in the range.
126 127 128 129 130 131 132 |
# File 'lib/rrlist/list.rb', line 126 def set_at(index, value) raise "The index #{index} is not in the range of the list" unless in_limits?(index) pos = position_for_index(index) set_value(pos,value) end |
#to_s ⇒ Object
Pretty print this object
170 171 172 |
# File 'lib/rrlist/list.rb', line 170 def to_s values.to_s end |
#values ⇒ Array<Object>
Returns The values of this list.
159 160 161 162 163 164 165 166 167 |
# File 'lib/rrlist/list.rb', line 159 def values ret = if @current_index < (@size*@range) @store.raw else @store.start_at(@position) end return ret end |