End of the day
[graph.njae.git] / lib / graph.njae / edge.rb
index f38a32b21e693bbf645c25bdcca159579f97dceb..f3efc91eec9157301ece98c735663d2c94f0d1c8 100644 (file)
@@ -10,8 +10,8 @@ module GraphNjae
   # Each connection is handled by a Graph::Connection object, so that each end
   # of the Edge can have it's own attributes.
   class Edge < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self.connections = []
       self
     end
@@ -20,6 +20,7 @@ module GraphNjae
     def <<(other)
       c = Connection.new
       c.end = other
+      other.edges << self unless other.edges.include? self
       self.connections << c
       self
     end
@@ -33,13 +34,27 @@ module GraphNjae
     def connection_at(vertex)
       self.connections.find {|c| c.end.equal?  vertex}
     end
+    
+    # Return the vertex at the other end of the one given.
+    # Self-loops should still return the vertex
+    def other_end(vertex)
+      if self.vertices[0] == vertex
+        self.vertices[1]
+      else
+        self.vertices[0]
+      end
+    end
+    
+    def to_s
+      '<E: ' + self.type.to_s + ' [' + self.vertices.map {|n| n.to_s}.join(', ') + '] >'
+    end
   end
   
   # A connection between an Edge and a Vertex.The connection can have arbitrary attributes,
   # treated as method names.
   class Connection < OpenStruct
-    def initialize
-      super
+    def initialize(values = {})
+      super(values)
       self
     end
   end