Class: Seafoam::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/seafoam/graph.rb

Overview

A graph, with properties, nodes, and edges. We don’t encapsulate the graph too much - be careful.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = nil) ⇒ Graph

Returns a new instance of Graph.



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

def initialize(props = nil)
  @props = props || {}
  @nodes = {}
  @edges = []
  @blocks = []
  @new_id = 0
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



9
10
11
# File 'lib/seafoam/graph.rb', line 9

def blocks
  @blocks
end

#edgesObject (readonly)

Returns the value of attribute edges.



9
10
11
# File 'lib/seafoam/graph.rb', line 9

def edges
  @edges
end

#new_idObject (readonly)

Returns the value of attribute new_id.



9
10
11
# File 'lib/seafoam/graph.rb', line 9

def new_id
  @new_id
end

#nodesObject (readonly)

Returns the value of attribute nodes.



9
10
11
# File 'lib/seafoam/graph.rb', line 9

def nodes
  @nodes
end

#propsObject (readonly)

Returns the value of attribute props.



9
10
11
# File 'lib/seafoam/graph.rb', line 9

def props
  @props
end

Instance Method Details

#create_block(id, node_ids) ⇒ Object

Add a new basic block with given id and node id list.



39
40
41
42
43
44
# File 'lib/seafoam/graph.rb', line 39

def create_block(id, node_ids)
  nodes = node_ids.select { |n| @nodes.key?(n) }.map { |n| @nodes[n] }
  block = Block.new(id, nodes)
  @blocks.push(block)
  block
end

#create_edge(from, to, props = nil) ⇒ Object

Create an edge between two nodes.



29
30
31
32
33
34
35
36
# File 'lib/seafoam/graph.rb', line 29

def create_edge(from, to, props = nil)
  props ||= {}
  edge = Edge.new(from, to, props)
  @edges.push(edge)
  from.outputs.push(edge)
  to.inputs.push(edge)
  edge
end

#create_node(id, props = nil) ⇒ Object

Create a node.



20
21
22
23
24
25
26
# File 'lib/seafoam/graph.rb', line 20

def create_node(id, props = nil)
  props ||= {}
  node = Node.new(id, props)
  @nodes[id] = node
  @new_id = id + 1
  node
end

#remove_edge(edge) ⇒ Object



46
47
48
49
50
# File 'lib/seafoam/graph.rb', line 46

def remove_edge(edge)
  edge.from.outputs.delete(edge)
  edge.to.inputs.delete(edge)
  edges.delete(edge)
end