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/weighted_btree.h"
21 #include <QThreadStorage>
30 typedef map<string, WordList *> GlobalWordList;
31 // Each thread uses its own word list, but only the one of the GUI thread is
32 // used to do real work. The others are only neded to prevent simultanous
33 // write access e.g. from a cloned buffer and a true document buffer.
34 QThreadStorage<GlobalWordList *> theGlobalWordList;
37 WordList * theWordList(string const & lang)
39 if (!theGlobalWordList.hasLocalData())
40 theGlobalWordList.setLocalData(new GlobalWordList);
41 GlobalWordList * globalWordList = theGlobalWordList.localData();
42 GlobalWordList::iterator it = globalWordList->find(lang);
43 if (it != globalWordList->end())
46 WordList * wl = new WordList;
47 (*globalWordList)[lang] = wl;
53 void WordList::cleanupWordLists()
55 if (!theGlobalWordList.hasLocalData())
57 GlobalWordList * globalWordList = theGlobalWordList.localData();
58 GlobalWordList::const_iterator it = globalWordList->begin();
59 for (; it != globalWordList->end(); ++it)
61 globalWordList->clear();
66 struct WordList::Impl {
70 typedef stx::weighted_btree<docstring, size_t, int> Words;
82 for (size_t i = 1000000; i > 0; --i) {
83 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
95 docstring const & WordList::word(size_t idx) const
97 Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
98 LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
100 // We use the key() method here, and not something like it->first
101 // because the btree only returns (iterator-) temporary value pairs.
102 // If we returned the first component of those here, we get an
103 // invalid reference and therefore strange crashes.
108 size_t WordList::size() const
110 return d->words_.summed_weight();
114 void WordList::insert(docstring const & w)
116 Impl::Words::iterator it = d->words_.find(w);
117 if (it == d->words_.end())
118 d->words_.insert(w, size_t(1), 1);
121 d->words_.change_weight(it, 1);
126 void WordList::remove(docstring const & w)
128 Impl::Words::iterator it = d->words_.find(w);
129 if (it != d->words_.end()) {
131 d->words_.change_weight(it, 0);
132 // We will not erase here, but instead we just leave it
133 // in the btree with weight 0. This avoid too much
134 // reorganisation of the tree all the time.
135 //if (it.data() == 0)
136 // d->words_.erase(w);