]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_utils.C
small cleanup, doxygen, formatting changes
[lyx.git] / src / mathed / math_utils.C
index cb6da98baf7db601345b84f5b4ccf50bb6248c58..14e43528feb8d05e88693d3e438fbaa99a44c3d8 100644 (file)
@@ -4,20 +4,30 @@
  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
  *  Created:     August 1996
  *  
- *  Copyright: (c) 1996, 1997 Alejandro Aguilar Sierra
+ *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
  *
  *  License: GNU GPL version 2 or later
  */
 
 #include <config.h>
 
-#include <stdlib.h>
+#include <algorithm>
+
 #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;
+};
+
+
+static
+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 +64,35 @@ struct binary_op_pair { short id, isrel; } binary_op_table[] = {
       { LM_ddagger, LMB_OPERATOR }
 };
 
-extern "C" 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;
-}
+
+struct compara {
+       // used by sort and lower_bound
+       inline
+       int operator()(binary_op_pair const & a,
+                      binary_op_pair const & b) const {
+               return a.id < b.id;
+       }
+};
+
 
 int MathedLookupBOP(short id)
 {
-    static int bopCount = sizeof(binary_op_table) / sizeof(binary_op_pair);
-    static bool issorted = false;
+       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;
+       }
 
-    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;
+       binary_op_pair search_elem = { id, 0 };
+       
+       binary_op_pair * res = lower_bound(binary_op_table,
+                                          binary_op_table + bopCount,
+                                          search_elem, compara());
+       if (res != binary_op_table + bopCount && res->id == id)
+               return res->isrel;
+       else
+               return LMB_NONE;
 }
-