Class: DS::ExpandableArray

Inherits:
Array
  • Object
show all
Defined in:
lib/ds/matrixes/expandable_array.rb

Overview

Class provides access to any element in array. If index i is greter than array size then elements from size to i are filled with extender.

Instance Method Summary collapse

Methods inherited from Array

#push_uniq, #sorted?

Constructor Details

#initialize(size = 0, extender = 0) ⇒ ExpandableArray

Returns a new instance of ExpandableArray.



7
8
9
10
# File 'lib/ds/matrixes/expandable_array.rb', line 7

def initialize(size=0,extender=0)
  @extender = extender
  super(size,extender)
end

Instance Method Details

#[](x) ⇒ Object

Returns element at index. If index is greater than size of array then elements from size to index are filled with extender.



14
15
16
17
18
19
20
21
# File 'lib/ds/matrixes/expandable_array.rb', line 14

def [](x)
  if x >=size
    for i in size..x
      self[i]=@extender
    end
  end
  super(x)
end

#[]=(x, v) ⇒ Object

Sets the element at index. If index is greater than size of array then elements from size to index are filled with extender.



25
26
27
28
29
30
31
32
33
# File 'lib/ds/matrixes/expandable_array.rb', line 25

def []=(x,v)
  max = size
  super(x,v)
  if size - max >1
    (max..size-2).each do |i|
      self[i] = @extender
    end
  end
end