]> git.lyx.org Git - lyx.git/blob - src/WordList.cpp
prepare Qt 5.6 builds
[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()
77 {
78         d = new Impl;
79         d->c_ = 0;
80
81 #if 0
82         for (size_t i = 1000000; i > 0; --i) {
83                 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
84         }
85 #endif
86 }
87
88
89 WordList::~WordList()
90 {
91         delete d;
92 }
93
94
95 docstring const & WordList::word(size_t idx) const
96 {
97         Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
98         LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
99         
100         // We use the key() method here, and not something like it->first
101         // because the btree only returns (iterator-) temporary value pairs.
102         // If we returned the first component of those here, we get an
103         // invalid reference and therefore strange crashes.
104         return it.key();
105 }
106
107
108 size_t WordList::size() const
109 {
110         return d->words_.summed_weight();
111 }
112
113
114 void WordList::insert(docstring const & w)
115 {
116         Impl::Words::iterator it = d->words_.find(w);
117         if (it == d->words_.end())
118                 d->words_.insert(w, size_t(1), 1);
119         else {
120                 it.data()++;
121                 d->words_.change_weight(it, 1);
122         }
123 }
124
125
126 void WordList::remove(docstring const & w)
127 {
128         Impl::Words::iterator it = d->words_.find(w);
129         if (it != d->words_.end()) {
130                 it.data()--;
131                 d->words_.change_weight(it, 0);
132                 // We will not erase here, but instead we just leave it
133                 // in the btree with weight 0. This avoid too much
134                 // reorganisation of the tree all the time.
135                 //if (it.data() == 0)
136                 //      d->words_.erase(w);
137         }
138 }
139
140 } // namespace lyx