]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/kbList.cpp
Undo the link creation in builddir. contrib is still going to be
[wxWidgets.git] / samples / richedit / kbList.cpp
1 /*-*- c++ -*-********************************************************
2 * kbList.cc : a double linked list *
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$ *
7 * *
8 * $Log$
9 * Revision 1.3 2004/08/06 17:27:18 ABX
10 * Deleting void is undefined.
11 *
12 * Revision 1.2 2002/01/16 13:39:50 GT
13 * Added checks for wxUSE_IOSTREAMH to determine which iostream(.h) to use
14 *
15 * Revision 1.1 1999/06/07 09:57:12 KB
16 * Formerly known as wxLayout.
17 *
18 * Revision 1.3 1998/11/19 20:34:50 KB
19 * fixes
20 *
21 * Revision 1.8 1998/09/23 08:57:27 KB
22 * changed deletion behaviour
23 *
24 * Revision 1.7 1998/08/16 21:21:29 VZ
25 *
26 * 1) fixed config file bug: it was never created (attempt to create ~/.M/config
27 * always failed, must mkdir("~/.M") first)
28 * 2) "redesign" of "Folder properties" dialog and bug corrected, small change to
29 * MInputBox (it was too wide)
30 * 3) bug in ProvFC when it didn't reckognize the books as being in the correct
31 * format (=> messages "can't reopen book") corrected
32 * 4) I tried to enhance MDialog_About(), but it didn't really work... oh well,
33 * I've never said I was an artist
34 *
35 * Revision 1.6 1998/07/08 11:56:56 KB
36 * M compiles and runs on Solaris 2.5/gcc 2.8/c-client gso
37 *
38 * Revision 1.5 1998/06/27 20:07:18 KB
39 * several bug fixes for kbList
40 * started adding my layout stuff
41 *
42 * Revision 1.1.1.1 1998/06/13 21:51:12 karsten
43 * initial code
44 *
45 * Revision 1.4 1998/05/24 14:48:00 KB
46 * lots of progress on Python, but cannot call functions yet
47 * kbList fixes again?
48 *
49 * Revision 1.3 1998/05/18 17:48:34 KB
50 * more list<>->kbList changes, fixes for wxXt, improved makefiles
51 *
52 * Revision 1.2 1998/05/14 16:39:31 VZ
53 *
54 * fixed SIGSEGV in ~kbList if the list is empty
55 *
56 * Revision 1.1 1998/05/13 19:02:11 KB
57 * added kbList, adapted MimeTypes for it, more python, new icons
58 *
59 *******************************************************************/
60
61 #ifdef __GNUG__
62 # pragma implementation "kbList.h"
63 #endif
64
65 #include "wx/wxprec.h"
66
67 #ifdef __BORLANDC__
68 # pragma hdrstop
69 #endif
70
71 #include "wx/wx.h"
72
73 #include "kbList.h"
74
75
76 kbListNode::kbListNode( void *ielement,
77 kbListNode *iprev,
78 kbListNode *inext)
79 {
80 next = inext;
81 prev = iprev;
82 if(prev)
83 prev->next = this;
84 if(next)
85 next->prev = this;
86 element = ielement;
87 }
88
89 kbListNode::~kbListNode()
90 {
91 if(prev)
92 prev->next = next;
93 if(next)
94 next->prev = prev;
95 }
96
97
98 kbList::iterator::iterator(kbListNode *n)
99 {
100 node = n;
101 }
102
103 void *
104 kbList::iterator::operator*()
105 {
106 return node->element;
107 }
108
109 kbList::iterator &
110 kbList::iterator::operator++()
111 {
112 node = node ? node->next : NULL;
113 return *this;
114 }
115
116 kbList::iterator &
117 kbList::iterator::operator--()
118 {
119 node = node ? node->prev : NULL;
120 return *this;
121 }
122 kbList::iterator &
123 kbList::iterator::operator++(int /* foo */)
124 {
125 return operator++();
126 }
127
128 kbList::iterator &
129 kbList::iterator::operator--(int /* bar */)
130 {
131 return operator--();
132 }
133
134
135 bool
136 kbList::iterator::operator !=(kbList::iterator const & i) const
137 {
138 return node != i.node;
139 }
140
141 bool
142 kbList::iterator::operator ==(kbList::iterator const & i) const
143 {
144 return node == i.node;
145 }
146
147 kbList::kbList(bool ownsEntriesFlag)
148 {
149 first = NULL;
150 last = NULL;
151 ownsEntries = ownsEntriesFlag;
152 }
153
154 void
155 kbList::push_back(void *element)
156 {
157 if(! first) // special case of empty list
158 {
159 first = new kbListNode(element);
160 last = first;
161 return;
162 }
163 else
164 last = new kbListNode(element, last);
165 }
166
167 void
168 kbList::push_front(void *element)
169 {
170 if(! first) // special case of empty list
171 {
172 push_back(element);
173 return;
174 }
175 else
176 first = new kbListNode(element, NULL, first);
177 }
178
179 void *
180 kbList::pop_back(void)
181 {
182 iterator i;
183 void *data;
184 bool ownsFlagBak = ownsEntries;
185 i = tail();
186 data = *i;
187 ownsEntries = false;
188 erase(i);
189 ownsEntries = ownsFlagBak;
190 return data;
191 }
192
193 void *
194 kbList::pop_front(void)
195 {
196 iterator i;
197 void *data;
198 bool ownsFlagBak = ownsEntries;
199
200 i = begin();
201 data = *i;
202 ownsEntries = false;
203 erase(i);
204 ownsEntries = ownsFlagBak;
205 return data;
206
207 }
208
209 void
210 kbList::insert(kbList::iterator & i, void *element)
211 {
212 if(! i.Node())
213 return;
214 else if(i.Node() == first)
215 {
216 push_front(element);
217 i = first;
218 return;
219 }
220 i = kbList::iterator(new kbListNode(element, i.Node()->prev, i.Node()));
221 }
222
223 void
224 kbList::doErase(kbList::iterator & i)
225 {
226 kbListNode
227 *node = i.Node(),
228 *prev, *next;
229
230 if(! node) // illegal iterator
231 return;
232
233 prev = node->prev;
234 next = node->next;
235
236 // correct first/last:
237 if(node == first)
238 first = node->next;
239 if(node == last) // don't put else here!
240 last = node->prev;
241
242 // build new links:
243 if(prev)
244 prev->next = next;
245 if(next)
246 next->prev = prev;
247
248 // delete this node and contents:
249 // now done separately
250 //if(ownsEntries)
251 //delete *i;
252 delete i.Node();
253
254 // change the iterator to next element:
255 i = kbList::iterator(next);
256 }
257
258 kbList::~kbList()
259 {
260 kbListNode *next;
261
262 while ( first != NULL )
263 {
264 next = first->next;
265 if(ownsEntries)
266 {
267 #if 0
268 delete first->element;
269 #else
270 wxLogError(wxT("Deleting `void*' is undefined."));
271 wxLogError(wxT("Entries of kbList should be deleted by destructors of derived classes."));
272 #endif
273 }
274 delete first;
275 first = next;
276 }
277 }
278
279 kbList::iterator
280 kbList::begin(void) const
281 {
282 return kbList::iterator(first);
283 }
284
285 kbList::iterator
286 kbList::tail(void) const
287 {
288 return kbList::iterator(last);
289 }
290
291 kbList::iterator
292 kbList::end(void) const
293 {
294 return kbList::iterator(NULL); // the one after the last
295 }
296
297 unsigned
298 kbList::size(void) const // inefficient
299 {
300 unsigned count = 0;
301 kbList::iterator i;
302 for(i = begin(); i != end(); i++, count++)
303 ;
304 return count;
305 }
306
307
308
309
310
311
312
313 #ifdef KBLIST_TEST
314
315 #if wxUSE_IOSTREAMH
316 #include <iostream.h>
317 #else
318 #include <iostream>
319 #endif
320
321 KBLIST_DEFINE(kbListInt,int);
322
323 int main(void)
324 {
325 int
326 n, *ptr;
327 kbListInt
328 l;
329 kbListInt::iterator
330 i;
331
332 for(n = 0; n < 10; n++)
333 {
334 ptr = new int;
335 *ptr = n*n;
336 l.push_back(ptr);
337 }
338
339 i = l.begin(); // first element
340 i++; // 2nd
341 i++; // 3rd
342 i++; // 4th, insert here:
343 ptr = new int;
344 *ptr = 4444;
345 l.insert(i,ptr);
346
347 // this cannot work, because l.end() returns NULL:
348 i = l.end(); // behind last
349 i--; // still behind last
350 l.erase(i); // doesn't do anything
351
352 // this works:
353 i = l.tail(); // last element
354 i--;
355 --i;
356 l.erase(i); // erase 3rd last element (49)
357
358 for(i = l.begin(); i != l.end(); i++)
359 cout << *i << '\t' << *((int *)*i) << endl;
360
361
362 return 0;
363 }
364 #endif