tangle 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb6c6e2247f126a8309178790612215cd099aaecaf909de00f114a1fb04e5caf
4
- data.tar.gz: 42386680537801bfb1be9d1f14f684f6a82fe857246fdfe492d9ae31a602d980
3
+ metadata.gz: 1fe724ce08f713fbe503fde9eb9705f8e2aa39df7d44a2cb1463f5404dcb8380
4
+ data.tar.gz: 64ac53373a7747cb68f2023bb1cf6c5a9656449c0916ae1a2f6ee856f5c67593
5
5
  SHA512:
6
- metadata.gz: f231571ede13d8261d03c3a9c6410cf184ec79deb5a612aa9f655533410f296846e417aebde6a81c04bc7706253737689560b0fb0fabb52a9388314cc7514187
7
- data.tar.gz: 0a52fa6c13da5c282167690c7f1fa73a70b5c9175f7e34f9d68bfcdd0036f305dfe177fb2ad2153c7722bdf4fef989b694cc68cfb56edefb52ee424c28ff98aa
6
+ metadata.gz: 8121539f09661bf3ff0bac14211d0cda52768d2b0a9f4ffb76fb92ea1049afacebca54aa85fce60f47cbffe194ca1f2832758d3f6f1c28acb8bb80f09ee33cec
7
+ data.tar.gz: 604724b372a499f6237240a9dfa3298e91f1889c7f674a733956d56a616b633af154e4a97aa9250d724b8ba7306dad7f69615e4af295e1d7705c5ccbe672b06c
@@ -1,8 +1,17 @@
1
1
  require 'tangle/version'
2
+ require 'tangle/errors'
2
3
  require 'tangle/graph'
4
+ require 'tangle/simple/graph'
3
5
 
4
- # Tangle manages graphs of things
6
+ # Tangle manages various types of graphs
7
+ #
8
+ # Tangle::MultiGraph.new
9
+ # => Undirected graph without edge constraints
10
+ #
11
+ # Tangle::SimpleGraph.new
12
+ # => Undirected graph with single edges between vertices, and no loops
5
13
  #
6
14
  module Tangle
7
- # Your code goes here...
15
+ MultiGraph = Tangle::Graph
16
+ SimpleGraph = Tangle::Simple::Graph
8
17
  end
@@ -1,3 +1,5 @@
1
+ require 'tangle/errors'
2
+
1
3
  module Tangle
2
4
  #
3
5
  # An edge in a graph, connecting two vertices
@@ -16,7 +18,7 @@ module Tangle
16
18
  @vertices = Set[vertex1, vertex2]
17
19
  @graph = graph
18
20
 
19
- raise ArgumentError unless valid_edge?
21
+ validate_edge
20
22
  end
21
23
 
22
24
  # Follow the edge from a vertex to the other end
@@ -43,7 +45,7 @@ module Tangle
43
45
  vertices = @vertices.map do |vertex|
44
46
  graph.get_vertex(vertex.vertex_id)
45
47
  end
46
- Edge.new(*vertices, graph: graph)
48
+ self.class.new(*vertices, graph: graph)
47
49
  rescue KeyError
48
50
  nil
49
51
  end
@@ -52,16 +54,23 @@ module Tangle
52
54
  "#<#{self.class}: #{@vertices}>"
53
55
  end
54
56
 
57
+ def eql?(other)
58
+ @graph == other.graph && @vertices == other.vertices
59
+ end
60
+ alias == eql?
61
+ alias === eql?
62
+ alias equal? eql?
63
+
55
64
  attr_reader :name
56
65
  attr_reader :graph
57
66
  attr_reader :vertices
58
67
 
59
- def_delegators :@vertices, :include?
68
+ def_delegators :@vertices, :include?, :hash
60
69
 
61
70
  private
62
71
 
63
- def valid_edge?
64
- @vertices.all? { |vertex| vertex.graph == @graph }
72
+ def validate_edge
73
+ raise GraphError unless @vertices.all? { |vertex| vertex.graph == @graph }
65
74
  end
66
75
  end
67
76
  end
