| 307 | |
| 308 | |
| 309 | class SampleTreeNode: |
| 310 | |
| 311 | __slots__ = ('parent', 'children', 'uprob') |
| 312 | |
| 313 | def __init__(self, parent=None): |
| 314 | self.parent = parent |
| 315 | self.children = [] |
| 316 | self.uprob = 0 |
| 317 | |
| 318 | def __repr__(self): |
| 319 | return ( |
| 320 | f'SampleTreeNode(uprob={self.uprob}, ' |
| 321 | f'children={[x.uprob for x in self.children]})' |
| 322 | ) |
| 323 | |
| 324 | def __len__(self): |
| 325 | return len(self.children) |
| 326 | |
| 327 | def __bool__(self): |
| 328 | return True |
| 329 | |
| 330 | def append(self, child): |
| 331 | if child.parent: |
| 332 | child.parent.remove(child) |
| 333 | child.parent = self |
| 334 | self.children.append(child) |
| 335 | self.recompute() |
| 336 | |
| 337 | def remove(self, child): |
| 338 | child.parent = None |
| 339 | self.children.remove(child) |
| 340 | self.recompute() |
| 341 | |
| 342 | def recompute(self): |
| 343 | self.uprob = sum(x.uprob for x in self.children) |
| 344 | self.parent and self.parent.recompute() |
| 345 | |
| 346 | |
| 347 | class SampleTreeEntry: |