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