use binary search for unicode -> macron.

This commit is contained in:
Kelvin Sherlock 2016-07-23 16:05:05 -04:00
parent f97625eba7
commit 84737e1cf7
1 changed files with 42 additions and 7 deletions

View File

@ -3,29 +3,64 @@
#include <string>
#include <algorithm>
#include <stdint.h>
#include <array>
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<uint16_t, 0x80> table = {{
#undef _
#define _(macroman,unicode,comment) unicode ,
#include "macroman.x"
#undef _
};
}};
if (c < 0x80) return c;
return table[c - 0x80];