* qt_helpers.cpp:
[lyx.git] / src / WordList.cpp
blob7dc620e0898e2fba99101d23a546021b118982bf
1 /**
2 * \file WordList.cpp
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.
9 */
11 #include <config.h>
13 #include "WordList.h"
15 #include "support/convert.h"
16 #include "support/debug.h"
17 #include "support/docstring.h"
18 #include "support/weighted_btree.h"
20 #include "support/lassert.h"
22 namespace lyx {
24 ///
25 WordList theGlobalWordList;
27 WordList & theWordList()
29 return theGlobalWordList;
32 ///
33 struct WordList::Impl {
34 ///
35 size_t c_;
36 ///
37 typedef stx::weighted_btree<docstring, size_t, int> Words;
38 ///
39 Words words_;
43 WordList::WordList()
45 d = new Impl;
46 d->c_ = 0;
48 #if 0
49 for (size_t i = 1000000; i > 0; --i) {
50 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
52 #endif
56 WordList::~WordList()
58 delete d;
62 docstring const & WordList::word(size_t idx) const
64 Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
65 LASSERT(it != d->words_.end(), /**/);
67 // We use the key() method here, and not something like it->first
68 // because the btree only returns (iterator-) temporary value pairs.
69 // If we returned the first component of those here, we get an
70 // invalid reference and therefore strange crashes.
71 return it.key();
75 size_t WordList::size() const
77 return d->words_.summed_weight();
81 void WordList::insert(docstring const & w)
83 Impl::Words::iterator it = d->words_.find(w);
84 if (it == d->words_.end())
85 d->words_.insert(w, size_t(1), 1);
86 else {
87 it.data()++;
88 d->words_.change_weight(it, 1);
93 void WordList::remove(docstring const & w)
95 Impl::Words::iterator it = d->words_.find(w);
96 if (it != d->words_.end()) {
97 it.data()--;
98 d->words_.change_weight(it, 0);
99 // We will not erase here, but instead we just leave it
100 // in the btree with weight 0. This avoid too much
101 // reorganisation of the tree all the time.
102 //if (it.data() == 0)
103 // d->words_.erase(w);
107 } // namespace lyx