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