From a5e14ca7cf7623c1a70145000ef416ea7b1db716 Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Fri, 23 Sep 2022 10:12:56 -0400 Subject: [PATCH] Use a switch statement instead of an array This makes processing of opCodes faster --- src/CalcManager/CEngine/scicomm.cpp | 34 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/CalcManager/CEngine/scicomm.cpp b/src/CalcManager/CEngine/scicomm.cpp index 4d671fbd..dcf1c44f 100644 --- a/src/CalcManager/CEngine/scicomm.cpp +++ b/src/CalcManager/CEngine/scicomm.cpp @@ -30,17 +30,31 @@ namespace // 0 is returned. Higher the number, higher the precedence of the operator. int NPrecedenceOfOp(int nopCode) { - static uint16_t rgbPrec[] = { 0, 0, IDC_OR, 0, IDC_XOR, 0, IDC_AND, 1, IDC_NAND, 1, IDC_NOR, 1, IDC_ADD, 2, IDC_SUB, 2, IDC_RSHF, 3, - IDC_LSHF, 3, IDC_RSHFL, 3, IDC_MOD, 3, IDC_DIV, 3, IDC_MUL, 3, IDC_PWR, 4, IDC_ROOT, 4, IDC_LOGBASEY, 4 }; - - for (unsigned int iPrec = 0; iPrec < size(rgbPrec); iPrec += 2) + switch (nopCode) { - if (nopCode == rgbPrec[iPrec]) - { - return rgbPrec[iPrec + 1]; - } + default: + case IDC_OR: + case IDC_XOR: + return 0; + case IDC_AND: + case IDC_NAND: + case IDC_NOR: + return 1; + case IDC_ADD: + case IDC_SUB: + return 2; + case IDC_LSHF: + case IDC_RSHF: + case IDC_RSHFL: + case IDC_MOD: + case IDC_DIV: + case IDC_MUL: + return 3; + case IDC_PWR: + case IDC_ROOT: + case IDC_LOGBASEY: + return 4; } - return 0; } } @@ -518,7 +532,7 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) if (wParam == IDC_OPENP) { - // if there's an omitted multiplication sign + // if there's an omitted multiplication sign if (IsDigitOpCode(m_nLastCom) || IsUnaryOpCode(m_nLastCom) || m_nLastCom == IDC_PNT || m_nLastCom == IDC_CLOSEP) { ProcessCommand(IDC_MUL);