File: create_ladder_graph.py

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.4-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,998,488 kB
  • sloc: cpp: 6,951,470; ansic: 1,486,052; asm: 913,550; python: 232,020; f90: 80,126; objc: 75,349; lisp: 37,276; pascal: 16,990; sh: 9,935; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,164; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (49 lines) | stat: -rwxr-xr-x 1,438 bytes parent folder | download | duplicates (9)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
"""A ladder graph creation program.

This is a python program that creates c source code that will generate
CFGs that are ladder graphs.  Ladder graphs are generally the worst case
for a lot of dominance related algorithms (Dominance frontiers, etc),
and often generate N^2 or worse behavior.

One good use of this program is to test whether your linear time algorithm is
really behaving linearly.
"""

from __future__ import print_function

import argparse


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "rungs", type=int, help="Number of ladder rungs. Must be a multiple of 2"
    )
    args = parser.parse_args()
    if (args.rungs % 2) != 0:
        print("Rungs must be a multiple of 2")
        return
    print("int ladder(int *foo, int *bar, int x) {")
    rung1 = range(0, args.rungs, 2)
    rung2 = range(1, args.rungs, 2)
    for i in rung1:
        print("rung1%d:" % i)
        print("*foo = x++;")
        if i != rung1[-1]:
            print("if (*bar) goto rung1%d;" % (i + 2))
            print("else goto rung2%d;" % (i + 1))
        else:
            print("goto rung2%d;" % (i + 1))
    for i in rung2:
        print("rung2%d:" % i)
        print("*foo = x++;")
        if i != rung2[-1]:
            print("goto rung2%d;" % (i + 2))
        else:
            print("return *foo;")
    print("}")


if __name__ == "__main__":
    main()