1 /*-*- c++ -*-********************************************************
2 * kbList.cc : a double linked list *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
9 * Revision 1.1 1999/06/07 09:57:12 KB
10 * Formerly known as wxLayout.
12 * Revision 1.3 1998/11/19 20:34:50 KB
15 * Revision 1.8 1998/09/23 08:57:27 KB
16 * changed deletion behaviour
18 * Revision 1.7 1998/08/16 21:21:29 VZ
20 * 1) fixed config file bug: it was never created (attempt to create ~/.M/config
21 * always failed, must mkdir("~/.M") first)
22 * 2) "redesign" of "Folder properties" dialog and bug corrected, small change to
23 * MInputBox (it was too wide)
24 * 3) bug in ProvFC when it didn't reckognize the books as being in the correct
25 * format (=> messages "can't reopen book") corrected
26 * 4) I tried to enhance MDialog_About(), but it didn't really work... oh well,
27 * I've never said I was an artist
29 * Revision 1.6 1998/07/08 11:56:56 KB
30 * M compiles and runs on Solaris 2.5/gcc 2.8/c-client gso
32 * Revision 1.5 1998/06/27 20:07:18 KB
33 * several bug fixes for kbList
34 * started adding my layout stuff
36 * Revision 1.1.1.1 1998/06/13 21:51:12 karsten
39 * Revision 1.4 1998/05/24 14:48:00 KB
40 * lots of progress on Python, but cannot call functions yet
43 * Revision 1.3 1998/05/18 17:48:34 KB
44 * more list<>->kbList changes, fixes for wxXt, improved makefiles
46 * Revision 1.2 1998/05/14 16:39:31 VZ
48 * fixed SIGSEGV in ~kbList if the list is empty
50 * Revision 1.1 1998/05/13 19:02:11 KB
51 * added kbList, adapted MimeTypes for it, more python, new icons
53 *******************************************************************/
56 # pragma implementation "kbList.h"
62 kbListNode::kbListNode( void *ielement
,
75 kbListNode::~kbListNode()
84 kbList::iterator::iterator(kbListNode
*n
)
90 kbList::iterator::operator*()
96 kbList::iterator::operator++()
98 node
= node
? node
->next
: NULL
;
103 kbList::iterator::operator--()
105 node
= node
? node
->prev
: NULL
;
109 kbList::iterator::operator++(int /* foo */)
115 kbList::iterator::operator--(int /* bar */)
122 kbList::iterator::operator !=(kbList::iterator
const & i
) const
124 return node
!= i
.node
;
128 kbList::iterator::operator ==(kbList::iterator
const & i
) const
130 return node
== i
.node
;
133 kbList::kbList(bool ownsEntriesFlag
)
137 ownsEntries
= ownsEntriesFlag
;
141 kbList::push_back(void *element
)
143 if(! first
) // special case of empty list
145 first
= new kbListNode(element
);
150 last
= new kbListNode(element
, last
);
154 kbList::push_front(void *element
)
156 if(! first
) // special case of empty list
162 first
= new kbListNode(element
, NULL
, first
);
166 kbList::pop_back(void)
170 bool ownsFlagBak
= ownsEntries
;
175 ownsEntries
= ownsFlagBak
;
180 kbList::pop_front(void)
184 bool ownsFlagBak
= ownsEntries
;
190 ownsEntries
= ownsFlagBak
;
196 kbList::insert(kbList::iterator
& i
, void *element
)
200 else if(i
.Node() == first
)
206 i
= kbList::iterator(new kbListNode(element
, i
.Node()->prev
, i
.Node()));
210 kbList::doErase(kbList::iterator
& i
)
216 if(! node
) // illegal iterator
222 // correct first/last:
225 if(node
== last
) // don't put else here!
234 // delete this node and contents:
235 // now done separately
240 // change the iterator to next element:
241 i
= kbList::iterator(next
);
248 while ( first
!= NULL
)
252 delete first
->element
;
259 kbList::begin(void) const
261 return kbList::iterator(first
);
265 kbList::tail(void) const
267 return kbList::iterator(last
);
271 kbList::end(void) const
273 return kbList::iterator(NULL
); // the one after the last
277 kbList::size(void) const // inefficient
281 for(i
= begin(); i
!= end(); i
++, count
++)
294 #include <iostream.h>
296 KBLIST_DEFINE(kbListInt
,int);
307 for(n
= 0; n
< 10; n
++)
314 i
= l
.begin(); // first element
317 i
++; // 4th, insert here:
322 // this cannot work, because l.end() returns NULL:
323 i
= l
.end(); // behind last
324 i
--; // still behind last
325 l
.erase(i
); // doesn't do anything
328 i
= l
.tail(); // last element
331 l
.erase(i
); // erase 3rd last element (49)
333 for(i
= l
.begin(); i
!= l
.end(); i
++)
334 cout
<< *i
<< '\t' << *((int *)*i
) << endl
;