]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_hash.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / math_hash.C
index 6f48b5932269e07e299f8f5dbe8ca32635714510..51b70065cb0610572b518c7986b5be0848aab71d 100644 (file)
@@ -1,48 +1,15 @@
-/* C code produced by gperf version 2.5 (GNU C++ version) */
-/* Command-line: gperf -a -p -o -t -G -D keywords  */
 #include <config.h>
+#include <map>
 
 #include "math_defs.h"
 #include "math_parser.h"
 #include "support/lstrings.h"
+#include <iostream>
 
-int const TOTAL_KEYWORDS = 269;
-int const MIN_WORD_LENGTH = 2;
-int const MAX_WORD_LENGTH = 18;
-int const MIN_HASH_VALUE = 8;
-int const MAX_HASH_VALUE = 490;
+namespace {
 
-/* maximum key range = 483, duplicates = 40 */
-
-static
-unsigned int
-math_hash (register char const * str, register int len)
-{
-       static unsigned short asso_values[] = 
-       {
-               491, 491, 491, 491, 491, 491, 491, 491, 491, 491,
-               491, 491, 491, 491, 491, 491, 491, 491, 491, 491,
-               491, 491, 491, 491, 491, 491, 491, 491, 491, 491,
-               491, 491, 491, 491, 491, 491, 491, 491, 491, 491,
-               491, 491, 491, 491, 491, 491, 491, 491, 491, 491,
-               491, 491, 491, 491, 491, 491, 491, 491, 491, 491,
-               491, 491, 491, 491, 491, 491, 491, 491,  95, 491,
-               491,  45, 491,  10, 491, 491, 255, 491, 491,  65,
-               30, 491,   0,  70,  80,  70,  15, 491,  10, 491,
-               491, 491, 491, 491, 491, 491, 491,  50, 200, 140,
-               219,  35, 135, 237, 230,  69,   0,  20,  10, 180,
-               105, 247,  55, 208, 125,   0,  25,  42, 255,  75,
-               5,   0,   0, 491, 491, 491, 491, 491,
-       };
-       return len + asso_values[str[len - 1]] + asso_values[str[0]];
-}
-
-
-static
 latexkeys const wordlist[] = 
 {
-       {"",0,0}, {"",0,0}, {"",0,0}, {"",0,0},
-       {"",0,0}, {"",0,0}, {"",0,0}, {"",0,0}, 
        {"setminus",  LM_TK_SYM, LM_setminus},
        {"ldots",  LM_TK_DOTS, LM_ldots},
        {"limits",  LM_TK_LIMIT, 1 },
@@ -60,11 +27,11 @@ latexkeys const wordlist[] =
        {"smile",  LM_TK_SYM, LM_smile},
        {"exists",  LM_TK_SYM, LM_exists},
        {"Vert",  LM_TK_SYM, LM_Vert},
-       {"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
+       //{"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
        {"uplus",  LM_TK_SYM, LM_uplus},
        {"ell",  LM_TK_SYM, LM_ell},
        {"langle",  LM_TK_SYM, LM_langle},
-       {"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
+       //{"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
        {"zeta",  LM_TK_SYM, LM_zeta},
        {"sigma",  LM_TK_SYM, LM_sigma},
        {"arccos",  LM_TK_FUNC, 0},
@@ -75,11 +42,11 @@ latexkeys const wordlist[] =
        {"approx",  LM_TK_SYM, LM_approx},
        {"triangleleft",  LM_TK_SYM, LM_triangleleft},
        {"triangleright",  LM_TK_SYM, LM_triangleright},
-       {"tilde",  LM_TK_ACCENT, LM_tilde},
+       {"tilde",  LM_TK_DECORATION, LM_tilde},
        {"lambda",  LM_TK_SYM, LM_lambda},
        {"emptyset",  LM_TK_MACRO, LM_emptyset},
        {"triangle",  LM_TK_SYM, LM_triangle},
-       {"textstyle",  LM_TK_STY, LM_ST_TEXT},
+       //{"textstyle",  LM_TK_STY, LM_ST_TEXT},
        {"tau",  LM_TK_SYM, LM_tau},
        {"limsup",  LM_TK_FUNCLIM, 0},
        {"partial",  LM_TK_SYM, LM_partial},
@@ -94,10 +61,10 @@ latexkeys const wordlist[] =
        {"swarrow",  LM_TK_SYM, LM_swarrow},
        {"top",  LM_TK_SYM, LM_top},
        {"Rightarrow",  LM_TK_SYM, LM_Rightarrow},
-       {"underline",  LM_TK_WIDE, LM_underline},
-       {"underbrace",  LM_TK_WIDE, LM_underbrace},
+       {"underline",  LM_TK_DECORATION, LM_underline},
+       {"underbrace",  LM_TK_DECORATION, LM_underbrace},
        {"eta",  LM_TK_SYM, LM_eta},
-       {"acute",  LM_TK_ACCENT, LM_acute},
+       {"acute",  LM_TK_DECORATION, LM_acute},
        {"angle",  LM_TK_SYM, LM_angle},
        {"exp",  LM_TK_FUNC, 0},
        {"leftarrow",  LM_TK_SYM, LM_leftarrow},
@@ -112,14 +79,14 @@ latexkeys const wordlist[] =
        {"Psi",  LM_TK_SYM, LM_Psi},
        {"longleftrightarrow",  LM_TK_SYM, LM_longleftrightarrow},
        {"alpha",  LM_TK_SYM, LM_alpha},
-       {"widehat",  LM_TK_WIDE, LM_widehat},
+       {"widehat",  LM_TK_DECORATION, LM_widehat},
        {"sin",  LM_TK_FUNC, 0},
        {"asymp",  LM_TK_SYM, LM_asymp},
        {"nolimits",  LM_TK_LIMIT, 0 },
        {"perp",  LM_TK_MACRO, LM_perp},
        {"wedge",  LM_TK_SYM, LM_wedge},
        {"ln",  LM_TK_FUNC, 0},
-       {"widetilde",  LM_TK_WIDE, LM_widetilde},
+       {"widetilde",  LM_TK_DECORATION, LM_widetilde},
        {"Omega",  LM_TK_SYM, LM_Omega},
        {"natural",  LM_TK_SYM, LM_natural},
        {"iota",  LM_TK_SYM, LM_iota},
@@ -132,7 +99,7 @@ latexkeys const wordlist[] =
        {"star",  LM_TK_SYM, LM_star},
        {"leftharpoondown",  LM_TK_SYM, LM_leftharpoondown},
        {"wp",  LM_TK_SYM, LM_wp},
-       {"not",  LM_TK_ACCENT, LM_not},
+       {"not",  LM_TK_DECORATION, LM_not},
        {"tan",  LM_TK_FUNC, 0},
        {"Theta",  LM_TK_SYM, LM_Theta},
        {"rceil",  LM_TK_SYM, LM_rceil},
@@ -157,7 +124,7 @@ latexkeys const wordlist[] =
        {"arcsin",  LM_TK_FUNC, 0},
        {"arctan",  LM_TK_FUNC, 0},
        {"flat",  LM_TK_SYM, LM_flat},
-       {"check",  LM_TK_ACCENT, LM_check},
+       {"check",  LM_TK_DECORATION, LM_check},
        {"rangle",  LM_TK_SYM, LM_rangle},
        {"cot",  LM_TK_FUNC, 0},
        {"cdot",  LM_TK_SYM, LM_cdot},
@@ -209,15 +176,15 @@ latexkeys const wordlist[] =
        {"mp",  LM_TK_SYM, LM_mp},
        {"pm",  LM_TK_SYM, LM_pm},
        {"nonumber",  LM_TK_NONUM, 0},
-       {"breve",  LM_TK_ACCENT, LM_breve},
+       {"breve",  LM_TK_DECORATION, LM_breve},
        {"bigvee",  LM_TK_BIGSYM, LM_vee},
        {"bowtie",  LM_TK_SYM, LM_bowtie},
        {"bigwedge",  LM_TK_BIGSYM, LM_wedge},
        {"frown",  LM_TK_SYM, LM_frown},
        {"rightharpoondown",  LM_TK_SYM, LM_rightharpoondown},
        {"det",  LM_TK_FUNCLIM, 0},
-       {"dot",  LM_TK_ACCENT, LM_dot},
-       {"ddot",  LM_TK_ACCENT, LM_ddot},
+       {"dot",  LM_TK_DECORATION, LM_dot},
+       {"ddot",  LM_TK_DECORATION, LM_ddot},
        {"lg",  LM_TK_FUNC, 0},
        {"log",  LM_TK_FUNC, 0},
        {"oplus",  LM_TK_SYM, LM_oplus},
@@ -227,21 +194,21 @@ latexkeys const wordlist[] =
        {"diamondsuit",  LM_TK_SYM, LM_diamondsuit},
        {"rfloor",  LM_TK_SYM, LM_rfloor},
        {"end",  LM_TK_END, 0},
-       {"hat",  LM_TK_ACCENT, LM_hat},
+       {"hat",  LM_TK_DECORATION, LM_hat},
        {"tanh",  LM_TK_FUNC, 0},
        {"vdots",  LM_TK_DOTS, LM_vdots},
        {"bigcap",  LM_TK_BIGSYM, LM_cap},
        {"bigcup",  LM_TK_BIGSYM, LM_cup},
        {"bigsqcup",  LM_TK_BIGSYM, LM_bigsqcup},
        {"heartsuit",  LM_TK_SYM, LM_heartsuit},
-       {"displaystyle",  LM_TK_STY, LM_ST_DISPLAY},
+       //{"displaystyle",  LM_TK_STY, LM_ST_DISPLAY},
        {"longmapsto",  LM_TK_SYM, LM_longmapsto},
        {"bigtriangleup",  LM_TK_SYM, LM_bigtriangleup},
        {"preceq",  LM_TK_SYM, LM_preceq},
        {"delta",  LM_TK_SYM, LM_delta},
        {"odot",  LM_TK_SYM, LM_odot},
        {"oint",  LM_TK_BIGSYM, LM_oint},
-       {"grave",  LM_TK_ACCENT, LM_grave},
+       {"grave",  LM_TK_DECORATION, LM_grave},
        {"pmod",  LM_TK_PMOD, 0},
        {"prod",  LM_TK_BIGSYM, LM_prod},
        {"frac",  LM_TK_FRAC, 0},
@@ -249,9 +216,9 @@ latexkeys const wordlist[] =
        {"circ",  LM_TK_SYM, LM_circ},
        {"aleph",  LM_TK_SYM, LM_aleph},
        {"min",  LM_TK_FUNCLIM, 0},
-       {"overline",  LM_TK_WIDE, LM_overline},
+       {"overline",  LM_TK_DECORATION, LM_overline},
        {"arg",  LM_TK_FUNC, 0},
-       {"overbrace",  LM_TK_WIDE, LM_overbrace},
+       {"overbrace",  LM_TK_DECORATION, LM_overbrace},
        {"amalg",  LM_TK_SYM, LM_amalg},
        {"gamma",  LM_TK_SYM, LM_gamma},
        {"vee",  LM_TK_SYM, LM_vee},
@@ -270,12 +237,12 @@ latexkeys const wordlist[] =
        {"bigtriangledown",  LM_TK_SYM, LM_bigtriangledown},
        {"mathbf",  LM_TK_FONT, LM_TC_BF},
        {"mathsf",  LM_TK_FONT, LM_TC_SF},
-       {"bar",  LM_TK_ACCENT, LM_bar},
+       {"bar",  LM_TK_DECORATION, LM_bar},
        {"varpi",  LM_TK_SYM, LM_varpi},
        {"varphi",  LM_TK_SYM, LM_varphi},
        {"newcommand",  LM_TK_NEWCOMMAND, 0 },
-       {"overleftarrow",  LM_TK_WIDE, LM_overleftarrow},
-       {"overrightarrow",  LM_TK_WIDE, LM_overightarrow},
+       {"overleftarrow",  LM_TK_DECORATION, LM_overleftarrow},
+       {"overrightarrow",  LM_TK_DECORATION, LM_overightarrow},
        {"Leftarrow",  LM_TK_SYM, LM_Leftarrow},
        {"Longleftarrow",  LM_TK_SYM, LM_Longleftarrow},
        {"Leftrightarrow",  LM_TK_SYM, LM_Leftrightarrow},
@@ -293,7 +260,7 @@ latexkeys const wordlist[] =
        {"coth",  LM_TK_FUNC, 0},
        {"rho",  LM_TK_SYM, LM_rho},
        {"cong",  LM_TK_SYM, LM_cong},
-       {"vec",  LM_TK_ACCENT, LM_vec},
+       {"vec",  LM_TK_DECORATION, LM_vec},
        {"dim",  LM_TK_FUNC, 0},
        {"mid",  LM_TK_SYM, LM_mid},
        {"hom",  LM_TK_FUNC, 0},
@@ -314,102 +281,66 @@ latexkeys const wordlist[] =
        {"vdash",  LM_TK_SYM, LM_vdash},
 };
 
-static short lookup[] = 
-{
-        -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   8,  -1,  -1,  -1,  -1,  -1,
-        -1,   9,  10,  -1,  11,  -1,  -1,  -1,  12,  -1,  -1, 491, -13,  -2,
-        -1,  15,  16, 491, -17,  -2,  19,  -1,  -1,  20,  -1,  21,  22,  23,
-        -1,  -1,  24,  -1,  25,  26,  27,  -1,  -1,  28,  29,  -1,  30,  31,
-        32,  -1,  33,  -1, 635,  37,  38,  39,  -1,  40,  41,  -1, 617,  44,
-        45,  46,  47,  48,  49,  50,  51,  -1,  52,  -1,  53,  54, 588,  57,
-        -1,  58,  59,  60,  61,  -1, 491, -62,  -2,  64,  65,  66,  -1,  67,
-        68, 565,  71,  72, 499,  75,  -1,  76,  -1,  77,  78,  -1,  79, -73,
-        -2,  80,  81,  82,  -1,  83,  -1,  84,  85,  -1,  86,  87,  88,  89,
-        90, 501,  93,  94,  95,  -1,  96, 493,  -1,  99, -97,  -2, -91,  -2,
-       100, 101, 102, 517, 105, 106,  -1, 107, 108, 109, 110, 497, 113,  -1,
-       114, 115, 116, 117,-111,  -2, 118, 491,-119,  -2, 121, 122, 123,  -1,
-       124, 125,-103,  -2,  -1, 126, -69,  -2, 491,-127,  -2, 129, -55,  -2,
-       130, 491,-131,  -2, 133, 492, 136,-134,  -2,  -1, 137, 138, 139, -42,
-        -2, 140, 495, 143, 144,  -1, 145,-141,  -2, -34,  -3, 146, 613, 149,
-       150, 605, 154, 155, 156, 157, 503,  -1, 491,-160,  -2, 162,  -1, 163,
-       491,-164,  -2,  -1, 166,-158,  -2, 167, 168,  -1, 169, 170,  -1, 577,
-       173,  -1, 174, 554,  -1, 177,  -1, 178, 179, 543, 182, 183, 184,  -1,
-       185, 509, 188, 189, 190, 191, 192, 193, 194, 499,  -1, 197, 198,  -1,
-       199, 200, 201, 202,-195,  -2,-186,  -2, 203,  -1, 500, 206, 492, 209,
-       -207,  -2,  -1, 210, 211, 212,-204,  -2, 213,  -1, 498, 216, 494, 219,
-        -1, 220,-217,  -2,-214,  -2,-180,  -2, 221, 222, 223,-175,  -2,  -1,
-       224,  -1, 225, 226,  -1, 491,-227,  -2, 229,  -1, 230, 231, 232, 491,
-       -233,  -2,-171,  -2,-151,  -3, 235, 236, 237,-147,  -2,  -1, 238, 239,
-       240,  -1,  -1, 241,  -1,  -1,  -1, 242, 498, 245,  -1, 246, 247,  -1,
-       248, 249,-243,  -2,  -1,  -1,  -1,  -1,  -1, 250,  -1,  -1,  -1,  -1,
-        -1, 251, 252,  -1,  -1,  -1, 253,  -1,  -1,  -1, 492, 256,-254,  -2,
-        -1,  -1,  -1, 257,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
-        -1,  -1,  -1,  -1,  -1,  -1, 258,  -1,  -1,  -1, 491,-259,  -2,  -1,
-        -1,  -1,  -1,  -1,  -1,  -1,  -1, 261,  -1,  -1,  -1,  -1,  -1,  -1,
-        -1,  -1,  -1, 262,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 263, 492, 266,
-       -264,  -2,  -1,  -1,  -1, 267,  -1,  -1,  -1,  -1,  -1, 268,  -1,  -1,
-       269,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1, 491,-270,  -2,
-        -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
-       272, 273,  -1, 274,  -1,  -1,  -1, 275,  -1,  -1,  -1,  -1,  -1,  -1,
-       276,
+
+struct symbolindex {
+       unsigned int id;
+       short token;
+
+       symbolindex(unsigned int i, short t) : id(i), token(t)
+       {}
+
+       bool operator<(symbolindex const & s) const
+       {
+               return (id < s.id) || (id == s.id && token < s.token);
+       }
 };
 
 
-latexkeys const *
-in_word_set (register char const * str, register int len)
-{
-       if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) {
-               int const key = math_hash (str, len);
-               
-               if (key <= MAX_HASH_VALUE && key >= 0) {
-                       int const idx = lookup[key];
-                       
-                       if (idx >= 0 && idx < MAX_HASH_VALUE) {
-                               char const * s = wordlist[idx].name;
-                               
-                               if (*s == *str && !compare(str + 1, s + 1))
-                                       return &wordlist[idx];
-                       } else if (idx < 0 && idx >= -MAX_HASH_VALUE) {
-                               return 0;
-                       } else {
-                               int const offset = key + idx + (idx > 0 ? -MAX_HASH_VALUE : MAX_HASH_VALUE);
-                               latexkeys const * base = &wordlist[-lookup[offset]];
-                               latexkeys const * ptr = base + -lookup[offset + 1];
-                               
-                               while (--ptr >= base)
-                                       if (*str == *ptr->name
-                                           && !compare(str + 1,
-                                                       ptr->name + 1))
-                                               return ptr;
+// global maps 
+std::map<symbolindex, int>  LatexkeyById;
+std::map<string, int>       LatexkeyByName;
+
+
+// helper structure to initialize the maps on startup:
+struct init {
+       init() {
+               int const n = sizeof(wordlist)/sizeof(wordlist[0]);
+               for (latexkeys const * it = wordlist; it != wordlist + n; ++it) {
+                       if (LatexkeyByName.find(it->name) != LatexkeyByName.end()) {
+                               std::cerr << "math_hash.C: Bug: Duplicate entry: " 
+                                         << it->name << std::endl;
+                       }
+                       LatexkeyByName[it->name] = it - wordlist;
+                       if (it->id != 0 && 
+                           LatexkeyById.find(symbolindex(it->id, it->token)) !=
+                           LatexkeyById.end()) {
+                               std::cerr << "math_hash.C: Bug: Duplicate entry: "
+                                         << it->name << " Id: "
+                                         << it->id << " token: " << it->token
+                                         << std::endl;
                        }
+                       LatexkeyById[symbolindex(it->id, it->token)] = it - wordlist;
                }
        }
-       return 0;
-}
+};
 
+// the "Initializer": Its default constructor is executed on loading and
+// fills the maps
+static init dummy;
 
-latexkeys const * in_word_set(string const & str) 
-{
-       return in_word_set(str.c_str(), str.length());
-}
+} // namespace anon
 
 
-latexkeys const * lm_get_key_by_id(int t, short tk)
+latexkeys const * in_word_set(string const & str)
 {
-       latexkeys const * l = &wordlist[MIN_HASH_VALUE+TOTAL_KEYWORDS];
-       latexkeys const * base = &wordlist[MIN_HASH_VALUE];
-       while (--l >= base) {
-               if (t == l->id && tk == l->token)
-                       return l;
-       }
-       return 0;
+       std::map<string, int>::const_iterator pos = LatexkeyByName.find(str);
+       return pos == LatexkeyByName.end() ? 0 : &wordlist[pos->second];
 }
 
 
-latexkeys const * lm_get_key_by_index(int i)
+latexkeys const * lm_get_key_by_id(unsigned int id, short tc)
 {
-       if (i > 0 && i < TOTAL_KEYWORDS + 2)
-               return &wordlist[i];
-       else
-               return 0;
+       std::map<symbolindex, int>::const_iterator pos
+               = LatexkeyById.find(symbolindex(id, tc));
+       return pos == LatexkeyById.end() ? 0 : &wordlist[pos->second];
 }