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"
28 typedef map<string, WordList *> GlobalWordList;
29 GlobalWordList theGlobalWordList;
32 WordList * theWordList(string const & lang)
34 GlobalWordList::iterator it = theGlobalWordList.find(lang);
35 if (it != theGlobalWordList.end())
38 theGlobalWordList[lang] = new WordList;
39 return theGlobalWordList[lang];
43 void WordList::cleanupWordLists()
45 GlobalWordList::const_iterator it = theGlobalWordList.begin();
46 for (; it != theGlobalWordList.end(); ++it)
48 theGlobalWordList.clear();
53 struct WordList::Impl {
57 typedef stx::weighted_btree<docstring, size_t, int> Words;
69 for (size_t i = 1000000; i > 0; --i) {
70 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
82 docstring const & WordList::word(size_t idx) const
84 Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
85 LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
87 // We use the key() method here, and not something like it->first
88 // because the btree only returns (iterator-) temporary value pairs.
89 // If we returned the first component of those here, we get an
90 // invalid reference and therefore strange crashes.
95 size_t WordList::size() const
97 return d->words_.summed_weight();
101 void WordList::insert(docstring const & w)
103 Impl::Words::iterator it = d->words_.find(w);
104 if (it == d->words_.end())
105 d->words_.insert(w, size_t(1), 1);
108 d->words_.change_weight(it, 1);
113 void WordList::remove(docstring const & w)
115 Impl::Words::iterator it = d->words_.find(w);
116 if (it != d->words_.end()) {
118 d->words_.change_weight(it, 0);
119 // We will not erase here, but instead we just leave it
120 // in the btree with weight 0. This avoid too much
121 // reorganisation of the tree all the time.
122 //if (it.data() == 0)
123 // d->words_.erase(w);