modular_tree 0.4.0 → 0.6.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 +18 -3
- data/lib/modular_tree/filter.rb +3 -2
- data/lib/modular_tree/implementations.rb +1 -0
- data/lib/modular_tree/matcher.rb +1 -1
- data/lib/modular_tree/pool.rb +2 -0
- data/lib/modular_tree/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6913b33ae629fa98c3832ba7ae313e9948c581a53811d49648233f349b85b64
|
4
|
+
data.tar.gz: eaaec7d8f9f85924c3064c18b5a88c7805baa0c70e2a7db21e798d217348b67f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de1721efbd56c4fe8abdfb54f7d044273daf1938612e4316ef6d1344bc9b1e13bddda02da8f8b0f757d75e45f27a20674b365f28c5ecbc19c53c269217a2d7e5
|
7
|
+
data.tar.gz: a9c68cf3df5204e568a575b1e510fa8974cd39baddead8a52f83edceee865284729a808e8065c898a1e717f2dc7ddfccc1b7a74056c50ff9e368199780c84e29
|
@@ -77,6 +77,21 @@ module Tree
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
+
# Filter children. Doesn't recurse. If a block is given, it should return
|
81
|
+
# truish to select a child node
|
82
|
+
#
|
83
|
+
# TODO: Maybe make #children an Array extended with filtering
|
84
|
+
def choose(match_expr = nil, &block)
|
85
|
+
matcher = Matcher.new(match_expr, &block)
|
86
|
+
if block_given?
|
87
|
+
a = []
|
88
|
+
each_branch { |branch, key| a << branch if matcher.match? branch }
|
89
|
+
a
|
90
|
+
else
|
91
|
+
Enumerator.new { |enum| each_branch { |branch, key| enum << branch if matcher.match? branch } }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
80
95
|
# Like #each but the block is called with node, key, and parent instead of
|
81
96
|
# value, key, and parent
|
82
97
|
def nodes(*filter, this: true, &block) = common_each(*filter, :node, :do_each_preorder, this, &block)
|
@@ -101,9 +116,9 @@ module Tree
|
|
101
116
|
|
102
117
|
# Return array of [previous-matching-node, matching-node] tuples. Returns
|
103
118
|
# the empty array ff there is no matching set of nodes
|
104
|
-
def pairs(
|
105
|
-
first_matcher = Matcher.new(
|
106
|
-
second_matcher = Matcher.new(
|
119
|
+
def pairs(first_match_expr, second_match_expr, this: true)
|
120
|
+
first_matcher = Matcher.new(first_match_expr)
|
121
|
+
second_matcher = Matcher.new(second_match_expr)
|
107
122
|
or_matcher = first_matcher | second_matcher # avoids re-computing this value over and over
|
108
123
|
result = []
|
109
124
|
nodes(first_matcher, false, this: this) { |node|
|
data/lib/modular_tree/filter.rb
CHANGED
@@ -22,7 +22,7 @@ module Tree
|
|
22
22
|
#
|
23
23
|
# when +traverse+ is
|
24
24
|
# true Traverse always. This is the default
|
25
|
-
# false Traverse only if select didn't match
|
25
|
+
# false Traverse only if select didn't match # TODO: Never traverse to be able to use filters on children
|
26
26
|
# nil Expects +select+ to be a Proc object that returns a [select,
|
27
27
|
# traverse] tuple of booleans
|
28
28
|
#
|
@@ -33,7 +33,7 @@ module Tree
|
|
33
33
|
# It is not an error if the method doesn't exists on the a given node but
|
34
34
|
# the node is not selected/traversed. If the expression is a class or an
|
35
35
|
# array of classes, a given node matches if it is an instance of one of the
|
36
|
-
# classes
|
36
|
+
# classes and their subclasses
|
37
37
|
#
|
38
38
|
# If a block is given, it is supposed to return a [select, traverse] tuple
|
39
39
|
# of booleans
|
@@ -42,6 +42,7 @@ module Tree
|
|
42
42
|
# enumerators that doesn't execute the filter unless the enumerator is
|
43
43
|
# evaluated
|
44
44
|
#
|
45
|
+
# TODO: Accept RegExp -> use to_s to match
|
45
46
|
def initialize(select_expr = nil, traverse_expr = nil, &block)
|
46
47
|
if select_expr.nil? && block_given?
|
47
48
|
@matcher = block
|
data/lib/modular_tree/matcher.rb
CHANGED
@@ -29,7 +29,7 @@ module Tree
|
|
29
29
|
|
30
30
|
def initialize(expr = nil, &block)
|
31
31
|
constrain expr, Proc, Symbol, Class, [Class], true, false, nil
|
32
|
-
expr.nil? == block_given? or raise ArgumentError
|
32
|
+
expr.nil? == block_given? or raise ArgumentError, "Expected either an argument or a block"
|
33
33
|
@expr = (expr.nil? ? block : expr)
|
34
34
|
end
|
35
35
|
|
data/lib/modular_tree/pool.rb
CHANGED
data/lib/modular_tree/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modular_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-12-
|
11
|
+
date: 2022-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: constrain
|