tangle 0.3.1 → 0.4.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: 4456f429bc3e43dc2498e665b5ab50ceeba8ada36ed86fad33e477675237f53f
4
- data.tar.gz: 961bfb96b98ec4bc56fc7e4feed6b36a7f5db1d5427ca636274e2799b609939a
3
+ metadata.gz: 46af55dc3f6a3cb2ffc754776a93209e02d647cac2aa824729b3816665b344a6
4
+ data.tar.gz: b128f9e8ffa5c608ffc2b9a12946c772886b4bdec661d51ac542c9e25346e890
5
5
  SHA512:
6
- metadata.gz: 7388845b841906216962b5b536084518a2ec2ddd645d5f13627efb860d0073a04c59afe5e088e664528e9c55a87a0deca1e6feeeee66a047b1f3ed0204b835de
7
- data.tar.gz: 23a0299a55237a37a752ea57cbc477d70cae6a95ad80c44e004ca651a4f5faab266a9022bea668e6a73b51088811e46adee6a53a41deda278f79a8191768f6e8
6
+ metadata.gz: 5cd121de93316e33175f8184a7092bce69e85c6865b75826da6fd741997b583004b53167679cf098e9e2cb9023809e725d7ab9528c61d32cfeef02e1a589d7a6
7
+ data.tar.gz: 685ce03d7f320333b527c22582a46aedddcf2ab7bd0eec1351aa49b5ec9c319130b2d3ccd4348558844ebda593ab5e8403765711f235e85730b9c4eca96fc424
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Gem Version](https://badge.fury.io/rb/tangle.svg)](https://badge.fury.io/rb/tangle)
1
+ [![Gem Version](https://badge.fury.io/rb/tangle.svg)](https://badge.fury.io/rb/tangle) [![Maintainability](https://api.codeclimate.com/v1/badges/0d92a4d05b6bb5c06dce/maintainability)](https://codeclimate.com/github/notCalle/ruby-tangle/maintainability)
2
2
 
3
3
  # Tangle
4
4
 
@@ -0,0 +1,31 @@
1
+ require 'tangle/edge'
2
+
3
+ module Tangle
4
+ module Directed
5
+ #
6
+ # An edge in a directed graph
7
+ #
8
+ class Edge < Tangle::Edge
9
+ def initialize(vertex1, vertex2 = vertex1, graph: nil)
10
+ @child, @parent = @vertices = [vertex1, vertex2]
11
+ super
12
+ end
13
+
14
+ def parent?(vertex)
15
+ @parent == vertex
16
+ end
17
+
18
+ def parent(_vertex = nil)
19
+ @parent
20
+ end
21
+
22
+ def child?(vertex)
23
+ @child == vertex
24
+ end
25
+
26
+ def child(_vertex = nil)
27
+ @child
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ require 'tangle/graph'
2
+ require 'tangle/directed/edge'
3
+
4
+ module Tangle
5
+ module Directed
6
+ #
7
+ # A directed graph
8
+ class Graph < Tangle::Graph
9
+ Edge = Tangle::Directed::Edge
10
+ DEFAULT_MIXINS = [Tangle::Mixin::Ancestry].freeze
11
+
12
+ def initialize(mixins: DEFAULT_MIXINS, **kvargs)
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/tangle/edge.rb CHANGED
@@ -16,7 +16,7 @@ module Tangle
16
16
  # End users should probably use Graph#add_edge instead.
17
17
  #
18
18
  def initialize(vertex1, vertex2 = vertex1, graph: nil)
19
- @vertices = Set[vertex1, vertex2]
19
+ @vertices ||= Set[vertex1, vertex2]
20
20
  @graph = graph
21
21
 
22
22
  initialize_mixins
@@ -28,9 +28,16 @@ module Tangle
28
28
  #
29
29
  # walk(vertex) => Vertex
30
30
  #
31
- def walk(from_vertex)
32
- raise RuntimeError unless @vertices.include?(from_vertex)
33
- @vertices.find { |other| other != from_vertex } || from_vertex
31
+ def walk(from_vertex, selector: :other)
32
+ send(selector, from_vertex)
33
+ end
34
+
35
+ # Return the other vertex for a vertex of this edge
36
+ #
37
+ def other(for_vertex)
38
+ raise RuntimeError unless @vertices.include?(for_vertex)
39
+
40
+ @vertices.find { |other| other != for_vertex } || for_vertex
34
41
  end
35
42
 
36
43
  # Duplicate an edge into another graph, replacing original vertices with
@@ -0,0 +1,64 @@
1
+ require 'tangle/mixin/connectedness'
2
+
3
+ module Tangle
4
+ module Mixin
5
+ #
6
+ # Mixin for adding ancestry features
7
+ #
8
+ module Ancestry
9
+ #
10
+ # Mixins for adding ancestry relations to a digraph
11
+ #
12
+ module Graph
13
+ include Tangle::Mixin::Connectedness::Graph
14
+
15
+ def ancestor_subgraph(vertex)
16
+ subgraph { |other| vertex.ancestor?(other) }
17
+ end
18
+
19
+ def descendant_subgraph(vertex)
20
+ subgraph { |other| vertex.descendant?(other) }
21
+ end
22
+ end
23
+
24
+ #
25
+ # Mixins for adding ancestry relations to vertices in a digraph
26
+ #
27
+ module Vertex
28
+ include Tangle::Mixin::Connectedness::Vertex
29
+
30
+ def parent_edges
31
+ @graph.edges { |edge| edge.child?(self) }
32
+ end
33
+
34
+ def parents
35
+ neighbours(parent_edges)
36
+ end
37
+
38
+ def parent?(other)
39
+ @graph.edges.any? { |edge| edge.child?(self) && edge.parent?(other) }
40
+ end
41
+
42
+ def ancestor?(other)
43
+ connected?(other, test_method: :parent?)
44
+ end
45
+
46
+ def child_edges
47
+ @graph.edges { |edge| edge.parent?(self) }
48
+ end
49
+
50
+ def children
51
+ neighbours(child_edges)
52
+ end
53
+
54
+ def child?(other)
55
+ @graph.edges.any? { |edge| edge.parent?(self) && edge.child?(other) }
56
+ end
57
+
58
+ def descendant?(other)
59
+ connected?(other, test_method: :child?)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,5 +1,8 @@
1
1
  module Tangle
2
2
  module Mixin
3
+ #
4
+ # Mixins for adding connectedness features
5
+ #
3
6
  module Connectedness
4
7
  #
5
8
  # Mixin for adding connectedness to a graph
@@ -50,20 +53,20 @@ module Tangle
50
53
  # Two vertices are connected if there is a path between them,
51
54
  # and a vertex is connected to itself.
52
55
  #
53
- def connected?(other)
56
+ def connected?(other, test_method: :adjacent?)
54
57
  raise GraphError unless @graph == other.graph
55
58
  return true if self == other
56
59
 
57
- connected_excluding?(other, Set[self])
60
+ connected_excluding?(other, test_method, Set[self])
58
61
  end
59
62
 
60
63
  protected
61
64
 
62
- def connected_excluding?(other, history)
63
- return true if adjacent?(other)
65
+ def connected_excluding?(other, test_method, history)
66
+ return true if send(test_method, other)
64
67
 
65
68
  (neighbours - history).any? do |vertex|
66
- vertex.connected_excluding?(other, history << self)
69
+ vertex.connected_excluding?(other, test_method, history << self)
67
70
  end
68
71
  end
69
72
  end
data/lib/tangle/mixin.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'tangle/mixin/ancestry'
1
2
  require 'tangle/mixin/connectedness'
2
3
 
3
4
  module Tangle
data/lib/tangle/vertex.rb CHANGED
@@ -61,8 +61,8 @@ module Tangle
61
61
 
62
62
  # Return the set of adjacent vertices
63
63
  #
64
- def neighbours
65
- Set.new(edges.map { |edge| edge.walk(self) })
64
+ def neighbours(included = edges)
65
+ Set.new(included.map { |edge| edge.walk(self) })
66
66
  end
67
67
 
68
68
  # If two vertices have the same vertex_id, they have the same value
data/lib/tangle.rb CHANGED
@@ -2,6 +2,7 @@ require 'tangle/version'
2
2
  require 'tangle/errors'
3
3
  require 'tangle/graph'
4
4
  require 'tangle/simple/graph'
5
+ require 'tangle/directed/graph'
5
6
 
6
7
  # Tangle manages various types of graphs
7
8
  #
@@ -14,4 +15,5 @@ require 'tangle/simple/graph'
14
15
  module Tangle
15
16
  MultiGraph = Tangle::Graph
16
17
  SimpleGraph = Tangle::Simple::Graph
18
+ DiGraph = Tangle::Directed::Graph
17
19
  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.3.1
4
+ version: 0.4.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-05 00:00:00.000000000 Z
11
+ date: 2018-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git-version-bump
@@ -99,10 +99,13 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - lib/tangle.rb
102
+ - lib/tangle/directed/edge.rb
103
+ - lib/tangle/directed/graph.rb
102
104
  - lib/tangle/edge.rb
103
105
  - lib/tangle/errors.rb
104
106
  - lib/tangle/graph.rb
105
107
  - lib/tangle/mixin.rb
108
+ - lib/tangle/mixin/ancestry.rb
106
109
  - lib/tangle/mixin/connectedness.rb
107
110
  - lib/tangle/simple/edge.rb
108
111
  - lib/tangle/simple/graph.rb