modular_tree 0.11.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: c0947ea53f44cfdfdf8c615c26217fc7bfd86b9fae98f6c234519ace576f911d
4
- data.tar.gz: 6dc9685845668b34db4724828f777e80d1e808557c1e8b63988f23a82fdccccf
3
+ metadata.gz: a78d523f964aaf9e819f8620dbaafbad5facc1848f1baa511bf3e71625a1b0c9
4
+ data.tar.gz: 59e201fe0e71be312e3f1b2545b1dbcfbccb72c7fa04465d33d6da6ed8c14acb
5
5
  SHA512:
6
- metadata.gz: e18eeb61df2ec711d51501175409ce1b085dc86d01c65ae3863f9abf4c72ff2e051dfc070541652acfdc6515859a2b7e0cf9c11ea665c5d1f9146c83ad881701
7
- data.tar.gz: b7f8dfbd482c7c64dd8f52f52d17d2ad2b4e82ee5e0604adbc790aa576ade55c0e26456df207938656ef5314fd4601809797669c11a488ba89d861e020bee905
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)
@@ -159,6 +159,9 @@ module Tree
159
159
  attr_writer :next_sibling
160
160
  end
161
161
 
162
+ # TODO: A ChildrenArrayImplementation that defines #insert and #append and
163
+ # adds an optional index argument to #attach
164
+
162
165
  module InternalChildrenArrayImplementation
163
166
  include InternalChildrenImplementation
164
167
 
@@ -183,6 +186,8 @@ module Tree
183
186
  # index or an object
184
187
  #
185
188
  # TODO: Rename #attach. #insert & #append are Enumerable operations
189
+ #
190
+ # TODO: Default where argument - insert: 0, append: -1
186
191
  def insert(where, child) = insert_append(:insert, where, child)
187
192
  def append(where, child) = insert_append(:append, where, child)
188
193
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tree
4
- VERSION = "0.11.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.11.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-23 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