aboutsummaryrefslogtreecommitdiffstats
path: root/tools/enum_parser/parse_enum/benchmark_build/lib/gen.py
blob: 783ab9f181317b9c76f0b49a5a954ba8333dc09d (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# - * - encoding: UTF-8 - * -

from argparse import ArgumentParser
import random
import sys
import math


def parse_args():
    parser = ArgumentParser(description="")
    parser.add_argument('--range', type=int)
    parser.add_argument('--enum', nargs=2, action="append", metavar=("NAME", "SIZE"))
    parser.add_argument('--namespace', type=str)
    args = parser.parse_args()
    return args


def gen_enum(name, n):
    rg = random.Random(n)
    h1 = list(range(n))
    h2 = list(range(n))
    rg.shuffle(h1)
    rg.shuffle(h2)

    print("enum class %s {" % name)
    for  k, v in zip(h1, h2):
        print("    V%x = 0x%04x," % (k, v))
    print("};")
    print()


def main():
    args = parse_args()

    print("#pragma once\n\n")

    gr = {}
    for name, size in args.enum or []:
        assert name not in gr
        gr[name] = int(size)
    if args.range:
        step = max(int(math.sqrt(args.range)), 1)
        for s in range(args.range, -1, -step):
            gr["EDenseEnum%04d" % s] = s

    if args.namespace:
        print(f"namespace {args.namespace} {{")

    for name, size in sorted(gr.items(), key=lambda kv: -kv[1]):
        gen_enum(name, size)

    if args.namespace:
        print(f"}} // namespace {args.namespace}")

    return 0


if __name__ == '__main__':
    sys.exit(main())