7.4.7. Class EdgeSet¶
Note on memory management: Set objects are automatically destroyed when they are no longer in use.
7.4.7.1. Obtaining an EdgeSet¶
You can create a new, empty edge set by calling the EdgeSet constructor with an empty parameter list. Passing an existing EdgeSet object to the constructor creates an independent copy of the set. You can pass any iterable yielding Edge objects to the constructor to populate the new set.
All RFG interface methods that return sets of edges return EdgeSet objects.
Caution
You can insert edges from different graphs into the same EdgeSet instance. Be careful to distinguish types and attribute descriptors when querying edges from a mixed-graph edge set. It is recommended to avoid using mixed-graph edge sets entirely.
7.4.7.2. Iterating the Edges Contained in an Edge Set¶
__iter__()
The __iter__ instance method returns a generator for all edges contained in the edge set. See the example code in Listing Iterating a set of edges.
from bauhaus import rfg
#...fill some_edge_set
for e in some_edge_set:
... # perform your actions on the edges
Caution
Avoid modifying the set while iterating over it, as this may result in unpredictable behavior. If you need to modify a set during iteration, create a copy of it and modify the copy while iterating over the original.
7.4.7.3. General Operations¶
__len__()
The __len__ instance method returns the size of the set. Typically, this is used by calling the len() function on the set.
__str__()
The __str__ instance method returns a textual representation of the edge set for debugging purposes. For user output, use iteration and the attribute handling methods for individual edges.
The __eq__ and __ne__ instance methods check for equality and inequality, respectively, between two edge sets. Equality is based on content; two sets are equal if they contain the same elements. Equality does not imply that modifying one set will affect the other.
Typically, both functions are called via the == and != operators. The operators
<= and >= check whether the left set is a subset or a superset of the right
set, respectively.
Note
These instance methods check whether the instance is a subset or a superset of other_set, respectively.
clear()
Clears all elements from the set.
7.4.7.4. Membership in the View¶
The __contains__ instance method checks whether the specified edge is contained in the set. It can also be used with the in operator.
The insert and add instance methods insert an edge into the set. The remove instance method removes an edge from the set; it has no effect if the edge is not present in the set.
To insert or remove all edges of a set from a view, pass the set to the add or remove instance methods of the View class.
7.4.7.5. Set Operations¶
These operations return new sets containing the intersection, union, difference, or symmetric difference of two sets. The update instance method adds edges from an EdgeSet or any iterable yielding Edge objects to the instance and returns the instance.
These operator versions are also available:
operator |
function |
operation |
|---|---|---|
s1 & s2 |
__and__ |
s1.intersection(s2) |
s1 | s2 |
__or__, __add__ |
s1.union(s2) |
s1 - s2 |
__sub__ |
s1.difference(s2) |
s1 ^ s2 |
__xor__ |
s1.symmetric_difference(s2) |
copy()
The copy instance method returns a copy of the instance.
pop()
The pop instance method removes an arbitrary edge from the set and returns it. It raises KeyError if the set is empty.
7.4.7.6. Querying the Neighborhood of Edges in the Set¶
The attached, sources, and targets instance methods all return a node set.
If the node_matcher parameter is used, only nodes matching the matcher are returned.
7.4.7.7. Filtering an Edge Set¶
filter(edge_matcher)
The filter instance method applies the edge_matcher predicate to each edge in the set and returns a new set containing all edges that match the predicate.