Indexes redesigned to return data reference directly for cleaner code
This commit is contained in:
parent
4d72a5bc4c
commit
e13133d0e4
@ -76,13 +76,13 @@ bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event)
|
||||
ks->chr = 0;
|
||||
ks->seq_len = n;
|
||||
memcpy(ks->seq, m_seq.data(), sizeof(ZRCola::keyseq_db::keyseq::key_t)*n);
|
||||
found = m_ks_db.idxKey.find((const unsigned __int16&)*ks, start, end);
|
||||
found = m_ks_db.idxKey.find(*ks, start, end);
|
||||
delete ks;
|
||||
}
|
||||
|
||||
if (found) {
|
||||
// The exact key sequence found.
|
||||
const ZRCola::keyseq_db::keyseq &ks = (const ZRCola::keyseq_db::keyseq&)m_ks_db.data[m_ks_db.idxKey[start]];
|
||||
const ZRCola::keyseq_db::keyseq &ks = m_ks_db.idxKey[start];
|
||||
m_seq.clear();
|
||||
|
||||
wxObject *obj = event.GetEventObject();
|
||||
@ -95,7 +95,7 @@ bool wxZRColaKeyHandler::ProcessEvent(wxEvent& event)
|
||||
return true;
|
||||
}
|
||||
} else if (start < m_ks_db.idxKey.size() &&
|
||||
ZRCola::keyseq_db::keyseq::CompareSequence(m_seq.data(), m_seq.size(), ((const ZRCola::keyseq_db::keyseq&)m_ks_db.data[m_ks_db.idxKey[start]]).seq, std::min<unsigned __int16>(((const ZRCola::keyseq_db::keyseq&)m_ks_db.data[m_ks_db.idxKey[start]]).seq_len, m_seq.size())) == 0)
|
||||
ZRCola::keyseq_db::keyseq::CompareSequence(m_seq.data(), m_seq.size(), m_ks_db.idxKey[start].seq, std::min<unsigned __int16>(m_ks_db.idxKey[start].seq_len, m_seq.size())) == 0)
|
||||
{
|
||||
// The sequence is a partial match. Continue watching.
|
||||
event.StopPropagation();
|
||||
|
@ -305,8 +305,8 @@ int _tmain(int argc, _TCHAR *argv[])
|
||||
// Check key sequences.
|
||||
for (std::vector<unsigned __int32>::size_type i = 1, n = db.idxKey.size(); i < n; i++) {
|
||||
const ZRCola::keyseq_db::keyseq
|
||||
&ks1 = (const ZRCola::keyseq_db::keyseq&)db.data[db.idxKey[i - 1]],
|
||||
&ks2 = (const ZRCola::keyseq_db::keyseq&)db.data[db.idxKey[i ]];
|
||||
&ks1 = db.idxKey[i - 1],
|
||||
&ks2 = db.idxKey[i ];
|
||||
|
||||
if (ZRCola::keyseq_db::keyseq::CompareSequence(ks1.seq, ks1.seq_len, ks2.seq, ks2.seq_len) == 0) {
|
||||
std::wstring seq_str;
|
||||
|
@ -55,7 +55,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// Memory index
|
||||
///
|
||||
template <class T, class T_idx = unsigned __int32>
|
||||
template <class T, class T_idx = unsigned __int32, class T_data = T>
|
||||
class index : public std::vector<T_idx>
|
||||
{
|
||||
protected:
|
||||
@ -70,6 +70,58 @@ namespace ZRCola {
|
||||
index(_In_ std::vector<T> &h) : host(h) {}
|
||||
|
||||
|
||||
///
|
||||
/// Returns data at given position according to the index
|
||||
///
|
||||
/// \param[in] pos Position
|
||||
///
|
||||
/// \returns Data reference
|
||||
///
|
||||
inline const T_data& at(size_type pos) const
|
||||
{
|
||||
return (const T_data&)host.at(std::vector<T_idx>::at(pos));
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns data at given position according to the index
|
||||
///
|
||||
/// \param[in] pos Position
|
||||
///
|
||||
/// \returns Data reference
|
||||
///
|
||||
inline T_data& at(size_type pos)
|
||||
{
|
||||
return (T_data&)host.at(std::vector<T_idx>::at(pos));
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns data at given position according to the index
|
||||
///
|
||||
/// \param[in] pos Position
|
||||
///
|
||||
/// \returns Data reference
|
||||
///
|
||||
inline const T_data& operator[](size_type pos) const
|
||||
{
|
||||
return (const T_data&)host[std::vector<T_idx>::at(pos)];
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Returns data at given position according to the index
|
||||
///
|
||||
/// \param[in] pos Position
|
||||
///
|
||||
/// \returns Data reference
|
||||
///
|
||||
inline T_data& operator[](size_type pos)
|
||||
{
|
||||
return (T_data&)host[std::vector<T_idx>::at(pos)];
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
/// Sorts index
|
||||
///
|
||||
@ -90,7 +142,7 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const T &a, _In_ const T &b) const = 0;
|
||||
virtual int compare(_In_ const T_data &a, _In_ const T_data &b) const = 0;
|
||||
|
||||
|
||||
///
|
||||
@ -104,7 +156,7 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const T &a, _In_ const T &b) const = 0;
|
||||
virtual int compare_sort(_In_ const T_data &a, _In_ const T_data &b) const = 0;
|
||||
|
||||
|
||||
///
|
||||
@ -119,26 +171,26 @@ namespace ZRCola {
|
||||
/// - true if found
|
||||
/// - false otherwise
|
||||
///
|
||||
bool find(_In_ const T &el, _Out_ size_type &start, _Out_ size_type &end) const
|
||||
bool find(_In_ const T_data &el, _Out_ size_type &start, _Out_ size_type &end) const
|
||||
{
|
||||
// Start with the full search area.
|
||||
for (start = 0, end = size(); start < end; ) {
|
||||
size_type m = (start + end) / 2;
|
||||
int r = compare(el, host[at(m)]);
|
||||
int r = compare(el, at(m));
|
||||
if (r < 0) end = m;
|
||||
else if (r > 0) start = m + 1;
|
||||
else {
|
||||
// Narrow the search area on the left to start at the first element in the run.
|
||||
for (size_type end2 = m; start < end2;) {
|
||||
size_type m = (start + end2) / 2;
|
||||
int r = compare(el, host[at(m)]);
|
||||
int r = compare(el, at(m));
|
||||
if (r <= 0) end2 = m; else start = m + 1;
|
||||
}
|
||||
|
||||
// Narrow the search area on the right to end at the first element not in the run.
|
||||
for (size_type start2 = m + 1; start2 < end;) {
|
||||
size_type m = (start2 + end) / 2;
|
||||
int r = compare(el, host[at(m)]);
|
||||
int r = compare(el, at(m));
|
||||
if (0 <= r) start2 = m + 1; else end = m;
|
||||
}
|
||||
|
||||
@ -152,8 +204,8 @@ namespace ZRCola {
|
||||
private:
|
||||
static int __cdecl compare_s(void *p, const void *a, const void *b)
|
||||
{
|
||||
const index<T, T_idx> *t = (const index<T, T_idx>*)p;
|
||||
return t->compare_sort(t->host[*(const T_idx*)a], t->host[*(const T_idx*)b]);
|
||||
const index<T, T_idx, T_data> *t = (const index<T, T_idx, T_data>*)p;
|
||||
return t->compare_sort((const T_data&)t->host[*(const T_idx*)a], (const T_data&)t->host[*(const T_idx*)b]);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -81,7 +81,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// Composition index
|
||||
///
|
||||
class indexComp : public index<unsigned __int16, unsigned __int32>
|
||||
class indexComp : public index<unsigned __int16, unsigned __int32, translation>
|
||||
{
|
||||
public:
|
||||
///
|
||||
@ -89,7 +89,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
///
|
||||
indexComp(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32>(h) {}
|
||||
indexComp(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, translation>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two transformations by string (for searching)
|
||||
@ -102,13 +102,9 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare(_In_ const translation &a, _In_ const translation &b) const
|
||||
{
|
||||
const translation
|
||||
&trans_a = (const translation&)a,
|
||||
&trans_b = (const translation&)b;
|
||||
|
||||
int r = translation::CompareString(trans_a.str, trans_a.str_len, trans_b.str, trans_b.str_len);
|
||||
int r = translation::CompareString(a.str, a.str_len, b.str, b.str_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
return 0;
|
||||
@ -125,17 +121,13 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare_sort(_In_ const translation &a, _In_ const translation &b) const
|
||||
{
|
||||
const translation
|
||||
&trans_a = (const translation&)a,
|
||||
&trans_b = (const translation&)b;
|
||||
|
||||
int r = translation::CompareString(trans_a.str, trans_a.str_len, trans_b.str, trans_b.str_len);
|
||||
int r = translation::CompareString(a.str, a.str_len, b.str, b.str_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
if (trans_a.chr < trans_b.chr) return -1;
|
||||
else if (trans_a.chr > trans_b.chr) return +1;
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return +1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -145,7 +137,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// Decomposition index
|
||||
///
|
||||
class indexDecomp : public index<unsigned __int16, unsigned __int32>
|
||||
class indexDecomp : public index<unsigned __int16, unsigned __int32, translation>
|
||||
{
|
||||
public:
|
||||
///
|
||||
@ -153,7 +145,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
///
|
||||
indexDecomp(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32>(h) {}
|
||||
indexDecomp(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, translation>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two transformations by character (for searching)
|
||||
@ -166,14 +158,10 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare(_In_ const translation &a, _In_ const translation &b) const
|
||||
{
|
||||
const translation
|
||||
&trans_a = (const translation&)a,
|
||||
&trans_b = (const translation&)b;
|
||||
|
||||
if (trans_a.chr < trans_b.chr) return -1;
|
||||
else if (trans_a.chr > trans_b.chr) return +1;
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return +1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -189,16 +177,12 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare_sort(_In_ const translation &a, _In_ const translation &b) const
|
||||
{
|
||||
const translation
|
||||
&trans_a = (const translation&)a,
|
||||
&trans_b = (const translation&)b;
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return +1;
|
||||
|
||||
if (trans_a.chr < trans_b.chr) return -1;
|
||||
else if (trans_a.chr > trans_b.chr) return +1;
|
||||
|
||||
int r = translation::CompareString(trans_a.str, trans_a.str_len, trans_b.str, trans_b.str_len);
|
||||
int r = translation::CompareString(a.str, a.str_len, b.str, b.str_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
return 0;
|
||||
|
@ -49,7 +49,7 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
|
||||
// Get the j-th character of the composition.
|
||||
// All compositions that get short on characters are lexically ordered before.
|
||||
// Thus the j-th character is considered 0.
|
||||
const translation &trans = (const translation&)data[idxComp[m]];
|
||||
const translation &trans = idxComp[m];
|
||||
wchar_t s = j < trans.str_len ? trans.str[j] : 0;
|
||||
|
||||
// Do the bisection test.
|
||||
@ -61,7 +61,7 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
|
||||
// Narrow the search area on the left to start at the first composition in the run.
|
||||
for (size_t rr = m; l < rr;) {
|
||||
size_t m = (l + rr) / 2;
|
||||
const translation &trans = (const translation&)data[idxComp[m]];
|
||||
const translation &trans = idxComp[m];
|
||||
wchar_t s = j < trans.str_len ? trans.str[j] : 0;
|
||||
if (c <= s) rr = m; else l = m + 1;
|
||||
}
|
||||
@ -69,7 +69,7 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
|
||||
// Narrow the search area on the right to end at the first composition not in the run.
|
||||
for (size_t ll = m + 1; ll < r;) {
|
||||
size_t m = (ll + r) / 2;
|
||||
const translation &trans = (const translation&)data[idxComp[m]];
|
||||
const translation &trans = idxComp[m];
|
||||
wchar_t s = j < trans.str_len ? trans.str[j] : 0;
|
||||
if (s <= c) ll = m + 1; else r = m;
|
||||
}
|
||||
@ -80,7 +80,7 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
|
||||
|
||||
if (l >= r) {
|
||||
// The search area is empty.
|
||||
const translation &trans = (const translation&)data[idxComp[l_prev]];
|
||||
const translation &trans = idxComp[l_prev];
|
||||
if (j && l_prev < compositionsCount && j == trans.str_len) {
|
||||
// The first composition of the previous run was a match.
|
||||
output += trans.chr;
|
||||
@ -99,7 +99,7 @@ void ZRCola::translation_db::Compose(_In_z_count_(inputMax) const wchar_t* input
|
||||
} else {
|
||||
// End of input reached.
|
||||
|
||||
const translation &trans = (const translation&)data[idxComp[l]];
|
||||
const translation &trans = idxComp[l];
|
||||
if (l < compositionsCount && j == trans.str_len) {
|
||||
// The first composition of the previous run was a match.
|
||||
output += trans.chr;
|
||||
@ -143,7 +143,7 @@ void ZRCOLA_API ZRCola::translation_db::Decompose(_In_z_count_(inputMax) const w
|
||||
for (size_t l = 0, r = decompositionsCount;; ) {
|
||||
if (l < r) {
|
||||
size_t m = (l + r) / 2;
|
||||
const translation &trans = (const translation&)data[idxDecomp[m]];
|
||||
const translation &trans = idxDecomp[m];
|
||||
wchar_t decompSrc = trans.chr;
|
||||
if (c < decompSrc) r = m;
|
||||
else if (decompSrc < c) l = m + 1;
|
||||
|
@ -89,7 +89,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// Character index
|
||||
///
|
||||
class indexChr : public index<unsigned __int16, unsigned __int32>
|
||||
class indexChr : public index<unsigned __int16, unsigned __int32, keyseq>
|
||||
{
|
||||
public:
|
||||
///
|
||||
@ -97,7 +97,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
///
|
||||
indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32>(h) {}
|
||||
indexChr(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, keyseq>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two key sequences by character (for searching)
|
||||
@ -110,14 +110,10 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare(_In_ const keyseq &a, _In_ const keyseq &b) const
|
||||
{
|
||||
const keyseq
|
||||
&ks_a = (const keyseq&)a,
|
||||
&ks_b = (const keyseq&)b;
|
||||
|
||||
if (ks_a.chr < ks_b.chr) return -1;
|
||||
else if (ks_a.chr > ks_b.chr) return +1;
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return +1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -133,16 +129,12 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare_sort(_In_ const keyseq &a, _In_ const keyseq &b) const
|
||||
{
|
||||
const keyseq
|
||||
&ks_a = (const keyseq&)a,
|
||||
&ks_b = (const keyseq&)b;
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return +1;
|
||||
|
||||
if (ks_a.chr < ks_b.chr) return -1;
|
||||
else if (ks_a.chr > ks_b.chr) return +1;
|
||||
|
||||
int r = keyseq::CompareSequence(ks_a.seq, ks_a.seq_len, ks_b.seq, ks_b.seq_len);
|
||||
int r = keyseq::CompareSequence(a.seq, a.seq_len, b.seq, b.seq_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
return 0;
|
||||
@ -153,7 +145,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// Key index
|
||||
///
|
||||
class indexKey : public index<unsigned __int16, unsigned __int32>
|
||||
class indexKey : public index<unsigned __int16, unsigned __int32, keyseq>
|
||||
{
|
||||
public:
|
||||
///
|
||||
@ -161,7 +153,7 @@ namespace ZRCola {
|
||||
///
|
||||
/// \param[in] h Reference to vector holding the data
|
||||
///
|
||||
indexKey(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32>(h) {}
|
||||
indexKey(_In_ std::vector<unsigned __int16> &h) : index<unsigned __int16, unsigned __int32, keyseq>(h) {}
|
||||
|
||||
///
|
||||
/// Compares two key sequences by key (for searching)
|
||||
@ -174,13 +166,9 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare(_In_ const keyseq &a, _In_ const keyseq &b) const
|
||||
{
|
||||
const keyseq
|
||||
&ks_a = (const keyseq&)a,
|
||||
&ks_b = (const keyseq&)b;
|
||||
|
||||
int r = keyseq::CompareSequence(ks_a.seq, ks_a.seq_len, ks_b.seq, ks_b.seq_len);
|
||||
int r = keyseq::CompareSequence(a.seq, a.seq_len, b.seq, b.seq_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
return 0;
|
||||
@ -197,17 +185,13 @@ namespace ZRCola {
|
||||
/// - =0 when a == b
|
||||
/// - >0 when a > b
|
||||
///
|
||||
virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &b) const
|
||||
virtual int compare_sort(_In_ const keyseq &a, _In_ const keyseq &b) const
|
||||
{
|
||||
const keyseq
|
||||
&ks_a = (const keyseq&)a,
|
||||
&ks_b = (const keyseq&)b;
|
||||
|
||||
int r = keyseq::CompareSequence(ks_a.seq, ks_a.seq_len, ks_b.seq, ks_b.seq_len);
|
||||
int r = keyseq::CompareSequence(a.seq, a.seq_len, b.seq, b.seq_len);
|
||||
if (r != 0) return r;
|
||||
|
||||
if (ks_a.chr < ks_b.chr) return -1;
|
||||
else if (ks_a.chr > ks_b.chr) return +1;
|
||||
if (a.chr < b.chr) return -1;
|
||||
else if (a.chr > b.chr) return +1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user