@@ -0,0 +1,28 @@
1
+ module Tangle
2
+ #
3
+ # LoopError is raised when a looped edge is disallowed.
4
+ #
5
+ class LoopError < RuntimeError
6
+ def initialize(reason = 'loops not allowed', *)
7
+ super
8
+ end
9
+ end
10
+
11
+ # MultiEdgeError is raised when multiple edges between a single pair of
12
+ # vertices is disallowed.
13
+ #
14
+ class MultiEdgeError < RuntimeError
15
+ def initialize(reason = 'multiedges not allowed', *)
16
+ super
17
+ end
18
+ end
19
+
20
+ # GraphError is raised when graph elements in an operation belong to
21
+ # different graphs.
22
+ #
23
+ class GraphError < RuntimeError
24
+ def initialize(reason = 'not in the same graph', *)
25
+ super
26
+ end
27
+ end
28
+ end
@@ -6,6 +6,8 @@ module Tangle
6
6
  # Base class for all kinds of graphs
7
7
  #
8
8
  class Graph
9
+ Edge = Tangle::Edge
10
+
9
11
  # Initialize a new graph, optionally preloading it with vertices and edges
10
12
  #
11
13
  # Graph.new() => Graph
@@ -28,7 +30,7 @@ module Tangle
28
30
  def initialize(vertices: nil, edges: nil)
29
31
  @vertices_by_id = {}
30
32
  @vertices_by_name = {}
31
- @edges = []
33
+ @edges ||= []
32
34
 
33
35
  initialize_vertices(vertices)
34
36
  initialize_edges(edges)
@@ -42,7 +44,7 @@ module Tangle
42
44
  if block_given?
43
45
  @edges.select(&selector)
44
46
  else
45
- @edges
47
+ @edges.to_a
46
48
  end
47
49
  end
48
50
 
@@ -52,7 +54,7 @@ module Tangle
52
54
  #
53
55
  def add_edge(*vertices, **kvargs)
54
56
  vertices = vertices.map { |v| get_vertex(v) }
55
- insert_edge(Edge.new(*vertices, graph: self, **kvargs))
57
+ insert_edge(self.class::Edge.new(*vertices, graph: self, **kvargs))
56
58
  end
57
59
 
58
60
  # Get all vertices.
@@ -97,7 +99,7 @@ module Tangle
97
99
  # Unless a selector is provided, the subgraph contains the entire graph.
98
100
  #
99
101
  def subgraph(&selector)
100
- graph = Graph.new
102
+ graph = self.class.new
101
103
 
102
104
  dup_vertices_into(graph, &selector)
103
105
  dup_edges_into(graph)
@@ -0,0 +1,18 @@
1
+ require 'tangle/edge'
2
+
3
+ module Tangle
4
+ module Simple
5
+ #
6
+ # An edge in a simple graph, with no loops or multiedges
7
+ #
8
+ class Edge < Tangle::Edge
9
+ private
10
+
11
+ def validate_edge
12
+ super
13
+ raise LoopError unless @vertices.count == 2
14
+ raise MultiEdgeError if @graph.edges.include? self
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ require 'tangle/graph'
2
+ require 'tangle/simple/edge'
3
+
4
+ module Tangle
5
+ module Simple
6
+ #
7
+ # A simple graph, without loops and multiple edges
8
+ class Graph < Tangle::Graph
9
+ Edge = Tangle::Simple::Edge
10
+
11
+ def initialize(**kvargs)
12
+ @edges ||= Set[]
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Tangle
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tangle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Calle Englund
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-03 00:00:00.000000000 Z
11
+ date: 2018-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,7 +86,10 @@ files:
86
86
  - bin/setup
87
87
  - lib/tangle.rb
88
88
  - lib/tangle/edge.rb
89
+ - lib/tangle/errors.rb
89
90
  - lib/tangle/graph.rb
91
+ - lib/tangle/simple/edge.rb
92
+ - lib/tangle/simple/graph.rb
90
93
  - lib/tangle/version.rb
91
94
  - lib/tangle/vertex.rb
92
95
  - tangle.gemspec