Class: Graphos::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/graphos/path.rb

Overview

This class represents a collection of edges When adding an edge that does not start at the last node, it raises an error

Defined Under Namespace

Classes: IncorretPathError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edge = nil) ⇒ Path

Returns a new instance of Path.



11
12
13
14
15
16
17
18
19
# File 'lib/graphos/path.rb', line 11

def initialize edge=nil
  if edge
    @cost = edge.weight
    @path = [edge]
  else
    @cost = 0
    @path = []
  end
end

Instance Attribute Details

#costObject (readonly)

Returns the value of attribute cost.



10
11
12
# File 'lib/graphos/path.rb', line 10

def cost
  @cost
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/graphos/path.rb', line 10

def path
  @path
end

Instance Method Details

#+(path) ⇒ Object



34
35
36
# File 'lib/graphos/path.rb', line 34

def + path
  dup.tap{|d| d.merge! path }
end

#add_edge(edge) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/graphos/path.rb', line 21

def add_edge edge
  if @path.last.to != edge.from
    raise IncorrectPathError.new
  end
  @path += [node]
  @cost += weight
end

#merge!(paf) ⇒ Object



29
30
31
32
# File 'lib/graphos/path.rb', line 29

def merge! paf
  @cost += paf.cost
  @path = @path + paf.path
end