File: gen-threads.py

package info (click to toggle)
notmuch 0.39-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,104 kB
  • sloc: sh: 21,888; ansic: 14,897; lisp: 9,061; cpp: 7,990; python: 6,221; perl: 391; makefile: 231; javascript: 34; ruby: 13
file content (33 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Generate all possible single-root message thread structures of size
# argv[1].  Each output line is a thread structure, where the n'th
# field is either a number giving the parent of message n or "None"
# for the root.
import sys
from itertools import chain, combinations

def subsets(s):
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

nodes = set(range(int(sys.argv[1])))

# Queue of (tree, free, to_expand) where tree is a {node: parent}
# dictionary, free is a set of unattached nodes, and to_expand is
# itself a queue of nodes in the tree that need to be expanded.
# The queue starts with all single-node trees.
queue = [({root: None}, nodes - {root}, (root,)) for root in nodes]

# Process queue
while queue:
    tree, free, to_expand = queue.pop()

    if len(to_expand) == 0:
        # Only print full-sized trees
        if len(free) == 0:
            print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())])))
    else:
        # Expand node to_expand[0] with each possible set of children
        for children in subsets(free):
            ntree = {child: to_expand[0] for child in children}
            ntree.update(tree)
            nfree = free.difference(children)
            queue.append((ntree, nfree, to_expand[1:] + tuple(children)))