]> git.lyx.org Git - lyx.git/blob - development/Win32/vld/src/set.h
Add support for glue length in parskip (#12867)
[lyx.git] / development / Win32 / vld / src / set.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //  Visual Leak Detector - Lightweight STL-like Set Template
4 //  Copyright (c) 2006 Dan Moulding
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License, or (at your option) any later version.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 //
20 //  See COPYING.txt for the full terms of the GNU Lesser General Public License.
21 //
22 ////////////////////////////////////////////////////////////////////////////////
23
24 #pragma once
25
26 #ifndef VLDBUILD
27 #error \
28 "This header should only be included by Visual Leak Detector when building it from source. \
29 Applications should never include this header."
30 #endif
31
32 #include "tree.h" // Provides access to the Tree template class.
33
34 ////////////////////////////////////////////////////////////////////////////////
35 //
36 //  The Set Template Class
37 //
38 //  This is a lightweight STL-like set template. It makes use of the Tree class
39 //  template to enable fast insert, find, and erase operations.
40 //
41 //  Note that while this is a STL-like class, it is not a full STL-compliant
42 //  implementation of the STL set container. It contains just the bare minimum
43 //  functionality required by Visual Leak Detector. Because of its "lightweight"
44 //  nature, this set class has a noticeable performance advantage over some
45 //  other standard STL set implementations.
46 //
47 template <typename Tk>
48 class Set {
49 public:
50     class Iterator {
51     public:
52         // Constructor
53         Iterator ()
54         {
55             // Plainly constructed iterators don't reference anything.
56             m_node = NULL;
57             m_tree = NULL;
58         }
59
60         // operator != - Inequality operator for Set Iterators. Two Set
61         //   Iterators are considered equal if and only if they both reference
62         //   the same key in the same Set.
63         //
64         //  - other (IN): The other Set Iterator to compare against.
65         //
66         //  Return Value:
67         //
68         //    Returns true if the specified Set Iterator is not equal to this
69         //    Set Iterator; otherwise, returns false.
70         //
71         BOOL operator != (const Iterator &other) const
72         {
73             return ((m_tree != other.m_tree) || (m_node != other.m_node));
74         }
75
76         // operator * - Dereference operator for Set Iterators.
77         //
78         //  Note:  The reference returned by this function is "const", so the
79         //    value referenced by the Iterator may not be modified through the
80         //    Iterator. This is a departure from STL iterator behavior.
81         //
82         //    Also, dereferencing an Iterator which does not reference a valid
83         //    value in the Set is undefined and will almost certainly cause a
84         //    crash.
85         //
86         //  Return Value:
87         //
88         //    Returns a const reference to the key in the Map referenced by the
89         //    Iterator.
90         //
91         const Tk& operator * () const
92         {
93             return m_node->key;
94         }
95
96         // operator ++ - Prefix increment operator for Set Iterators. Causes the
97         //   Iterator to reference the in-oder successor of the key currently
98         //   referenced by the Iterator. If the Iterator is currently
99         //   referencing the largest key in the Map, then the resulting Iterator
100         //   will reference the Set's end (the NULL pair).
101         //
102         //  Note: Incrementing an Iterator which does not reference a valid
103         //    key in the Set is undefined and will almost certainly cause a
104         //    crash.
105         //
106         //  Return Value:
107         //
108         //    Returns the Iterator after it has been incremented.
109         // 
110         Iterator& operator ++ (int)
111         {
112             m_node = m_tree->next(m_node);
113             return *this;
114         }
115
116         // operator ++ - Postfix increment operator for Map Iterators. Causes
117         //   the Iterator to reference the in-order successor of the key/value
118         //   pair currently referenced by the Iterator. If the Iterator is
119         //   currently referencing the largest key/value pair in the Map, then
120         //   the resulting Iterator will reference the Map's end (the NULL
121         //   pair).
122         //
123         //  Note: Incrementing an Iterator which does not reference a valid
124         //    key/value pair in the Map is undefined and will almost certainly
125         //    cause a crash.
126         //
127         //  Return Value:
128         //
129         //    Returns the Iterator before it has been incremented.
130         // 
131         Iterator operator ++ ()
132         {
133             typename Tree<Tk>::node_t *cur = m_node;
134
135             m_node = m_tree->next(m_node);
136             return Iterator(m_tree, cur);
137         }
138
139         // operator - - Subtraction operator for Set Iterators. Causes the
140         //   the Iterator to reference a key that is an in-order predecessor of
141         //   the currently refereced key.
142         //
143         //  - num (IN): Number indicating the number of preceding keys to
144         //      decrement the iterator.
145         //
146         //  Return Value:
147         //
148         //    Returns an Iterator referencing the key that precedes the original
149         //    Iterator by "num" keys.
150         //
151         Iterator operator - (SIZE_T num) const
152         {
153             SIZE_T                     count;
154             typename Tree<Tk>::node_t *cur = m_node;
155
156             cur = m_tree->prev(m_node);
157             for (count = 0; count < num; count++)  {
158                 cur = m_tree->prev(m_node);
159                 if (cur == NULL) {
160                     return Iterator(m_tree, NULL);
161                 }
162             }
163             return Iterator(m_tree, cur);
164         }
165
166         // operator == - Equality operator for Set Iterators. Set Iterators are
167         //   considered equal if and only if they both refernce the same
168         //   key in the same Set.
169         //
170         //  - other (IN): The other Set Iterator to compare against.
171         //
172         //  Return Value:
173         //
174         //    Returns true if the specified Set Iterator is equal to this Set
175         //    Iterator; otherwise returns false.
176         //
177         BOOL operator == (const Iterator &other) const
178         {
179             return ((m_tree == other.m_tree) && (m_node == other.m_node));
180         }
181
182     private:
183         // Private constructor. Only the Set class itself may use this
184         //   constructor. It is used for constructing Iterators which reference
185         //   specific nodes in the internal tree's structure.
186         Iterator (const Tree<Tk> *tree, typename Tree<Tk>::node_t *node)
187         {
188             m_node = node;
189             m_tree = tree;
190         }
191
192     protected:
193         typename Tree<Tk>::node_t *m_node; // Pointer to the node referenced by the Set Iterator.
194         const Tree<Tk>            *m_tree; // Pointer to the tree containing the referenced node.
195
196         // The Set class is a friend of Set Iterators.
197         friend class Set<Tk>;
198     };
199
200     // Muterator class - This class provides a mutable Iterator (the regular
201     // Iterators are const Iterators). By dereferencing a Muterator, you get
202     // a modifiable element.
203     //
204     //   Caution: Modifing an element in a way that changes its sorting value
205     //     will corrupt the Set container. Muterators should only be used when
206     //     you are absolutely certain you will not be using it to make a
207     //     modification that changes the sort order of the referenced element.
208     //
209     class Muterator : public Iterator
210     {
211     public:
212         // operator = - Assignment operator for Set Muterators. Can be used to
213         //   copy a Muterator from an existing Iterator, such that the Muterator
214         //   references the same element referenced by the Iterator.
215         Muterator& operator = (const Iterator& other) {
216             *(Iterator*)this = other;
217             return *this;
218         }
219
220         // operator * - Dereference operator for Set Muterators.
221         //
222         //  Note: Dereferencing a Muterator which does not reference a valid
223         //    value in the Set is undefined and will almost certainly cause a
224         //    crash.
225         //
226         //  Return Value:
227         //
228         //    Returns a reference to the key in the Map referenced by the
229         //    Muterator.
230         //
231         Tk& operator * ()
232         {
233             return m_node->key;
234         }
235     };
236
237     // begin - Obtains an Iterator referencing the beginning of the Set (i.e.
238     //   the lowest key currently stored in the Set).
239     //
240     //  Return Value:
241     //
242     //    Returns an Iterator referencing the first key in the Set. If no keys
243     //    are currenly stored in the Set, returns the "NULL" Iterator.
244     //
245     Iterator begin () const
246     {
247         return Iterator(&m_tree, m_tree.begin());
248     }
249
250     // end - Obtains an Iterator referencing the end of the Set. The end of
251     //   the Set does not reference an actual key. Instead it represents a
252     //   "null" key which signifies the end (i.e. just beyond largest key
253     //   currently stored in the Set). Also known as the "NULL" Iterator.
254     //
255     //  Return Value:
256     //
257     //    Returns the "NULL" Iterator, signifying the end of the Set.
258     //
259     Iterator end () const
260     {
261         return Iterator(&m_tree, NULL);
262     }
263
264     // erase - Erases a key from the Set.
265     //
266     //  - it (IN): Iterator referencing the key to be erased from the Set.
267     //
268     //  Return Value:
269     //
270     //    None.
271     //
272     VOID erase (Iterator& it)
273     {
274         m_tree.erase(it.m_node);
275     }
276
277     // erase - Erases a key from the Set.
278     //
279     //  - key (IN): The key to be erased from the Set.
280     //
281     //  Return Value:
282     //
283     //    None.
284     //
285     VOID erase (const Tk &key)
286     {
287         m_tree.erase(key);
288     }
289
290     // find - Finds a key in the Set.
291     //
292     //  - key (IN): The key to be found.
293     //
294     //  Return Value:
295     //
296     //    Returns an Iterator referencing the found key. If the key could not
297     //    be found, then the "NULL" Iterator is returned.
298     //
299     Iterator find (const Tk &key) const
300     {
301         return Iterator(&m_tree, m_tree.find(key));
302     }
303
304     // insert - Inserts a key into the Set.
305     //
306     //  - key (IN): The key to be inserted.
307     //
308     //  Return Value:
309     //
310     //    Returns an Iterator referencing the key after it has been inserted
311     //    into the Set. If an element with an identical key value already exists
312     //    in the Set, then the NULL Iterator is returned.
313     //
314     Iterator insert (const Tk &key)
315     {
316         return Iterator(&m_tree, m_tree.insert(key));
317     }
318
319     // reserve - Sets the reserve size of the Set. The reserve size is the
320     //   number of keys for which space should be pre-allocated to avoid
321     //   frequent heap hits when inserting new keys into the Set.
322     //
323     //  - count (IN): The number of keys for which to reserve space in advance.
324     //
325     //  Return Value:
326     //
327     //    Returns the reserve size previously in use by the Set.
328     //
329     size_t reserve (size_t count)
330     {
331         return m_tree.reserve(count);
332     }
333
334 private:
335     // Private data
336     Tree<Tk> m_tree; // The keys are actually stored in a tree.
337 };