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