3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Stefan Schimanski
8 * Full author contact details are available in file CREDITS.
15 #include "support/convert.h"
16 #include "support/debug.h"
17 #include "support/docstring.h"
18 #include "support/lassert.h"
19 #include "support/unique_ptr.h"
20 #include "support/weighted_btree.h"
22 #include <QThreadStorage>
31 typedef map<string, unique_ptr<WordList>> GlobalWordList;
32 // Each thread uses its own word list, but only the one of the GUI thread is
33 // used to do real work. The others are only neded to prevent simultanous
34 // write access e.g. from a cloned buffer and a true document buffer.
35 QThreadStorage<GlobalWordList *> theGlobalWordList;
38 WordList & theWordList(string const & lang)
40 if (!theGlobalWordList.hasLocalData())
41 theGlobalWordList.setLocalData(new GlobalWordList);
42 GlobalWordList & globalWordList = *theGlobalWordList.localData();
43 GlobalWordList::iterator it = globalWordList.find(lang);
44 if (it != globalWordList.end())
47 return *(globalWordList[lang] = make_unique<WordList>());
52 struct WordList::Impl {
56 typedef stx::weighted_btree<docstring, size_t, int> Words;
62 WordList::WordList() : d(make_unique<Impl>())
67 for (size_t i = 1000000; i > 0; --i) {
68 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
74 docstring const & WordList::word(size_t idx) const
76 Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
77 LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
79 // We use the key() method here, and not something like it->first
80 // because the btree only returns (iterator-) temporary value pairs.
81 // If we returned the first component of those here, we get an
82 // invalid reference and therefore strange crashes.
87 size_t WordList::size() const
89 return d->words_.summed_weight();
93 void WordList::insert(docstring const & w)
95 Impl::Words::iterator it = d->words_.find(w);
96 if (it == d->words_.end())
97 d->words_.insert(w, size_t(1), 1);
100 d->words_.change_weight(it, 1);
105 void WordList::remove(docstring const & w)
107 Impl::Words::iterator it = d->words_.find(w);
108 if (it != d->words_.end()) {
110 d->words_.change_weight(it, 0);
111 // We will not erase here, but instead we just leave it
112 // in the btree with weight 0. This avoid too much
113 // reorganisation of the tree all the time.
114 //if (it.data() == 0)
115 // d->words_.erase(w);