Lars Gullik Bjønnes wrote:

> Angus Leeming <[EMAIL PROTECTED]> writes:
> 
> | Lars, you have posted the following warning in
> | controllers/ControlMath.C:
> | 
> | string const find_xpm(string const & name)
> | {
> | #warning Use a static table for this (Lgb)
> |         // And get O(log n) lookup (Lgb)
> | 
> |         string xpm_name = subst(name, ' ', '_');
> |         if (xpm_name == "(") xpm_name = "lparen";
> |         else if (xpm_name == ")") xpm_name = "rparen";
> |         else if (xpm_name == "[") xpm_name = "lbracket";
> |         else if (xpm_name == "]") xpm_name = "rbracket";
> |         else if (xpm_name == "{") xpm_name = "lbrace";
> |         ...
> | 
> | Do you mean something like:
> 
> actually I think I thought more of using just the xpm_names table
> and some std:: binary search algorithm.

Ok, this seems to do the trick.
Angus

#include <string>
#include <iostream>

using std::string;


namespace {

struct XPMmap {
        char const * key;
        char const * value;
};


struct CompareKey {
        bool operator()(XPMmap const & lhs, XPMmap const & rhs) {
                return strcmp(lhs.key, rhs.key) < 0;
        }
        bool operator()(XPMmap const & lhs, string const & rhs) {
                return strcmp(lhs.key, rhs.c_str()) < 0;
        }
};


XPMmap unsorted_xpm_map[] = {
        { "(", "lparen" },
        { ")", "rparen"},
        { "[", "lbracket"},
        { "]", "rbracket"},
        { "{", "lbrace"}
};


size_t const xpm_map_size = sizeof(unsorted_xpm_map) / sizeof(XPMmap);

XPMmap * build_sorted_map() {
        XPMmap * it  = unsorted_xpm_map;
        XPMmap * end = it + xpm_map_size;
        std::sort(it, end, CompareKey());
        return it;
}

} // namespace anon


string const find_xpm(string const & name)
{
        static XPMmap const * const begin = build_sorted_map();
        XPMmap const * const end = begin + xpm_map_size;
        XPMmap const * const it =
                std::lower_bound(begin, end, name, CompareKey());

        string xpm_name;
        if (it != end)
                xpm_name = it->value;
        else {
//              xpm_name = subst(name, "_", "underscore");
//              xpm_name = subst(xpm_name, ' ', '_');
        }

//      lyxerr[Debug::GUI] << "Looking for math XPM called \""
//              << xpm_name << '"' << std::endl;

//      return LibFileSearch("images/math/", xpm_name, "xpm");
        return xpm_name;
}


int main ()
{
        std::cout << "(: " << find_xpm("(") << '\n'
                  << "): " << find_xpm(")") << '\n'
                  << "[: " << find_xpm("[") << '\n'
                  << "]: " << find_xpm("]") << '\n'
                  << "{: " << find_xpm("{") << std::endl;
        return 0;
}


Reply via email to