Class: RRList::List

Inherits:
Object
  • Object
show all
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

Author:

  • Federico Dayan

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|index, old_value, new_value| ... } ⇒ List

Creates an object

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :size (Integer)

    The size of the list. It does not add more elements than this.

  • :range (Integer)

    The range of the list. A range of 5 group 5 indexes into one position.

  • :store (Integer)

    Represents the where we save the data. It must follow a contract (see RRList:Stores:InMemoryArray)

Yields:

  • (index, old_value, new_value)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rrlist/list.rb', line 16

def initialize(options={},&function_proc)
  raise ":size is required" unless options[:size]
  raise ":range should be greater thant 0" if options[:range] &&  options[:range] < 1

  @store = options[:store] || RRList::Stores::InMemoryArray.new
  @size = options[:size].to_i
  @range = options[: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.

See Also:



192
193
194
# File 'lib/rrlist/list.rb', line 192

def << value
  add(value)
end

#[](index) ⇒ Object

Returns the value for the given index

See Also:



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.

See Also:



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

Parameters:

  • value

    any object



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

Parameters:

  • index

    must be higher than max_index

  • value

    any object



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

#clearObject

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_indexObject

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.

Returns:

  • (Object)

    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.

Parameters:

  • index (Integer)

    A number

Returns:

  • (True)

    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.

Parameters:

  • index (Integer)

    A number

Returns:

  • (True)

    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_sizeInteger

Returns the index size of this list.

Returns:

  • (Integer)

    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.

Parameters:

  • index (Integer)

    A number

Returns:

  • (True)

    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_indexInteger

Returns the max indes of current list.

Returns:

  • (Integer)

    the max indes of current list



154
155
156
# File 'lib/rrlist/list.rb', line 154

def max_index
  @current_index
end

#min_indexInteger

Returns The min index of the current list.

Returns:

  • (Integer)

    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.

Parameters:

  • index (Integer)

    A number

Returns:

  • (True)

    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

#rangesArray<Integer>

Returns The min_index and max_index as a list.

Returns:

  • (Array<Integer>)

    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_slotInteger

Returns if range is used, returns the remaining numbers in the current position.

Returns:

  • (Integer)

    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

#resetObject

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_sObject

Pretty print this object



170
171
172
# File 'lib/rrlist/list.rb', line 170

def to_s
  values.to_s
end

#valuesArray<Object>

Returns The values of this list.

Returns:

  • (Array<Object>)

    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