Regenerate gemspec for version 0.2.0
authorNeil Smith <neil.github@njae.me.uk>
Fri, 23 Sep 2011 15:48:38 +0000 (16:48 +0100)
committerNeil Smith <neil.github@njae.me.uk>
Fri, 23 Sep 2011 15:48:38 +0000 (16:48 +0100)
graph.njae.gemspec
lib/graph.njae/edge.rb [new file with mode: 0644]
lib/graph.njae/graph.rb [new file with mode: 0644]
lib/graph.njae/vertex.rb [new file with mode: 0644]

index bda625afdfb38a51eeff5868e0dcc89f9192307a..0668b049e9aa7b5012a9c55009c868c3569efffd 100644 (file)
@@ -5,7 +5,7 @@
 
 Gem::Specification.new do |s|
   s.name = %q{graph.njae}
-  s.version = "0.1.0"
+  s.version = "0.2.0"
 
   s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
   s.authors = ["Neil Smith"]
@@ -27,9 +27,9 @@ Gem::Specification.new do |s|
     "VERSION",
     "graph.njae.gemspec",
     "lib/graph.njae.rb",
-    "lib/graph/edge.rb",
-    "lib/graph/graph.rb",
-    "lib/graph/vertex.rb",
+    "lib/graph.njae/edge.rb",
+    "lib/graph.njae/graph.rb",
+    "lib/graph.njae/vertex.rb",
     "spec/graph/edge_spec.rb",
     "spec/graph/graph_spec.rb",
     "spec/graph/vertex_spec.rb",
diff --git a/lib/graph.njae/edge.rb b/lib/graph.njae/edge.rb
new file mode 100644 (file)
index 0000000..6581033
--- /dev/null
@@ -0,0 +1,46 @@
+require 'ostruct'
+
+# A simple graph library
+
+module GraphNjae
+  
+  # An edge (or multiedge) in a graph. The edge can have arbitrary attributes,
+  # treated as method names.
+  #
+  # 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
+      self.connections = []
+      self
+    end
+    
+    # Connect this edge to a vertex
+    def <<(other)
+      c = Connection.new
+      c.end = other
+      self.connections << c
+      self
+    end
+    
+    # Return the set of vertices this edge connects.
+    def vertices
+      self.connections.map {|c| c.end}
+    end
+    
+    # Return the connection object that joins this Edge to the specified Vertex
+    def connection_at(vertex)
+      self.connections.select {|c| c.end.equal?  vertex}.first
+    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
+      self
+    end
+  end
+end
diff --git a/lib/graph.njae/graph.rb b/lib/graph.njae/graph.rb
new file mode 100644 (file)
index 0000000..1da5297
--- /dev/null
@@ -0,0 +1,27 @@
+require 'ostruct'
+
+# A simple graph library
+
+module GraphNjae
+  
+  # A container for all the parts of a graph.  The graph can have arbitrary attributes,
+  # treated as method names.
+  class Graph < OpenStruct
+    def initialize
+      super
+      self.edges = Array.new
+      self.vertices = Array.new
+      self
+    end
+    
+    # Add a Vertex or Edge to the graph.
+    def <<(other)
+      if other.class == Vertex
+        self.vertices << other
+      elsif
+        self.edges << other
+      end
+      self
+    end
+  end
+end
diff --git a/lib/graph.njae/vertex.rb b/lib/graph.njae/vertex.rb
new file mode 100644 (file)
index 0000000..bf14ddd
--- /dev/null
@@ -0,0 +1,41 @@
+require 'ostruct'
+
+# A simple graph library
+
+module GraphNjae
+  # A vertex in a graph. The edge can have arbitrary attributes,treated as 
+  # method names.
+  class Vertex < OpenStruct
+    def initialize
+      super
+      self.edges = []
+      self
+    end
+    
+    # Connect this vertex to another, creating an Edge to do so, and returning
+    # the Edge
+    def connect(other)
+      e = Edge.new
+      e << self << other
+      self.edges << e
+      other.edges << e unless self === other
+      e
+    end
+    
+    # Connect this vertex to another, creating an Edge to do so, and returning
+    # this Vertex
+    def <<(other)
+      connect(other)
+      self
+    end
+    
+    # Return the set of neighbouring vertices
+    def neighbours
+      vertices = self.edges.map {|e| e.vertices}.flatten
+      vertices_to_me = vertices.select {|v| v == self}
+      other_vertices = vertices.select {|v| v != self}
+      (vertices_to_me[1..-1] || []) + other_vertices
+    end
+    
+  end
+end