walkabout 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb6d40e1b5e2ed9cce8ec9546160fcc140db88d7a832f7f49a9864e801e67c86
4
- data.tar.gz: 06eb640637694a06b5f6bf7d32ef02bfb74cda68b3b1b9f29844eb2ccc1affa9
3
+ metadata.gz: 499d28078282af57fe6769db693cfe1e025c23a3dbfd0c9183c3e0731b6e7b50
4
+ data.tar.gz: 5588b93b988afd8555b2130e078fe2ac2b546729c2df73e022f453727767f58f
5
5
  SHA512:
6
- metadata.gz: 7d16e23d55b2b8ceb2c5a3aef3c54037a7ed6ec7a77b9d3b130cb060486c66cd291d822496f3be80cf4aab7a11142a7cc880d5ca5ea092c7545301b79a678e4d
7
- data.tar.gz: d7d6548a4c0a4bf81f9f0e33d1969b7ac92dcda796b73aa92da9d1e2f4512c8bcf44ddfee949b57728bb2a390c6363614d312659aa0604168d408958d60d81dd
6
+ metadata.gz: 7e2043bc352a431c3fa6cc63c33d3483d873e65c9eb2e7a3df48d3c0db83bb1b9cfd5e2f869ef172eddce8daee5e38a542510a54bbeb6cc9fb4e9eade3ac6a43
7
+ data.tar.gz: 73a3576d722319047d7433e611af22f35e2725c7744ef6fd4628a04c5ce8a98815ca5c676401f5a587696e034e30e98632bc9c8e648fb858e69ac118eda60da3
data/CHANGELOG CHANGED
@@ -1,5 +1,20 @@
1
1
  # walkabout/CHANGELOG
2
2
 
3
+ ## 20261110
4
+
5
+ 0.3.1: ~ DAG: An edge given twice is held once, a sequence of one member makes a node, and cycles are found in linear time.
6
+
7
+ 1. ~ DAG#build_nodes: Each edge is taken once, by edges.uniq. Duplicates made a node's predecessors wrong for anything reading them.
8
+ 2. ~ DAG#build_nodes: An edge of one member makes its node alone.
9
+ 3. ~ DAG.parse_sequence: A sequence of one member is an edge of one, rather than nothing. Its node was dropped.
10
+ 4. ~ DAG#cyclic_nodes: Trims forward from the nodes with no predecessor, then back from those with no successor, in linear time, rather than rescanning every node until nothing more goes.
11
+ 5. + DAG#trim
12
+ 6. - DAG#nodes_with_predecessor_and_successor_among
13
+ 7. ~ lib/Walkabout/VERSION.rb: /0.3.0/0.3.1/
14
+ 8. ~ test/Walkabout/DAG_test.rb: An edge given twice, a sequence of one member, an empty sequence, and a path between two cycles are tested.
15
+ 9. ~ README.md: A sequence of one member makes a node, and an edge given twice is held once.
16
+
17
+
3
18
  ## 20261024
4
19
 
5
20
  0.3.0: + cycle detection; + walkabout(.rb).gemspec
