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.
17 #include "support/convert.h"
18 #include "support/debug.h"
19 #include "support/docstring.h"
20 #include "support/weighted_btree.h"
22 #include "support/lassert.h"
31 map<Language, WordList *> theGlobalWordList;
34 WordList * theWordList(Language const & lang)
36 map<Language, WordList *>::iterator it = theGlobalWordList.find(lang);
37 if (it != theGlobalWordList.end())
40 theGlobalWordList[lang] = new WordList();
41 return theGlobalWordList[lang];
45 void WordList::cleanupWordLists() {
46 map<Language, WordList *>::const_iterator it = theGlobalWordList.begin();
47 for (; it != theGlobalWordList.end(); ++it)
49 theGlobalWordList.clear();
54 struct WordList::Impl {
58 typedef stx::weighted_btree<docstring, size_t, int> Words;
70 for (size_t i = 1000000; i > 0; --i) {
71 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
83 docstring const & WordList::word(size_t idx) const
85 Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
86 LASSERT(it != d->words_.end(), /**/);
88 // We use the key() method here, and not something like it->first
89 // because the btree only returns (iterator-) temporary value pairs.
90 // If we returned the first component of those here, we get an
91 // invalid reference and therefore strange crashes.
96 size_t WordList::size() const
98 return d->words_.summed_weight();
102 void WordList::insert(docstring const & w)
104 Impl::Words::iterator it = d->words_.find(w);
105 if (it == d->words_.end())
106 d->words_.insert(w, size_t(1), 1);
109 d->words_.change_weight(it, 1);
114 void WordList::remove(docstring const & w)
116 Impl::Words::iterator it = d->words_.find(w);
117 if (it != d->words_.end()) {
119 d->words_.change_weight(it, 0);
120 // We will not erase here, but instead we just leave it
121 // in the btree with weight 0. This avoid too much
122 // reorganisation of the tree all the time.
123 //if (it.data() == 0)
124 // d->words_.erase(w);