]> git.lyx.org Git - lyx.git/blob - src/WordList.cpp
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / WordList.cpp
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.
5  *
6  * \author Stefan Schimanski
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "WordList.h"
14
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"
20
21 #include <QThreadStorage>
22
23 #include <map>
24
25 using namespace std;
26
27 namespace lyx {
28
29 ///
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;
35
36
37 WordList * theWordList(string const & lang)
38 {
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())
44                 return it->second;
45         else {
46                 WordList * wl = new WordList;
47                 (*globalWordList)[lang] = wl;
48                 return wl;
49         }
50 }
51
52
53 void WordList::cleanupWordLists()
54 {
55         if (!theGlobalWordList.hasLocalData())
56                 return;
57         GlobalWordList * globalWordList = theGlobalWordList.localData();
58         GlobalWordList::const_iterator it = globalWordList->begin();
59         for (; it != globalWordList->end(); ++it)
60                 delete it->second;
61         globalWordList->clear();
62 }
63
64
65 ///
66 struct WordList::Impl {
67         ///
68         size_t c_;
69         ///
70         typedef stx::weighted_btree<docstring, size_t, int> Words;
71         ///
72         Words words_;
73 };
74
75
76 WordList::WordList() : d(new Impl)
77 {
78         d->c_ = 0;
79
80 #if 0
81         for (size_t i = 1000000; i > 0; --i) {
82                 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
83         }
84 #endif
85 }
86
87
88 docstring const & WordList::word(size_t idx) const
89 {
90         Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
91         LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
92         
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.
97         return it.key();
98 }
99
100
101 size_t WordList::size() const
102 {
103         return d->words_.summed_weight();
104 }
105
106
107 void WordList::insert(docstring const & w)
108 {
109         Impl::Words::iterator it = d->words_.find(w);
110         if (it == d->words_.end())
111                 d->words_.insert(w, size_t(1), 1);
112         else {
113                 it.data()++;
114                 d->words_.change_weight(it, 1);
115         }
116 }
117
118
119 void WordList::remove(docstring const & w)
120 {
121         Impl::Words::iterator it = d->words_.find(w);
122         if (it != d->words_.end()) {
123                 it.data()--;
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);
130         }
131 }
132
133 } // namespace lyx