ZRCola::mapping is generic now

This commit is contained in:
Simon Rozman 2016-04-20 12:24:23 +02:00
parent 5e4331903b
commit a021dd31f7
4 changed files with 46 additions and 31 deletions

View File

@ -51,7 +51,7 @@ void wxZRColaComposerPanel::OnDecomposedPaint(wxPaintEvent& event)
// Save new selection first, to avoid loop. // Save new selection first, to avoid loop.
m_selDecomposed.first = from; m_selDecomposed.first = from;
m_selDecomposed.second = to; m_selDecomposed.second = to;
m_composed->SetSelection(m_mapping.to_composed(from), m_mapping.to_composed(to)); m_composed->SetSelection(m_mapping.to_src(from), m_mapping.to_src(to));
} }
} }
@ -73,6 +73,7 @@ void wxZRColaComposerPanel::OnDecomposedText(wxCommandEvent& event)
std::wstring dst; std::wstring dst;
((ZRColaApp*)wxTheApp)->m_t_db.Compose(src.data(), src.size(), dst, &m_mapping); ((ZRColaApp*)wxTheApp)->m_t_db.Compose(src.data(), src.size(), dst, &m_mapping);
m_mapping.invert();
long from, to; long from, to;
m_decomposed->GetSelection(&from, &to); m_decomposed->GetSelection(&from, &to);
@ -80,7 +81,7 @@ void wxZRColaComposerPanel::OnDecomposedText(wxCommandEvent& event)
// Update composed text. // Update composed text.
m_progress = true; m_progress = true;
m_composed->SetValue(dst); m_composed->SetValue(dst);
m_composed->SetSelection(m_mapping.to_composed(from), m_mapping.to_composed(to)); m_composed->SetSelection(m_mapping.to_src(from), m_mapping.to_src(to));
event.Skip(); event.Skip();
m_progress = false; m_progress = false;
} }
@ -98,7 +99,7 @@ void wxZRColaComposerPanel::OnComposedPaint(wxPaintEvent& event)
// Save new selection first, to avoid loop. // Save new selection first, to avoid loop.
m_selComposed.first = from; m_selComposed.first = from;
m_selComposed.second = to; m_selComposed.second = to;
m_decomposed->SetSelection(m_mapping.to_decomposed(from), m_mapping.to_decomposed(to)); m_decomposed->SetSelection(m_mapping.to_dst(from), m_mapping.to_dst(to));
} }
} }
@ -132,7 +133,7 @@ void wxZRColaComposerPanel::OnComposedText(wxCommandEvent& event)
// Update decomposed text. // Update decomposed text.
m_progress = true; m_progress = true;
m_decomposed->SetValue(dst); m_decomposed->SetValue(dst);
m_decomposed->SetSelection(m_mapping.to_decomposed(from), m_mapping.to_decomposed(to)); m_decomposed->SetSelection(m_mapping.to_dst(from), m_mapping.to_dst(to));
event.Skip(); event.Skip();
m_progress = false; m_progress = false;
} }

View File

@ -223,15 +223,20 @@ namespace ZRCola {
/// ///
/// Composed-decomposed index transformation mapping /// Source-destination index transformation mapping
/// ///
class ZRCOLA_NOVTABLE ZRCOLA_API mapping { class ZRCOLA_NOVTABLE ZRCOLA_API mapping {
public: public:
size_t cmp; ///< Character index in composed string size_t src; ///< Character index in source string
size_t decmp; ///< Character index in decomposed string size_t dst; ///< Character index in destination string
inline mapping() {}; inline mapping() {};
inline mapping(_In_ size_t c, _In_ size_t d) : cmp(c), decmp(d) {} inline mapping(_In_ size_t s, _In_ size_t d) : src(s), dst(d) {}
///
/// Reverses source and destination indexes
///
inline void invert() { size_t tmp = src; src = dst; dst = tmp; }
}; };
@ -241,22 +246,31 @@ namespace ZRCola {
class ZRCOLA_API mapping_vector : public std::vector<mapping> { class ZRCOLA_API mapping_vector : public std::vector<mapping> {
public: public:
/// ///
/// Transforms character index of decomposed to composed string /// Transforms character index of destination to source
/// ///
/// \param[in] decmp Character index in decomposed string /// \param[in] decmp Character index in destination string
/// ///
/// \returns Character index in composed string /// \returns Character index in source string
/// ///
size_t to_composed(_In_ size_t decmp) const; size_t to_src(_In_ size_t dst) const;
/// ///
/// Transforms destination index to source index /// Transforms source index to destination index
/// ///
/// \param[in] cmp Character index in composed string /// \param[in] cmp Character index in source string
/// ///
/// \returns Character index in decomposed string /// \returns Character index in destination string
/// ///
size_t to_decomposed(_In_ size_t cmp) const; size_t to_dst(_In_ size_t src) const;
///
/// Reverses source and destination indexes
///
inline void invert()
{
for (iterator i = begin(), iEnd = end(); i != iEnd; ++i)
i->invert();
}
}; };
}; };

View File

@ -20,51 +20,51 @@
#include "stdafx.h" #include "stdafx.h"
size_t ZRCola::mapping_vector::to_composed(_In_ size_t decmp) const size_t ZRCola::mapping_vector::to_src(_In_ size_t dst) const
{ {
for (size_type l = 0, r = size();;) { for (size_type l = 0, r = size();;) {
if (l < r) { if (l < r) {
size_type m = (l + r) / 2; size_type m = (l + r) / 2;
const mapping &el = (*this)[m]; const mapping &el = (*this)[m];
if (decmp < el.decmp) r = m; if ( dst < el.dst) r = m;
else if (el.decmp < decmp) l = m + 1; else if (el.dst < dst) l = m + 1;
else { else {
// An exact match found. // An exact match found.
return el.cmp; return el.src;
} }
} else if (l) { } else if (l) {
// We found a map interval. // We found a map interval.
const mapping &el = (*this)[l - 1]; const mapping &el = (*this)[l - 1];
return el.cmp + (decmp - el.decmp); return el.src + (dst - el.dst);
} else { } else {
// The decomposed character index is far left. // The destination character index is far left.
return decmp; return dst;
} }
} }
} }
size_t ZRCola::mapping_vector::to_decomposed(_In_ size_t cmp) const size_t ZRCola::mapping_vector::to_dst(_In_ size_t src) const
{ {
for (size_type l = 0, r = size();;) { for (size_type l = 0, r = size();;) {
if (l < r) { if (l < r) {
size_type m = (l + r) / 2; size_type m = (l + r) / 2;
const mapping &el = (*this)[m]; const mapping &el = (*this)[m];
if (cmp < el.cmp) r = m; if ( src < el.src) r = m;
else if (el.cmp < cmp) l = m + 1; else if (el.src < src) l = m + 1;
else { else {
// An exact match found. // An exact match found.
return el.decmp; return el.dst;
} }
} else if (l) { } else if (l) {
// We found a map interval. // We found a map interval.
const mapping &el = (*this)[l - 1]; const mapping &el = (*this)[l - 1];
return el.decmp + (cmp - el.cmp); return el.dst + (src - el.src);
} else { } else {
// The composed character index is far left. // The source character index is far left.
return cmp; return src;
} }
} }
} }

View File

@ -91,7 +91,7 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
i += trans.str_len; i += trans.str_len;
if (trans.str_len > 1 && map) { if (trans.str_len > 1 && map) {
// Mapping changed. // Mapping changed.
map->push_back(ZRCola::mapping(output.length(), i)); map->push_back(ZRCola::mapping(i, output.length()));
} }
} else { } else {
// The match was not found. // The match was not found.