Class: DS::HeapStore

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ds/arrays/heap_store.rb

Overview

Simple Array-like data structure with indexing starting from one.

Direct Known Subclasses

IndexedHeapStore

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ HeapStore

Returns a new instance of HeapStore.



10
11
12
# File 'lib/ds/arrays/heap_store.rb', line 10

def initialize(*args)
  @store = [nil] + args.to_a
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/ds/arrays/heap_store.rb', line 24

def empty?
  length <= 0
end

#firstObject



18
19
20
# File 'lib/ds/arrays/heap_store.rb', line 18

def first
  @store[1]
end

#lengthObject Also known as: size



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

def length
  @store.size - 1
end

#swap(x, y) ⇒ Object



32
33
34
35
36
# File 'lib/ds/arrays/heap_store.rb', line 32

def swap(x, y)
  temp = self[y]
  self[y] = self[x]
  self[x] = temp
end

#to_aObject



28
29
30
# File 'lib/ds/arrays/heap_store.rb', line 28

def to_a
  @store[1..-1]
end