7.4.7. Class EdgeSet¶
A notice about memory management: The set objects are automatically destroyed when they are not used anymore.
7.4.7.1. Obtaining an EdgeSet¶
You can create a new, empty edge set by using the constructor of class EdgeSet with 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 from it.
All methods of the RFG interface that return sets of edges return values of class EdgeSet.
Caution
You can insert edges from different graphs into the same EdgeSet instance. Be careful to distinguish the types and attribute descriptors when querying the edges from a mixed graph edge set. The best idea is not to use such mixed graph edge sets at all.
7.4.7.2. Iterating the Edges Contained in an Edge Set¶
__iter__()
Instance method __iter__ returns an iterator object 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 n in some_edge_set:
#...perform your actions on the edges
Caution
You should pay attention not to modify the set you are currently iterating as this may result in unpredicted behavior. If you need to modify a set while iterating over it, make a copy of it and modify the copy while iterating over the original.
7.4.7.3. General Operations¶
__len__()
The instance method __len__ returns the size of the set. Ordinarily, it is used by calling the len function on the set.
__str__()
Instance method __str__ returns a textual representation of the edge set (only for debugging purposes). For user output, use iteration and the attribute handling methods for individual edges.
The two instance methods __eq__ and __ne__ check for equality respectively inequality of two edge sets. The equality is defined by the contents. If two sets contain the same elements, the two sets are equal. Equality does not mean that inserting into the one set also alters the other.
Ordinarily, both functions are called by way of the == and != operators.
Note
The instance methods check if other_set set is a subset or superset of the instance. We delivered both names with and without underscores because the names without underscores are Python naming convention whereas the names with underscores are following the naming conventions of the RFG interface.
clear()
Clears all elements from the set.
7.4.7.4. Membership in the View¶
Instance method __contains__ checks whether the edge passed as parameter is contained in the set. It is usable as in operator as well.
The instance method remove either removes an edge from the set (if parameter edge_or_view is an Edge) or it removes the set from a view (if parameter edge_or_view is a View).
Caution
If you remove an element from the last view it has been contained in, the element is automatically garbage collected immediately. So make sure you first add an element to a target view before removing it from a source view if you intend to transfer the element between two views.
The instance methods insert and add either insert an edge into the set (if the parameter is an Edge) or insert the set into a view (if parameter view is a View).
7.4.7.5. Set Operations¶
The three operators return new sets that contain the intersection, union, difference, or symmetric difference of two sets.
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 it is called for.
7.4.7.6. Querying the Neighborhood of Edges in the Set¶
The parameter view_or_graph has to be either the graph from which the queried edges stem or a view of that graph.
The instance methods attached, sources, and targets all return a node set.
If parameter node_matcher is used, only nodes matching the node matcher are returned.
7.4.7.7. Filtering an Edge Set¶
filter(edge_matcher)
Instance method filter applies the predicate edge_matcher to each edge in the set and inserts all edges matching the predicate into a new set that is passed back as result.