mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use an unordered_map with std::string keys
This is needed because we use references to values in the map and std::map invalidates references on insertion while std::unordered_map does not
This commit is contained in:
parent
78dea8e439
commit
53d0d1c249
@ -11,7 +11,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <unordered_map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sqlite3ext.h>
|
#include <sqlite3ext.h>
|
||||||
@ -98,13 +98,6 @@ class IteratorDescription {
|
|||||||
UScriptCode script;
|
UScriptCode script;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct char_cmp {
|
|
||||||
bool operator () (const char *a, const char *b) const
|
|
||||||
{
|
|
||||||
return strcmp(a,b)<0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Stemmer {
|
class Stemmer {
|
||||||
private:
|
private:
|
||||||
struct sb_stemmer *handle;
|
struct sb_stemmer *handle;
|
||||||
@ -142,6 +135,7 @@ public:
|
|||||||
|
|
||||||
typedef std::unique_ptr<icu::BreakIterator> BreakIterator;
|
typedef std::unique_ptr<icu::BreakIterator> BreakIterator;
|
||||||
typedef std::unique_ptr<Stemmer> StemmerPtr;
|
typedef std::unique_ptr<Stemmer> StemmerPtr;
|
||||||
|
static const std::string empty_string("");
|
||||||
|
|
||||||
class Tokenizer {
|
class Tokenizer {
|
||||||
private:
|
private:
|
||||||
@ -151,8 +145,8 @@ private:
|
|||||||
std::string token_buf, current_ui_language;
|
std::string token_buf, current_ui_language;
|
||||||
token_callback_func current_callback;
|
token_callback_func current_callback;
|
||||||
void *current_callback_ctx;
|
void *current_callback_ctx;
|
||||||
std::map<const char*, BreakIterator, char_cmp> iterators;
|
std::unordered_map<std::string, BreakIterator> iterators;
|
||||||
std::map<const char*, StemmerPtr, char_cmp> stemmers;
|
std::unordered_map<std::string, StemmerPtr> stemmers;
|
||||||
|
|
||||||
bool is_token_char(UChar32 ch) const {
|
bool is_token_char(UChar32 ch) const {
|
||||||
switch(u_charType(ch)) {
|
switch(u_charType(ch)) {
|
||||||
@ -225,11 +219,11 @@ private:
|
|||||||
|
|
||||||
void ensure_basic_iterator(void) {
|
void ensure_basic_iterator(void) {
|
||||||
std::lock_guard<std::mutex> lock(global_mutex);
|
std::lock_guard<std::mutex> lock(global_mutex);
|
||||||
if (current_ui_language != ui_language || iterators.find("") == iterators.end()) {
|
if (current_ui_language != ui_language || iterators.find(empty_string) == iterators.end()) {
|
||||||
current_ui_language.clear(); current_ui_language = ui_language;
|
current_ui_language.clear(); current_ui_language = ui_language;
|
||||||
icu::ErrorCode status;
|
icu::ErrorCode status;
|
||||||
if (current_ui_language.empty()) {
|
if (current_ui_language.empty()) {
|
||||||
iterators[""] = BreakIterator(icu::BreakIterator::createWordInstance(icu::Locale::getDefault(), status));
|
iterators[empty_string] = BreakIterator(icu::BreakIterator::createWordInstance(icu::Locale::getDefault(), status));
|
||||||
} else {
|
} else {
|
||||||
ensure_lang_iterator(ui_language);
|
ensure_lang_iterator(ui_language);
|
||||||
}
|
}
|
||||||
@ -297,7 +291,8 @@ public:
|
|||||||
Tokenizer(const char **args, int nargs, bool stem_words = false) :
|
Tokenizer(const char **args, int nargs, bool stem_words = false) :
|
||||||
remove_diacritics(true), stem_words(stem_words), diacritics_remover(),
|
remove_diacritics(true), stem_words(stem_words), diacritics_remover(),
|
||||||
byte_offsets(), token_buf(), current_ui_language(""),
|
byte_offsets(), token_buf(), current_ui_language(""),
|
||||||
current_callback(NULL), current_callback_ctx(NULL), iterators(),
|
current_callback(NULL), current_callback_ctx(NULL),
|
||||||
|
iterators(), stemmers(),
|
||||||
|
|
||||||
constructor_error(SQLITE_OK)
|
constructor_error(SQLITE_OK)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user