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;
76 WordList::WordList() : d(new Impl)
81 for (size_t i = 1000000; i > 0; --i) {
82 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
88 docstring const & WordList::word(size_t idx) const
90 Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
91 LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
93 // We use the key() method here, and not something like it->first
94 // because the btree only returns (iterator-) temporary value pairs.
95 // If we returned the first component of those here, we get an
96 // invalid reference and therefore strange crashes.
101 size_t WordList::size() const
103 return d->words_.summed_weight();
107 void WordList::insert(docstring const & w)
109 Impl::Words::iterator it = d->words_.find(w);
110 if (it == d->words_.end())
111 d->words_.insert(w, size_t(1), 1);
114 d->words_.change_weight(it, 1);
119 void WordList::remove(docstring const & w)
121 Impl::Words::iterator it = d->words_.find(w);
122 if (it != d->words_.end()) {
124 d->words_.change_weight(it, 0);
125 // We will not erase here, but instead we just leave it
126 // in the btree with weight 0. This avoid too much
127 // reorganisation of the tree all the time.
128 //if (it.data() == 0)
129 // d->words_.erase(w);