data/README.md CHANGED
@@ -40,7 +40,8 @@ dag = Walkabout::DAG.parse([
40
40
  ```
41
41
 
42
42
  Nodes are made from the names in the edges, and so a node appears only where it
43
- is in at least one edge.
43
+ is in at least one edge, or is the one member of a sequence. An edge given more
44
+ than once is held once.
44
45
 
45
46
  ### Walking it
46
47
 
data/lib/Walkabout/DAG.rb CHANGED
@@ -14,7 +14,11 @@ module Walkabout
14
14
  private
15
15
 
16
16
  def parse_sequence(sequence)
17
- sequence.each_cons(2).to_a
17
+ if sequence.size == 1
18
+ [sequence]
19
+ else
20
+ sequence.each_cons(2).to_a
21
+ end
18
22
  end
19
23
  end # class << self
20
24
 
@@ -39,8 +43,10 @@ module Walkabout
39
43
 
40
44
  def build_nodes(edges)
41
45
  nodes = {}
42
- edges.each do |a, b|
46
+ edges.uniq.each do |edge|
47
+ a, b = edge
43
48
  predecessor = (nodes[a] ||= Node.new(a))
49
+ next if edge.size == 1
44
50
  successor = (nodes[b] ||= Node.new(b))
45
51
  successor.predecessors << predecessor
46
52
  end
@@ -52,21 +58,32 @@ module Walkabout
52
58
  raise CycleError.new(nodes) unless nodes.empty?
53
59
  end
54
60
 
55
- # Trims, until nothing more goes, every node with no predecessor or no
56
- # successor left among those remaining. Only nodes on a cycle, or on a path
57
- # between two, survive.
61
+ # Trims forward from the nodes with no predecessor, then back from those
62
+ # left with no successor. Only nodes on a cycle, or on a path between two,
63
+ # survive.
58
64
  def cyclic_nodes
59
- nodes = @nodes
60
- while (trimmed = nodes_with_predecessor_and_successor_among(nodes)) != nodes
61
- nodes = trimmed
62
- end
63
- nodes
65
+ predecessors = @nodes.to_h{|node| [node, node.predecessors]}
66
+ successors = @nodes.to_h{|node| [node, []]}
67
+ @nodes.each{|node| node.predecessors.each{|p| successors[p] << node}}
68
+ trim(trim(@nodes, predecessors, successors), successors, predecessors)
64
69
  end
65
70
 
66
- def nodes_with_predecessor_and_successor_among(nodes)
67
- nodes.select do |node|
68
- node.predecessors.any?{|p| nodes.include?(p)} && nodes.any?{|n| n.predecessors.include?(node)}
71
+ # Removes each node with nothing inward of it among those remaining, until
72
+ # none is left to remove.
73
+ def trim(nodes, inward, outward)
74
+ remaining = nodes.to_h{|node| [node, true]}
75
+ counts = nodes.to_h{|node| [node, inward[node].count{|n| remaining[n]}]}
76
+ queue = nodes.select{|node| counts[node].zero?}
77
+ until queue.empty?
78
+ node = queue.shift
79
+ remaining.delete(node)
80
+ outward[node].each do |n|
81
+ next unless remaining[n]
82
+ counts[n] -= 1
83
+ queue << n if counts[n].zero?
84
+ end
69
85
  end
86
+ nodes.select{|node| remaining[node]}
70
87
  end
71
88
 
72
89
  def ready
@@ -2,5 +2,5 @@
2
2
  # Walkabout::VERSION
3
3
 
4
4
  module Walkabout
5
- VERSION = '0.3.0'
5
+ VERSION = '0.3.1'
6
6
  end
@@ -15,6 +15,22 @@ describe Walkabout::DAG do
15
15
  _(names(dag.take(5))).must_equal [:a, :d]
16
16
  end
17
17
 
18
+ it 'gives a predecessor once where an edge is given twice' do
19
+ dag = Walkabout::DAG.parse([[:a, :b], [:a, :b, :c]])
20
+ dag.take.complete!
21
+ _(names(dag.take.predecessors)).must_equal [:a]
22
+ end
23
+
24
+ it 'makes a node of a sequence with one member' do
25
+ dag = Walkabout::DAG.parse([[:a], [:b, :c]])
26
+ _(names(dag.take(5))).must_equal [:a, :b]
27
+ end
28
+
29
+ it 'makes no node of an empty sequence' do
30
+ dag = Walkabout::DAG.parse([[], [:a, :b]])
31
+ _(names(dag.take(5))).must_equal [:a]
32
+ end
33
+
18
34
  it 'joins sequences at the members they share' do
19
35
  dag = Walkabout::DAG.parse([[:a, :b], [:c, :b]])
20
36
  dag.take.complete!
@@ -80,6 +96,11 @@ describe Walkabout::DAG do
80
96
  _(error.message).must_equal 'cycle among: a, b, c'
81
97
  end
82
98
 
99
+ it 'names the nodes on a path between two cycles' do
100
+ error = _{Walkabout::DAG.parse([[:a, :b, :a], [:b, :c, :d, :c]])}.must_raise Walkabout::CycleError
101
+ _(names(error.nodes).sort).must_equal [:a, :b, :c, :d]
102
+ end
103
+
83
104
  it 'does not raise where paths diverge and rejoin' do
84
105
  _(Walkabout::DAG.parse([[:a, :b, :d], [:a, :c, :d]]).take.name).must_equal :a
85
106
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: walkabout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 4.0.22
78
+ rubygems_version: 4.0.23
79
79
  specification_version: 4
80
80
  summary: Walk a directed acyclic graph in dependency order.
81
81
  test_files: []