Class: Digraph

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/graph/digraph.rb

Instance Method Summary collapse

Constructor Details

#initializeDigraph

Returns a new instance of Digraph.



6
7
8
# File 'lib/graph/digraph.rb', line 6

def initialize
  @vertices = Array.new
end

Instance Method Details

#add_vertex(vertex) ⇒ Object



24
25
26
27
# File 'lib/graph/digraph.rb', line 24

def add_vertex vertex
  @vertices.push vertex
  vertex
end

#each(&block) ⇒ Object



33
34
35
# File 'lib/graph/digraph.rb', line 33

def each &block
  @vertices.each &block
end

#eql?(obj) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/graph/digraph.rb', line 37

def eql? obj
  Set.new(self.vertices).eql? Set.new(obj.vertices)
  
end

#find_vertex(name, namespace = nil, paths = nil) ⇒ Object

Returns an Array of Vertices that match the name, namespace and path.



11
12
13
14
15
16
17
# File 'lib/graph/digraph.rb', line 11

def find_vertex name, namespace=nil, paths=nil # REFACTOR consider changing this to a Vertex object
  @vertices.select do |v|
    v.name == name &&
      (namespace.nil? || v.namespace.eql?(namespace)) &&
      (paths.nil? || (paths - v.paths).empty?) # checks to see if paths is a subset of v.paths
  end
end

#has_vertex?(vertex_name) ⇒ Boolean

Returns true if self has a Vertex with the name vertex_name.

Returns:

  • (Boolean)


20
21
22
# File 'lib/graph/digraph.rb', line 20

def has_vertex? vertex_name
  !@vertices.select { |v| v.name == vertex_name }.empty?
end

#remove_vertex(vertex) ⇒ Object



29
30
31
# File 'lib/graph/digraph.rb', line 29

def remove_vertex vertex
  @vertices.delete vertex
end

#to_sObject



47
48
49
# File 'lib/graph/digraph.rb', line 47

def to_s
  @vertices.map(&:to_s).join("\n")
end