diff --git a/lib/libZRCola/include/zrcola/common.h b/lib/libZRCola/include/zrcola/common.h index 5d2af28..cce794d 100644 --- a/lib/libZRCola/include/zrcola/common.h +++ b/lib/libZRCola/include/zrcola/common.h @@ -80,7 +80,7 @@ namespace ZRCola { /// - /// Compares two elements + /// Compares two elements (for searching) /// /// \param[in] a Pointer to first element /// \param[in] b Pointer to second element @@ -93,6 +93,20 @@ namespace ZRCola { virtual int compare(_In_ const T &a, _In_ const T &b) const = 0; + /// + /// Compares two elements (for sorting) + /// + /// \param[in] a Pointer to first element + /// \param[in] b Pointer to second element + /// + /// \returns + /// - <0 when a < b + /// - =0 when a == b + /// - >0 when a > b + /// + virtual int compare_sort(_In_ const T &a, _In_ const T &b) const = 0; + + /// /// Search for the element in the index /// The elements matching \p el are located on the interval [\p start, \p end) in the index. @@ -139,7 +153,7 @@ namespace ZRCola { static int __cdecl compare_s(void *p, const void *a, const void *b) { const index *t = (const index*)p; - return t->compare(t->host[*(const T_idx*)a], t->host[*(const T_idx*)b]); + return t->compare_sort(t->host[*(const T_idx*)a], t->host[*(const T_idx*)b]); } }; diff --git a/lib/libZRCola/include/zrcola/translate.h b/lib/libZRCola/include/zrcola/translate.h index 534762a..dcf2242 100644 --- a/lib/libZRCola/include/zrcola/translate.h +++ b/lib/libZRCola/include/zrcola/translate.h @@ -92,7 +92,7 @@ namespace ZRCola { indexComp(_In_ std::vector &h) : index(h) {} /// - /// Compares two transformations by string + /// Compares two transformations by string (for searching) /// /// \param[in] a Pointer to key sequence /// \param[in] b Pointer to second key sequence @@ -113,6 +113,32 @@ namespace ZRCola { return 0; } + + /// + /// Compares two transformations by string (for sorting) + /// + /// \param[in] a Pointer to first element + /// \param[in] b Pointer to second element + /// + /// \returns + /// - <0 when a < b + /// - =0 when a == b + /// - >0 when a > b + /// + virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &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); + if (r != 0) return r; + + if (trans_a.chr < trans_b.chr) return -1; + else if (trans_a.chr > trans_b.chr) return +1; + + return 0; + } } idxComp; ///< Composition index @@ -130,7 +156,7 @@ namespace ZRCola { indexDecomp(_In_ std::vector &h) : index(h) {} /// - /// Compares two transformations by string + /// Compares two transformations by character (for searching) /// /// \param[in] a Pointer to key sequence /// \param[in] b Pointer to second key sequence @@ -151,6 +177,32 @@ namespace ZRCola { return 0; } + + /// + /// Compares two transformations by character (for sorting) + /// + /// \param[in] a Pointer to first element + /// \param[in] b Pointer to second element + /// + /// \returns + /// - <0 when a < b + /// - =0 when a == b + /// - >0 when a > b + /// + virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &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; + + int r = translation::CompareString(trans_a.str, trans_a.str_len, trans_b.str, trans_b.str_len); + if (r != 0) return r; + + return 0; + } } idxDecomp; ///< Decomposition index diff --git a/lib/libZRColaUI/include/zrcolaui/keyboard.h b/lib/libZRColaUI/include/zrcolaui/keyboard.h index 8ebfd74..865a8cb 100644 --- a/lib/libZRColaUI/include/zrcolaui/keyboard.h +++ b/lib/libZRColaUI/include/zrcolaui/keyboard.h @@ -100,7 +100,7 @@ namespace ZRCola { indexChr(_In_ std::vector &h) : index(h) {} /// - /// Compares two key sequences by character + /// Compares two key sequences by character (for searching) /// /// \param[in] a Pointer to key sequence /// \param[in] b Pointer to second key sequence @@ -121,6 +121,32 @@ namespace ZRCola { return 0; } + + /// + /// Compares two key sequences by character (for sorting) + /// + /// \param[in] a Pointer to key sequence + /// \param[in] b Pointer to second key sequence + /// + /// \returns + /// - <0 when a < b + /// - =0 when a == b + /// - >0 when a > b + /// + virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &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; + + int r = keyseq::CompareSequence(ks_a.seq, ks_a.seq_len, ks_b.seq, ks_b.seq_len); + if (r != 0) return r; + + return 0; + } } idxChr; ///< Character index @@ -138,7 +164,7 @@ namespace ZRCola { indexKey(_In_ std::vector &h) : index(h) {} /// - /// Compares two key sequences by key + /// Compares two key sequences by key (for searching) /// /// \param[in] a Pointer to key sequence /// \param[in] b Pointer to second key sequence @@ -159,6 +185,32 @@ namespace ZRCola { return 0; } + + /// + /// Compares two key sequences by key (for sorting) + /// + /// \param[in] a Pointer to key sequence + /// \param[in] b Pointer to second key sequence + /// + /// \returns + /// - <0 when a < b + /// - =0 when a == b + /// - >0 when a > b + /// + virtual int compare_sort(_In_ const unsigned __int16 &a, _In_ const unsigned __int16 &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); + if (r != 0) return r; + + if (ks_a.chr < ks_b.chr) return -1; + else if (ks_a.chr > ks_b.chr) return +1; + + return 0; + } } idxKey; ///< Key index std::vector data; ///< Key sequences data