modular_tree 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/modular_tree/algorithms.rb +15 -15
- data/lib/modular_tree/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fec58fa42f91715e9a59438b1bf403976618ead4f433d70b39c8458c9396fe5
|
4
|
+
data.tar.gz: 255a5d2ae6daaf623f5f6adfa7d9b7206a66e3f9613a11f061e9c2336feec943
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74f3701071758d0fce55d9e103142532e760c861c96b2ca2835894b4c2e298f2a1190e4901db36dfcb5aa8cdfadb73ea976aad38551d58a452c1d1b0d8dc09e0
|
7
|
+
data.tar.gz: 21785c97fe07687be2c55eb1e3f5c7c9377db855392eeaa9caa490621512ef0ba930202eb63a0b0b8d50a45e6f854217955b2eeb3db4044264bcc978da821319
|
@@ -89,9 +89,8 @@ module Tree
|
|
89
89
|
|
90
90
|
# Enumerator of edges in the tree. Edges are [previous-matching-node,
|
91
91
|
# matching-node] tuples. Top-level nodes have previous-matching-node set to
|
92
|
-
# nil
|
93
|
-
#
|
94
|
-
# FIXME: Not working right now. Not sure how #edges relates to #pairs
|
92
|
+
# nil. If the filter matches all nodes the value is an edge-representation
|
93
|
+
# of the tree
|
95
94
|
def edges(*filter, this: true, &block)
|
96
95
|
if block_given?
|
97
96
|
each(*filter, this: this) { |node, _, parent| yield parent, node }
|
@@ -100,6 +99,19 @@ module Tree
|
|
100
99
|
end
|
101
100
|
end
|
102
101
|
|
102
|
+
# Return array of [previous-matching-node, matching-node] tuples. Returns
|
103
|
+
# the empty array ff there is no matching set of nodes
|
104
|
+
def pairs(first_matcher_expr, second_matcher_expr, this: true)
|
105
|
+
first_matcher = Matcher.new(first_matcher_expr)
|
106
|
+
second_matcher = Matcher.new(second_matcher_expr)
|
107
|
+
or_matcher = first_matcher | second_matcher # avoids re-computing this value over and over
|
108
|
+
result = []
|
109
|
+
nodes(first_matcher, false, this: this) { |node|
|
110
|
+
node.do_pairs(result, first_matcher, or_matcher)
|
111
|
+
}
|
112
|
+
result
|
113
|
+
end
|
114
|
+
|
103
115
|
# Find nodes matching +filter+ and call +traverse_block+ with a node and a
|
104
116
|
# block argument. +traverse_block+ can recurse into children by calling the
|
105
117
|
# supplied inner block
|
@@ -117,18 +129,6 @@ module Tree
|
|
117
129
|
nodes(filter, this: this).map { |node| traverse_block.call(node, inner_block) }
|
118
130
|
end
|
119
131
|
|
120
|
-
# Return array of [previous-matching-node, matching-node] tuples
|
121
|
-
def pairs(first_matcher_expr, second_matcher_expr, this: true)
|
122
|
-
first_matcher = Matcher.new(first_matcher_expr)
|
123
|
-
second_matcher = Matcher.new(second_matcher_expr)
|
124
|
-
or_matcher = first_matcher | second_matcher # avoids re-computing this value over and over
|
125
|
-
result = []
|
126
|
-
nodes(first_matcher, false, this: this) { |node|
|
127
|
-
node.do_pairs(result, first_matcher, or_matcher)
|
128
|
-
}
|
129
|
-
result
|
130
|
-
end
|
131
|
-
|
132
132
|
# Traverse the tree top-down while accumulating information in an
|
133
133
|
# accumulator object. The block takes a [accumulator, node] tuple and is
|
134
134
|
# responsible for adding itself to the accumulator. The return value from
|
data/lib/modular_tree/version.rb
CHANGED