@@ -162,38 +162,22 @@ def map(self, fn: Callable, filter: Optional[Any] = None) -> dict[Node, Any]:
162
162
results [node ] = fn (node , results , ** kwargs )
163
163
return results
164
164
165
- def find (self , type : type | tuple [type ], filter : Optional [Any ] = None ) -> set [Node ]:
166
- """Find all nodes of a given type in the graph.
167
-
168
- Parameters
169
- ----------
170
- type
171
- Type or tuple of types to find.
172
- filter
173
- Pattern-like object to filter out nodes from the traversal. The traversal
174
- will only visit nodes that match the given pattern and stop otherwise.
175
-
176
- Returns
177
- -------
178
- The set of nodes matching the given type.
179
- """
180
- nodes = Graph .from_bfs (self , filter = filter ).nodes ()
181
- return {node for node in nodes if isinstance (node , type )}
182
-
183
- @experimental
184
- def match (
185
- self , pat : Any , filter : Optional [Any ] = None , context : Optional [dict ] = None
186
- ) -> set [Node ]:
187
- """Find all nodes matching a given pattern in the graph.
188
-
189
- A more advanced version of find, this method allows to match nodes based on
190
- the more flexible pattern matching system implemented in the pattern module.
165
+ def find (
166
+ self ,
167
+ pat : type | tuple [type ],
168
+ filter : Optional [Any ] = None ,
169
+ context : Optional [dict ] = None ,
170
+ ) -> list [Node ]:
171
+ """Find all nodes matching a given pattern or type in the graph.
172
+
173
+ Allow to match nodes based on the flexible pattern matching system implemented
174
+ in the pattern module, but also provide a fast path for matching based on the
175
+ type of the node.
191
176
192
177
Parameters
193
178
----------
194
179
pat
195
- Pattern to match. `ibis.common.pattern()` function is used to coerce the
196
- input value into a pattern. See the pattern module for more details.
180
+ Python type or `Pattern` to match.
197
181
filter
198
182
Pattern-like object to filter out nodes from the traversal. The traversal
199
183
will only visit nodes that match the given pattern and stop otherwise.
@@ -202,12 +186,16 @@ def match(
202
186
203
187
Returns
204
188
-------
205
- The set of nodes matching the given pattern.
189
+ The list of nodes matching the given pattern. The order of the nodes is
190
+ determined by a breadth-first search.
206
191
"""
207
- pat = pattern (pat )
208
- ctx = context or {}
209
192
nodes = Graph .from_bfs (self , filter = filter ).nodes ()
210
- return {node for node in nodes if pat .match (node , ctx ) is not NoMatch }
193
+ if isinstance (pat , (tuple , type )):
194
+ return [node for node in nodes if isinstance (node , pat )]
195
+ else :
196
+ pat = pattern (pat )
197
+ ctx = context or {}
198
+ return [node for node in nodes if pat .match (node , ctx ) is not NoMatch ]
211
199
212
200
@experimental
213
201
def replace (
0 commit comments