Class: DS::ExpandableArray

Inherits:
Array
  • Object
show all
Defined in:
lib/ds/arrays/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

Constructor Details

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

Returns a new instance of ExpandableArray.



6
7
8
9
# File 'lib/ds/arrays/expandable_array.rb', line 6

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.



13
14
15
16
# File 'lib/ds/arrays/expandable_array.rb', line 13

def [](x)
  expand(size, x) if x >= size
  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.



20
21
22
23
24
# File 'lib/ds/arrays/expandable_array.rb', line 20

def []=(x, v)
  old_size = size
  super(x, v)
  expand(old_size, size - 2) if size - old_size > 1
end