From 84737e1cf74b9ca999df2c283913693ba66c3a05 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sat, 23 Jul 2016 16:05:05 -0400 Subject: [PATCH] use binary search for unicode -> macron. --- macroman.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/macroman.cpp b/macroman.cpp index f0cc536..59c3495 100644 --- a/macroman.cpp +++ b/macroman.cpp @@ -3,29 +3,64 @@ #include #include #include +#include + + +struct mu_pair { + uint16_t unicode; + uint8_t macroman; + + constexpr bool operator<(const mu_pair &rhs) const noexcept { + return unicode < rhs.unicode; + } + + constexpr bool operator==(const mu_pair &rhs) const noexcept { + return unicode == rhs.unicode; + } + + constexpr bool operator<(uint16_t c) const noexcept { + return unicode < c; + } + + constexpr bool operator==(uint16_t c) const noexcept { + return unicode == c; + } +}; uint8_t unicode_to_macroman(uint16_t c){ - + + + static std::array< mu_pair, 0x80> table = {{ +#undef _ +#define _(macroman, unicode, comment) mu_pair{ unicode, macroman } , +#include "macroman.x" +#undef _ + }}; + + static bool init = false; if (c < 0x80) return c; -#undef _ -#define _(macroman,unicode,comment) if (c == unicode) return macroman; -#include "macroman.x" + if (!init) { + init = true; + std::sort(table.begin(), table.end()); + init = true; + } -#undef _ + auto iter = std::lower_bound(table.begin(), table.end(), c); + if (iter != table.end() && iter->unicode == c) return iter->macroman; return 0; } uint16_t macroman_to_unicode(uint8_t c) { - static uint16_t table[] = { + static std::array table = {{ #undef _ #define _(macroman,unicode,comment) unicode , #include "macroman.x" #undef _ - }; + }}; if (c < 0x80) return c; return table[c - 0x80];