1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
20
21
24
25
27 """Stores a list of nodes that are linked together."""
28
30 """Initiates a node chain."""
31 self.chain={}
32 self.id=-1
33
35 """Gets a new id for a node in the chain."""
36 self.id+=1
37 return self.id
38
40 """Return a list of all node ids."""
41 return self.chain.keys()
42
43 - def add(self,node,prev=None):
55
69
71 """Kills a node from chain without caring to what it is connected."""
72 if id not in self.chain:
73 raise ChainException('Unknown ID: '+str(id))
74 else:
75 del self.chain[id]
76
78 """Disconnects node from his predecessor."""
79 if id not in self.chain:
80 raise ChainException('Unknown ID: '+str(id))
81 else:
82 prev_id=self.chain[id].prev
83 if prev_id is not None:
84 self.chain[prev_id].succ.pop(self.chain[prev_id].succ.index(id))
85 self.chain[id].prev=None
86 return prev_id
87
88 - def link(self, parent,child):
89 """Connects son to parent."""
90 if child not in self.chain:
91 raise ChainException('Unknown ID: '+str(child))
92 elif parent not in self.chain:
93 raise ChainException('Unknown ID: '+str(parent))
94 else:
95 self.unlink(child)
96 self.chain[parent].succ.append(child)
97 self.chain[child].set_prev(parent)
98
100 """Check if grandchild is a subnode of parent."""
101 if grandchild==parent or grandchild in self.chain[parent].get_succ():
102 return True
103 else:
104 for sn in self.chain[parent].get_succ():
105 if self.is_parent_of(sn,grandchild):
106 return True
107 else:
108 return False
109
110 - def trace(self,start,finish):
111 """Returns a list of all node_ids between two nodes (excluding start, including end)."""
112 if start not in self.chain or finish not in self.chain:
113 raise NodeException('Unknown node.')
114 if not self.is_parent_of(start,finish) or start==finish:
115 return []
116 for sn in self.chain[start].get_succ():
117 if self.is_parent_of(sn,finish):
118 return [sn]+self.trace(sn,finish)
119
120
122 """A single node."""
123
125 """Represents a node with one predecessor and multiple successors."""
126 self.id=None
127 self.data=data
128 self.prev=None
129 self.succ=[]
130
132 """Sets the id of a node, if not set yet."""
133 if self.id is not None:
134 raise NodeException('Node id cannot be changed.')
135 self.id=id
136
138 """Returns the node's id."""
139 return self.id
140
142 """Returns a list of the node's successors."""
143 return self.succ
144
146 """Returns the id of the node's predecessor."""
147 return self.prev
148
150 """Adds a node id to the node's successors."""
151 if isinstance(id,type([])):
152 self.succ.extend(id)
153 else:
154 self.succ.append(id)
155
157 """Removes a node id from the node's successors."""
158 self.succ.remove(id)
159
161 """Sets the node's successors."""
162 if not isinstance(new_succ,type([])):
163 raise NodeException('Node successor must be of list type.')
164 self.succ=new_succ
165
167 """Sets the node's predecessor."""
168 self.prev=id
169
171 """Returns a node's data."""
172 return self.data
173
175 """Sets a node's data."""
176 self.data=data
177