modular_tree 0.10.0 → 0.12.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: ae3499e0ea36c7f7dd44068616ec8dd566e43be82c0e728dae86da829b085d7f
4
- data.tar.gz: f1c35152a0b97dad0b50e85cf2061baf9bad2fed42ca4bc2bb8df3b000b81468
3
+ metadata.gz: a78d523f964aaf9e819f8620dbaafbad5facc1848f1baa511bf3e71625a1b0c9
4
+ data.tar.gz: 59e201fe0e71be312e3f1b2545b1dbcfbccb72c7fa04465d33d6da6ed8c14acb
5
5
  SHA512:
6
- metadata.gz: 71c8a99e260fe03d2453371b0afe9bc79c22234fe74a79ddf61036affd1a3129c84597006c9b5648831f1b3f469f79f26f73fd1c6263c277cee3bcea91905480
7
- data.tar.gz: cb5feecaf6a795c91011fbafca901bb1fea9814e9b1283407b623052d9b33b801d1935d9e5594115154384ab2f49763dd5bd672dba1a3b490d800b4a9129bf3b
6
+ metadata.gz: 55c87a6034b013fe0f424dee4d7b2192eb1cd863c9d96085beebfbfbace4efcdaf4ac1137dcd8a1941679479502e4ef3f6b29f2d26ced05aa82f872a28c568cd
7
+ data.tar.gz: f34c14d5cfc770cd2ad502370e17411396fc290aef2988bcccdeb931456add73819a8d7d7a1c5b75565c3565549859e2cbcaeae28960353a2a567bd843841cbf
@@ -69,11 +69,18 @@ module Tree
69
69
 
70
70
  # Implementation of Enumerable#select extended with a single filter. As
71
71
  # #each, the block is called with value, key, and parent arguments
72
- def select(filter = nil, this: true, &block)
72
+ #
73
+ # The match expression can also be a list of classes (instead of an array of classes)
74
+ #
75
+ def select(*expr, this: true, &block)
76
+ !block_given? || expr.empty? or raise ArgumentError, "Can't use both match expression and block"
73
77
  if block_given?
74
- each(block, true, this: this).to_a
78
+ each.select { |branch| yield branch }
79
+ elsif !expr.empty?
80
+ matcher = Matcher.new(*expr, &block)
81
+ each.select { |branch| matcher.match? branch }
75
82
  else
76
- each(filter || true, true, this: this)
83
+ each
77
84
  end
78
85
  end
79
86
 
@@ -83,14 +90,15 @@ module Tree
83
90
  # The match expression can also be a list of classes (instead of an array of classes)
84
91
  #
85
92
  # TODO: Maybe make #children an Array extended with filtering
86
- def choose(*args, &block)
87
- matcher = Matcher.new(*args, &block)
93
+ def choose(*expr, &block)
94
+ !block_given? || expr.empty? or raise ArgumentError, "Can't use both match expression and block"
88
95
  if block_given?
89
- a = []
90
- each_branch { |branch, key| a << branch if matcher.match? branch }
91
- a
96
+ each_branch.select { |branch| yield branch }
97
+ elsif !expr.empty?
98
+ matcher = Matcher.new(*expr, &block)
99
+ each_branch.select { |branch| matcher.match? branch }
92
100
  else
93
- Enumerator.new { |enum| each_branch { |branch, key| enum << branch if matcher.match? branch } }
101
+ each
94
102
  end
95
103
  end
96
104
 
@@ -104,10 +112,14 @@ module Tree
104
112
  # Post-order enumerator of selected nodes
105
113
  def postorder(*filter, this: true) = common_each(*filter, :node_value, :each_postorder, this)
106
114
 
115
+ # TODO IDEA:
116
+ # edges -> parent/child nodes
117
+ # pairs -> parent/.../descendant nodes
118
+
107
119
  # Enumerator of edges in the tree. Edges are [previous-matching-node,
108
120
  # matching-node] tuples. Top-level nodes have previous-matching-node set to
109
- # nil. If the filter matches all nodes the value is an edge-representation
110
- # of the tree
121
+ # nil (the result is a forrest). If the filter matches all nodes the value
122
+ # is an edge-representation of the tree
111
123
  def edges(*filter, this: true, &block)
112
124
  if block_given?
113
125
  each(*filter, this: this) { |node, _, parent| yield parent, node }
@@ -117,7 +129,7 @@ module Tree
117
129
  end
118
130
 
119
131
  # Return array of [previous-matching-node, matching-node] tuples. Returns
120
- # the empty array ff there is no matching set of nodes
132
+ # the empty array iff there is no matching set of nodes
121
133
  def pairs(first_match_expr, second_match_expr, this: true)
122
134
  first_matcher = Matcher.new(first_match_expr)
123
135
  second_matcher = Matcher.new(second_match_expr)
@@ -53,6 +53,12 @@ module Tree
53
53
  include ParentImplementation
54
54
  end
55
55
 
56
+ module InternalRootImplementation
57
+ include RootProperty
58
+ include InternalParentImplementation
59
+ def root = @root ||= parent&.root || self
60
+ end
61
+
56
62
  module ChildrenImplementation
57
63
  include ChildrenProperty
58
64
  include BranchesProperty
@@ -153,6 +159,9 @@ module Tree
153
159
  attr_writer :next_sibling
154
160
  end
155
161
 
162
+ # TODO: A ChildrenArrayImplementation that defines #insert and #append and
163
+ # adds an optional index argument to #attach
164
+
156
165
  module InternalChildrenArrayImplementation
157
166
  include InternalChildrenImplementation
158
167
 
@@ -177,6 +186,8 @@ module Tree
177
186
  # index or an object
178
187
  #
179
188
  # TODO: Rename #attach. #insert & #append are Enumerable operations
189
+ #
190
+ # TODO: Default where argument - insert: 0, append: -1
180
191
  def insert(where, child) = insert_append(:insert, where, child)
181
192
  def append(where, child) = insert_append(:append, where, child)
182
193
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tree
4
- VERSION = "0.10.0"
4
+ VERSION = "0.12.0"
5
5
  end
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.10.0
4
+ version: 0.12.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-18 00:00:00.000000000 Z
11
+ date: 2023-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: constrain