gen.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. # - * - encoding: UTF-8 - * -
  3. from argparse import ArgumentParser
  4. import random
  5. import sys
  6. import math
  7. def parse_args():
  8. parser = ArgumentParser(description="")
  9. parser.add_argument('--range', type=int)
  10. parser.add_argument('--enum', nargs=2, action="append", metavar=("NAME", "SIZE"))
  11. parser.add_argument('--namespace', type=str)
  12. args = parser.parse_args()
  13. return args
  14. def gen_enum(name, n):
  15. rg = random.Random(n)
  16. h1 = list(range(n))
  17. h2 = list(range(n))
  18. rg.shuffle(h1)
  19. rg.shuffle(h2)
  20. print("enum class %s {" % name)
  21. for k, v in zip(h1, h2):
  22. print(" V%x = 0x%04x," % (k, v))
  23. print("};")
  24. print()
  25. def main():
  26. args = parse_args()
  27. print("#pragma once\n\n")
  28. gr = {}
  29. for name, size in args.enum or []:
  30. assert name not in gr
  31. gr[name] = int(size)
  32. if args.range:
  33. step = max(int(math.sqrt(args.range)), 1)
  34. for s in range(args.range, -1, -step):
  35. gr["EDenseEnum%04d" % s] = s
  36. if args.namespace:
  37. print(f"namespace {args.namespace} {{")
  38. for name, size in sorted(gr.items(), key=lambda kv: -kv[1]):
  39. gen_enum(name, size)
  40. if args.namespace:
  41. print(f"}} // namespace {args.namespace}")
  42. return 0
  43. if __name__ == '__main__':
  44. sys.exit(main())