From addd4d71faf45d62e5a0b2f7577d5c7a56dbe5e9 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 12 Oct 2022 13:46:07 -0700 Subject: [PATCH] util/indicies: use itertools.product instead of deeply nested loops Reviewed-by: Erik Faye-Lund Reviewed-by: Eric Engestrom Part-of: --- src/util/indices/u_indices_gen.py | 56 ++++++++++++++----------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/util/indices/u_indices_gen.py b/src/util/indices/u_indices_gen.py index 8f93b340aef..ef1b207e252 100644 --- a/src/util/indices/u_indices_gen.py +++ b/src/util/indices/u_indices_gen.py @@ -24,6 +24,8 @@ copyright = ''' */ ''' +import itertools + GENERATE, UBYTE, USHORT, UINT = 'generate', 'ubyte', 'ushort', 'uint' FIRST, LAST = 'first', 'last' PRDISABLE, PRENABLE = 'prdisable', 'prenable' @@ -190,9 +192,8 @@ def postamble(): def prim_restart(in_verts, out_verts, out_prims, close_func = None): print('restart:') print(' if (i + ' + str(in_verts) + ' > in_nr) {') - for i in range(out_prims): - for j in range(out_verts): - print(' (out+j+' + str(out_verts * i) + ')[' + str(j) + '] = restart_index;') + for i, j in itertools.product(range(out_prims), range(out_verts)): + print(' (out+j+' + str(out_verts * i) + ')[' + str(j) + '] = restart_index;') print(' continue;') print(' }') for i in range(in_verts): @@ -366,27 +367,24 @@ def tristripadj(intype, outtype, inpv, outpv, pr): def emit_funcs(): - for intype in INTYPES: - for outtype in OUTTYPES: - for inpv in (FIRST, LAST): - for outpv in (FIRST, LAST): - for pr in (PRDISABLE, PRENABLE): - if pr == PRENABLE and intype == GENERATE: - continue - points(intype, outtype, inpv, outpv, pr) - lines(intype, outtype, inpv, outpv, pr) - linestrip(intype, outtype, inpv, outpv, pr) - lineloop(intype, outtype, inpv, outpv, pr) - tris(intype, outtype, inpv, outpv, pr) - tristrip(intype, outtype, inpv, outpv, pr) - trifan(intype, outtype, inpv, outpv, pr) - quads(intype, outtype, inpv, outpv, pr) - quadstrip(intype, outtype, inpv, outpv, pr) - polygon(intype, outtype, inpv, outpv, pr) - linesadj(intype, outtype, inpv, outpv, pr) - linestripadj(intype, outtype, inpv, outpv, pr) - trisadj(intype, outtype, inpv, outpv, pr) - tristripadj(intype, outtype, inpv, outpv, pr) + for intype, outtype, inpv, outpv, pr in itertools.product( + INTYPES, OUTTYPES, [FIRST, LAST], [FIRST, LAST], [PRDISABLE, PRENABLE]): + if pr == PRENABLE and intype == GENERATE: + continue + points(intype, outtype, inpv, outpv, pr) + lines(intype, outtype, inpv, outpv, pr) + linestrip(intype, outtype, inpv, outpv, pr) + lineloop(intype, outtype, inpv, outpv, pr) + tris(intype, outtype, inpv, outpv, pr) + tristrip(intype, outtype, inpv, outpv, pr) + trifan(intype, outtype, inpv, outpv, pr) + quads(intype, outtype, inpv, outpv, pr) + quadstrip(intype, outtype, inpv, outpv, pr) + polygon(intype, outtype, inpv, outpv, pr) + linesadj(intype, outtype, inpv, outpv, pr) + linestripadj(intype, outtype, inpv, outpv, pr) + trisadj(intype, outtype, inpv, outpv, pr) + tristripadj(intype, outtype, inpv, outpv, pr) def init(intype, outtype, inpv, outpv, pr, prim): if intype == GENERATE: @@ -408,13 +406,9 @@ def init(intype, outtype, inpv, outpv, pr, prim): def emit_all_inits(): - for intype in INTYPES: - for outtype in OUTTYPES: - for inpv in PVS: - for outpv in PVS: - for pr in PRS: - for prim in PRIMS: - init(intype, outtype, inpv, outpv, pr, prim) + for intype, outtype, inpv, outpv, pr, prim in itertools.product( + INTYPES, OUTTYPES, PVS, PVS, PRS, PRIMS): + init(intype, outtype, inpv, outpv, pr, prim) def emit_init(): print('void u_index_init( void )')