Class: Plexus::Edge

Inherits:
Arc show all
Defined in:
lib/plexus/edge.rb

Overview

An undirected edge is simply an undirected pair (source, target) used in undirected graphs. Edge == Edge

Direct Known Subclasses

MultiEdge

Instance Method Summary collapse

Methods inherited from Arc

[], #initialize, #inspect, #reverse

Constructor Details

This class inherits a constructor from Plexus::Arc

Instance Method Details

#<=>(rhs) ⇒ Object

Sort support



21
22
23
# File 'lib/plexus/edge.rb', line 21

def <=>(rhs)
  [[source,target].max, [source,target].min] <=> [[rhs.source,rhs.target].max, [rhs.source,rhs.target].min]
end

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

Edge equality allows for the swapping of source and target.

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/plexus/edge.rb', line 8

def eql?(other)
  same_class = self.class.ancestors.include?(other.class) || other.class.ancestors.include?(self.class)
  super || (same_class && target == other.source && source == other.target)
end

#hashObject

Hash is defined such that source and target can be reversed and the hash value will be the same



16
17
18
# File 'lib/plexus/edge.rb', line 16

def hash
  source.hash ^ target.hash
end

#to_sObject

Edge.to_s => “(1=2)” Edge.to_s => “(1=2)” Edge.to_s => “(1=2 test)”



28
29
30
31
32
33
# File 'lib/plexus/edge.rb', line 28

def to_s
  l = label ? " '#{label.to_s}'" : ''
  s = source.to_s
  t = target.to_s
  "(#{[s,t].min}=#{[s,t].max}#{l})"
end