X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_utils.C;h=0d9c61c1e4bfaebdf630ea4e514bda72888cdb9a;hb=82fa210ea8d38d51aafb131f10d843e27e056429;hp=a2a25b6ef410b00dc7f79e3062146da126403914;hpb=27de1486ca34aaad446adb798d71a77d6f6304da;p=lyx.git diff --git a/src/mathed/math_utils.C b/src/mathed/math_utils.C index a2a25b6ef4..0d9c61c1e4 100644 --- a/src/mathed/math_utils.C +++ b/src/mathed/math_utils.C @@ -4,20 +4,28 @@ * Author: Alejandro Aguilar Sierra * Created: August 1996 * - * Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra + * Copyright: 1996, 1997 Alejandro Aguilar Sierra * * License: GNU GPL version 2 or later */ #include -#include +#include + #include "math_defs.h" #include "symbol_def.h" +using std::sort; +using std::lower_bound; // This table includes all binary operators and relations -struct binary_op_pair { short id, isrel; } binary_op_table[] = { +struct binary_op_pair { + short id; + short isrel; +}; + +binary_op_pair binary_op_table[] = { { LM_leq, LMB_RELATION }, { LM_geq, LMB_RELATION }, { LM_equiv, LMB_RELATION }, { LM_models, LMB_RELATION }, { LM_prec, LMB_RELATION }, { LM_succ, LMB_RELATION }, @@ -54,34 +62,38 @@ struct binary_op_pair { short id, isrel; } binary_op_table[] = { { LM_ddagger, LMB_OPERATOR } }; -static int compara(const void *a, const void *b) -{ - int i = ((binary_op_pair const *)a)->id, j = ((binary_op_pair const*)b)->id; - return i - j; -} -int MathedLookupBOP(short id) -{ - static int bopCount = sizeof(binary_op_table) / sizeof(binary_op_pair); - static bool issorted = false; +struct compara { + // used by sort + inline + int operator()(binary_op_pair const & a, + binary_op_pair const & b) const { + return a.id < b.id; + } + // used by lower_bound + inline + int operator()(binary_op_pair const & a, short int id) const { + return a.id < id; + } +}; - if (!issorted) { - qsort(binary_op_table, bopCount, sizeof(binary_op_pair), compara); - issorted = true; - } - - int result=0, m, k, l= 0, r = bopCount; - - while (l < r) { - m = (l+r)/2; - k = binary_op_table[m].id - id; - if (k==0) { - result = binary_op_table[m].isrel; - break; - } else - if (k<0) l = m+1; else r = m; - } - return result; +int MathedLookupBOP(short id) +{ + static int const bopCount = + sizeof(binary_op_table) / sizeof(binary_op_pair); + static bool issorted = false; + + if (!issorted) { + sort(binary_op_table, binary_op_table + bopCount, compara()); + issorted = true; + } + + binary_op_pair * res = lower_bound(binary_op_table, + binary_op_table + bopCount, + id, compara()); + if (res != binary_op_table + bopCount && res->id == id) + return res->isrel; + else + return LMB_NONE; } -