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