Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code-gen: Simplify switch #2240

Merged
merged 10 commits into from
Dec 18, 2023
43 changes: 31 additions & 12 deletions python/sdist/amici/cxxcodeprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ def get_switch_statement(
indentation_step: Optional[str] = " " * 4,
):
"""
Generate code for switch statement
Generate code for a C++ switch statement.

Generate code for a C++ switch statements with a ``break`` after each case.
dweindl marked this conversation as resolved.
Show resolved Hide resolved

:param condition:
Condition for switch
Expand All @@ -321,26 +323,43 @@ def get_switch_statement(
:return:
Code for switch expression as list of strings
"""
lines = []

if not cases:
return lines
return []

indent0 = indentation_level * indentation_step
indent1 = (indentation_level + 1) * indentation_step
indent2 = (indentation_level + 2) * indentation_step

# try to find redundant statements and collapse those cases
# map statements to case expressions
cases_map: dict[tuple[str, ...], list[str]] = {}
for expression, statements in cases.items():
if statements:
lines.extend(
statement_code = tuple(
[
f"{indent1}case {expression}:",
*(f"{indent2}{statement}" for statement in statements),
f"{indent2}break;",
]
)

if lines:
lines.insert(0, f"{indent0}switch({condition}) {{")
lines.append(indent0 + "}")

return lines
case_code = f"{indent1}case {expression}:"

try:
# there is already a case with the same statement, append
cases_map[statement_code].append(case_code)
except KeyError:
# add new case + statement
cases_map[statement_code] = [case_code]
dweindl marked this conversation as resolved.
Show resolved Hide resolved

if not cases_map:
return []

def get_lines():
for statements, case_code in cases_map.items():
yield from case_code
yield from statements

return [
f"{indent0}switch({condition}) {{",
*(get_lines()),
indent0 + "}",
]
dweindl marked this conversation as resolved.
Show resolved Hide resolved