5
5
// 2011-07-24 GONG Chen <[email protected] >
6
6
//
7
7
#include < algorithm>
8
+ #include < iterator>
8
9
#include < sstream>
9
10
#include < utility>
10
11
#include < rime/dict/vocabulary.h>
@@ -59,6 +60,18 @@ string Code::ToString() const {
59
60
return stream.str ();
60
61
}
61
62
63
+ inline ShortDictEntry DictEntry::ToShort () const {
64
+ return {text, code, weight};
65
+ }
66
+
67
+ bool ShortDictEntry::operator < (const ShortDictEntry& other) const {
68
+ // Sort different entries sharing the same code by weight desc.
69
+ if (weight != other.weight )
70
+ return weight > other.weight ;
71
+ // reduce carbon emission
72
+ return 0 ; // text < other.text;
73
+ }
74
+
62
75
bool DictEntry::operator < (const DictEntry& other) const {
63
76
// Sort different entries sharing the same code by weight desc.
64
77
if (weight != other.weight )
@@ -72,16 +85,34 @@ inline bool dereference_less(const T& a, const T& b) {
72
85
return *a < *b;
73
86
}
74
87
88
+ template <typename C>
89
+ inline void sort (C &container) {
90
+ std::sort (std::begin (container), std::end (container), dereference_less<typename C::value_type>);
91
+ }
92
+
93
+ template <typename C>
94
+ inline void sort_range (C &container, size_t start, size_t count) {
95
+ if (start >= container.size ())
96
+ return ;
97
+ auto i (std::begin (container) + start);
98
+ auto j (start + count >= container.size () ? std::end (container) : i + count);
99
+ std::sort (i, j, dereference_less<typename C::value_type>);
100
+ }
101
+
102
+ void ShortDictEntryList::Sort () {
103
+ sort (*this );
104
+ }
105
+
106
+ void ShortDictEntryList::SortRange (size_t start, size_t count) {
107
+ sort_range (*this , start, count);
108
+ }
109
+
75
110
void DictEntryList::Sort () {
76
- std:: sort (begin (), end (), dereference_less<DictEntryList::value_type> );
111
+ sort (* this );
77
112
}
78
113
79
114
void DictEntryList::SortRange (size_t start, size_t count) {
80
- if (start >= size ())
81
- return ;
82
- iterator i (begin () + start);
83
- iterator j (start + count >= size () ? end () : i + count);
84
- std::sort (i, j, dereference_less<DictEntryList::value_type>);
115
+ sort_range (*this , start, count);
85
116
}
86
117
87
118
void DictEntryFilterBinder::AddFilter (DictEntryFilter filter) {
@@ -96,7 +127,7 @@ void DictEntryFilterBinder::AddFilter(DictEntryFilter filter) {
96
127
}
97
128
}
98
129
99
- DictEntryList * Vocabulary::LocateEntries (const Code& code) {
130
+ ShortDictEntryList * Vocabulary::LocateEntries (const Code& code) {
100
131
Vocabulary* v = this ;
101
132
size_t n = code.size ();
102
133
for (size_t i = 0 ; i < n; ++i) {
0 commit comments