]> git.lyx.org Git - lyx.git/blob - src/WordList.cpp
Avoid full metrics computation with Update:FitCursor
[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/unique_ptr.h"
20 #include "support/weighted_btree.h"
21
22 #include <QThreadStorage>
23
24 #include <map>
25
26 using namespace std;
27
28 namespace lyx {
29
30 ///
31 typedef map<string, unique_ptr<WordList>> GlobalWordList;
32 // Each thread uses its own word list, but only the one of the GUI thread is
33 // used to do real work. The others are only neded to prevent simultanous
34 // write access e.g. from a cloned buffer and a true document buffer.
35 QThreadStorage<GlobalWordList *> theGlobalWordList;
36
37
38 WordList & theWordList(string const & lang)
39 {
40         if (!theGlobalWordList.hasLocalData())
41                 theGlobalWordList.setLocalData(new GlobalWordList);
42         GlobalWordList & globalWordList = *theGlobalWordList.localData();
43         GlobalWordList::iterator it = globalWordList.find(lang);
44         if (it != globalWordList.end())
45                 return *it->second;
46         else
47                 return *(globalWordList[lang] = make_unique<WordList>());
48 }
49
50
51 ///
52 struct WordList::Impl {
53         ///
54         size_t c_;
55         ///
56         typedef stx::weighted_btree<docstring, size_t, int> Words;
57         ///
58         Words words_;
59 };
60
61
62 WordList::WordList() : d(make_unique<Impl>())
63 {
64         d->c_ = 0;
65
66 #if 0
67         for (size_t i = 1000000; i > 0; --i) {
68                 d->words_.insert("a" + convert<docstring>(i), size_t(1), stx::Void());
69         }
70 #endif
71 }
72
73
74 docstring const & WordList::word(size_t idx) const
75 {
76         Impl::Words::const_iterator it = d->words_.find_summed_weight(idx);
77         LASSERT(it != d->words_.end(), { static docstring dummy; return dummy; });
78
79         // We use the key() method here, and not something like it->first
80         // because the btree only returns (iterator-) temporary value pairs.
81         // If we returned the first component of those here, we get an
82         // invalid reference and therefore strange crashes.
83         return it.key();
84 }
85
86
87 size_t WordList::size() const
88 {
89         return d->words_.summed_weight();
90 }
91
92
93 void WordList::insert(docstring const & w)
94 {
95         Impl::Words::iterator it = d->words_.find(w);
96         if (it == d->words_.end())
97                 d->words_.insert(w, size_t(1), 1);
98         else {
99                 it.data()++;
100                 d->words_.change_weight(it, 1);
101         }
102 }
103
104
105 void WordList::remove(docstring const & w)
106 {
107         Impl::Words::iterator it = d->words_.find(w);
108         if (it != d->words_.end()) {
109                 it.data()--;
110                 d->words_.change_weight(it, 0);
111                 // We will not erase here, but instead we just leave it
112                 // in the btree with weight 0. This avoid too much
113                 // reorganisation of the tree all the time.
114                 //if (it.data() == 0)
115                 //      d->words_.erase(w);
116         }
117 }
118
119 } // namespace lyx