-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dict): shorten the data struct used by Vocabulary
This may save about 18% peak memory consumption when compiling dict.
- Loading branch information
1 parent
2644f3c
commit 210ab6c
Showing
6 changed files
with
73 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
// 2011-07-24 GONG Chen <[email protected]> | ||
// | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <sstream> | ||
#include <utility> | ||
#include <rime/dict/vocabulary.h> | ||
|
@@ -59,6 +60,18 @@ string Code::ToString() const { | |
return stream.str(); | ||
} | ||
|
||
inline ShortDictEntry DictEntry::ToShort() const { | ||
return {text, code, weight}; | ||
} | ||
|
||
bool ShortDictEntry::operator< (const ShortDictEntry& other) const { | ||
// Sort different entries sharing the same code by weight desc. | ||
if (weight != other.weight) | ||
return weight > other.weight; | ||
// reduce carbon emission | ||
return 0; //text < other.text; | ||
} | ||
|
||
bool DictEntry::operator< (const DictEntry& other) const { | ||
// Sort different entries sharing the same code by weight desc. | ||
if (weight != other.weight) | ||
|
@@ -72,16 +85,34 @@ inline bool dereference_less(const T& a, const T& b) { | |
return *a < *b; | ||
} | ||
|
||
template <typename C> | ||
inline void sort(C &container) { | ||
std::sort(std::begin(container), std::end(container), dereference_less<typename C::value_type>); | ||
} | ||
|
||
template <typename C> | ||
inline void sort_range(C &container, size_t start, size_t count) { | ||
if (start >= container.size()) | ||
return; | ||
auto i(std::begin(container) + start); | ||
auto j(start + count >= container.size() ? std::end(container) : i + count); | ||
std::sort(i, j, dereference_less<typename C::value_type>); | ||
} | ||
|
||
void ShortDictEntryList::Sort() { | ||
sort(*this); | ||
} | ||
|
||
void ShortDictEntryList::SortRange(size_t start, size_t count) { | ||
sort_range(*this, start, count); | ||
} | ||
|
||
void DictEntryList::Sort() { | ||
std::sort(begin(), end(), dereference_less<DictEntryList::value_type>); | ||
sort(*this); | ||
} | ||
|
||
void DictEntryList::SortRange(size_t start, size_t count) { | ||
if (start >= size()) | ||
return; | ||
iterator i(begin() + start); | ||
iterator j(start + count >= size() ? end() : i + count); | ||
std::sort(i, j, dereference_less<DictEntryList::value_type>); | ||
sort_range(*this, start, count); | ||
} | ||
|
||
void DictEntryFilterBinder::AddFilter(DictEntryFilter filter) { | ||
|
@@ -96,7 +127,7 @@ void DictEntryFilterBinder::AddFilter(DictEntryFilter filter) { | |
} | ||
} | ||
|
||
DictEntryList* Vocabulary::LocateEntries(const Code& code) { | ||
ShortDictEntryList* Vocabulary::LocateEntries(const Code& code) { | ||
Vocabulary* v = this; | ||
size_t n = code.size(); | ||
for (size_t i = 0; i < n; ++i) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters