Reputation: 111
I'm trying to optimize my maze generation algorithm. At the moment i have a list of sets of nodes and a list of nodes themselves. Nodes are stored as (x,y) tuples. At the beginning each set contains only one node. I pick a random border between two nodes and check if they are in the same set. Here's the problem - i have to iterate through the list of sets and look at every single item until i find the set which contains given node/nodes. I want to be able to access sets as a property of Node class, but also i want my sets to contain objects of "Node" class and i run into this:
class Node:
def __init__(self, xy:tuple, group:set):
self.xy = xy
self.group = group
node = Node((10, 10),{Node(10, 10),{Node(10, 10),{... and so on }}})
How do i create such a relation so that i can access sets as node.group and at the same time group property would point to the needed set with other Node objects without recursion?
Upvotes: 2
Views: 61
Reputation: 26667
Is this what you wanted ?
class Node:
def __init__(self, xy:tuple):
self.xy = xy
self.group = None
def set_group(self, group:set):
if self.group is not None:
self.group.remove(self)
group.add(self)
self.group = group
node1 = Node((1,1))
node2 = Node((2,2))
group1 = set()
group2 = set()
node1.set_group(group1)
node2.set_group(group2)
Upvotes: 1