]> git.notmuchmail.org Git - notmuch/blob - test/gen-threads.py
Merge tag '0.18.2'
[notmuch] / test / gen-threads.py
1 # Generate all possible single-root message thread structures of size
2 # argv[1].  Each output line is a thread structure, where the n'th
3 # field is either a number giving the parent of message n or "None"
4 # for the root.
5
6 import sys
7 from itertools import chain, combinations
8
9 def subsets(s):
10     return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
11
12 nodes = set(range(int(sys.argv[1])))
13
14 # Queue of (tree, free, to_expand) where tree is a {node: parent}
15 # dictionary, free is a set of unattached nodes, and to_expand is
16 # itself a queue of nodes in the tree that need to be expanded.
17 # The queue starts with all single-node trees.
18 queue = [({root: None}, nodes - {root}, (root,)) for root in nodes]
19
20 # Process queue
21 while queue:
22     tree, free, to_expand = queue.pop()
23
24     if len(to_expand) == 0:
25         # Only print full-sized trees
26         if len(free) == 0:
27             print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())])))
28     else:
29         # Expand node to_expand[0] with each possible set of children
30         for children in subsets(free):
31             ntree = dict(tree, **{child: to_expand[0] for child in children})
32             nfree = free.difference(children)
33             queue.append((ntree, nfree, to_expand[1:] + tuple(children)))