]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/list.cpp
Removed 'interface' pragma for gcc 2.96
[wxWidgets.git] / src / common / list.cpp
... / ...
CommitLineData
1////////////////////////////////////////////////////////////////////////////////
2// Name: list.cpp
3// Purpose: wxList implementation
4// Author: Julian Smart
5// Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
10////////////////////////////////////////////////////////////////////////////////
11
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19#ifdef __GNUG__
20#pragma implementation "list.h"
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#include <stdarg.h>
31#include <stdlib.h>
32#include <string.h>
33
34#ifndef WX_PRECOMP
35 #include "wx/defs.h"
36 #include "wx/list.h"
37 #include "wx/utils.h" // for copystring() (beurk...)
38#endif
39
40// =============================================================================
41// implementation
42// =============================================================================
43
44// -----------------------------------------------------------------------------
45// wxListKey
46// -----------------------------------------------------------------------------
47
48wxListKey wxDefaultListKey;
49
50bool wxListKey::operator==(wxListKeyValue value) const
51{
52 switch ( m_keyType )
53 {
54 default:
55 wxFAIL_MSG(wxT("bad key type."));
56 // let compiler optimize the line above away in release build
57 // by not putting return here...
58
59 case wxKEY_STRING:
60 return wxStrcmp(m_key.string, value.string) == 0;
61
62 case wxKEY_INTEGER:
63 return m_key.integer == value.integer;
64 }
65}
66
67// -----------------------------------------------------------------------------
68// wxNodeBase
69// -----------------------------------------------------------------------------
70
71wxNodeBase::wxNodeBase(wxListBase *list,
72 wxNodeBase *previous, wxNodeBase *next,
73 void *data, const wxListKey& key)
74{
75 m_list = list;
76 m_data = data;
77 m_previous = previous;
78 m_next = next;
79
80 switch ( key.GetKeyType() )
81 {
82 case wxKEY_NONE:
83 break;
84
85 case wxKEY_INTEGER:
86 m_key.integer = key.GetNumber();
87 break;
88
89 case wxKEY_STRING:
90 // to be free()d later
91 m_key.string = wxStrdup(key.GetString());
92 break;
93
94 default:
95 wxFAIL_MSG(wxT("invalid key type"));
96 }
97
98 if ( previous )
99 previous->m_next = this;
100
101 if ( next )
102 next->m_previous = this;
103}
104
105wxNodeBase::~wxNodeBase()
106{
107 // handle the case when we're being deleted from the list by the user (i.e.
108 // not by the list itself from DeleteNode) - we must do it for
109 // compatibility with old code
110 if ( m_list != NULL )
111 {
112 if ( m_list->m_keyType == wxKEY_STRING )
113 {
114 free(m_key.string);
115 }
116
117 m_list->DetachNode(this);
118 }
119}
120
121int wxNodeBase::IndexOf() const
122{
123 wxCHECK_MSG( m_list, wxNOT_FOUND, wxT("node doesn't belong to a list in IndexOf"));
124
125 // It would be more efficient to implement IndexOf() completely inside
126 // wxListBase (only traverse the list once), but this is probably a more
127 // reusable way of doing it. Can always be optimized at a later date (since
128 // IndexOf() resides in wxListBase as well) if efficiency is a problem.
129 int i;
130 wxNodeBase *prev = m_previous;
131
132 for( i = 0; prev; i++ )
133 {
134 prev = prev->m_previous;
135 }
136
137 return i;
138}
139
140// -----------------------------------------------------------------------------
141// wxListBase
142// -----------------------------------------------------------------------------
143
144void wxListBase::Init(wxKeyType keyType)
145{
146 m_nodeFirst =
147 m_nodeLast = (wxNodeBase *) NULL;
148 m_count = 0;
149 m_destroy = FALSE;
150 m_keyType = keyType;
151}
152
153wxListBase::wxListBase(size_t count, void *elements[])
154{
155 Init();
156
157 for ( size_t n = 0; n < count; n++ )
158 {
159 Append(elements[n]);
160 }
161}
162
163void wxListBase::DoCopy(const wxListBase& list)
164{
165 wxASSERT_MSG( !list.m_destroy,
166 wxT("copying list which owns it's elements is a bad idea") );
167
168 m_destroy = list.m_destroy;
169 m_keyType = list.m_keyType;
170 m_nodeFirst =
171 m_nodeLast = (wxNodeBase *) NULL;
172
173 switch (m_keyType) {
174
175 case wxKEY_INTEGER:
176 {
177 long key;
178 for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() )
179 {
180 key = node->GetKeyInteger();
181 Append(key, node->GetData());
182 }
183 break;
184 }
185
186 case wxKEY_STRING:
187 {
188 const wxChar *key;
189 for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() )
190 {
191 key = node->GetKeyString();
192 Append(key, node->GetData());
193 }
194 break;
195 }
196
197 default:
198 {
199 for ( wxNodeBase *node = list.GetFirst(); node; node = node->GetNext() )
200 {
201 Append(node->GetData());
202 }
203 break;
204 }
205 }
206
207 wxASSERT_MSG( m_count == list.m_count, _T("logic error in wxList::DoCopy") );
208}
209
210wxListBase::~wxListBase()
211{
212 wxNodeBase *each = m_nodeFirst;
213 while ( each != NULL )
214 {
215 wxNodeBase *next = each->GetNext();
216 DoDeleteNode(each);
217 each = next;
218 }
219}
220
221wxNodeBase *wxListBase::AppendCommon(wxNodeBase *node)
222{
223 if ( !m_nodeFirst )
224 {
225 m_nodeFirst = node;
226 m_nodeLast = m_nodeFirst;
227 }
228 else
229 {
230 m_nodeLast->m_next = node;
231 m_nodeLast = node;
232 }
233
234 m_count++;
235
236 return node;
237}
238
239wxNodeBase *wxListBase::Append(void *object)
240{
241 // all objects in a keyed list should have a key
242 wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL,
243 wxT("need a key for the object to append") );
244
245 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object);
246
247 return AppendCommon(node);
248}
249
250wxNodeBase *wxListBase::Append(long key, void *object)
251{
252 wxCHECK_MSG( (m_keyType == wxKEY_INTEGER) ||
253 (m_keyType == wxKEY_NONE && m_count == 0),
254 (wxNodeBase *)NULL,
255 wxT("can't append object with numeric key to this list") );
256
257 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key);
258 return AppendCommon(node);
259}
260
261wxNodeBase *wxListBase::Append (const wxChar *key, void *object)
262{
263 wxCHECK_MSG( (m_keyType == wxKEY_STRING) ||
264 (m_keyType == wxKEY_NONE && m_count == 0),
265 (wxNodeBase *)NULL,
266 wxT("can't append object with string key to this list") );
267
268 wxNodeBase *node = CreateNode(m_nodeLast, (wxNodeBase *)NULL, object, key);
269 return AppendCommon(node);
270}
271
272wxNodeBase *wxListBase::Insert(wxNodeBase *position, void *object)
273{
274 // all objects in a keyed list should have a key
275 wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL,
276 wxT("need a key for the object to insert") );
277
278 wxCHECK_MSG( !position || position->m_list == this, (wxNodeBase *)NULL,
279 wxT("can't insert before a node from another list") );
280
281 // previous and next node for the node being inserted
282 wxNodeBase *prev, *next;
283 if ( position )
284 {
285 prev = position->GetPrevious();
286 next = position;
287 }
288 else
289 {
290 // inserting in the beginning of the list
291 prev = (wxNodeBase *)NULL;
292 next = m_nodeFirst;
293 }
294
295 wxNodeBase *node = CreateNode(prev, next, object);
296 if ( !m_nodeFirst )
297 {
298 m_nodeLast = node;
299 }
300
301 if ( prev == NULL )
302 {
303 m_nodeFirst = node;
304 }
305
306 m_count++;
307
308 return node;
309}
310
311wxNodeBase *wxListBase::Item(size_t n) const
312{
313 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
314 {
315 if ( n-- == 0 )
316 {
317 return current;
318 }
319 }
320
321 wxFAIL_MSG( wxT("invalid index in wxListBase::Item") );
322
323 return (wxNodeBase *)NULL;
324}
325
326wxNodeBase *wxListBase::Find(const wxListKey& key) const
327{
328 wxASSERT_MSG( m_keyType == key.GetKeyType(),
329 wxT("this list is not keyed on the type of this key") );
330
331 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
332 {
333 if ( key == current->m_key )
334 {
335 return current;
336 }
337 }
338
339 // not found
340 return (wxNodeBase *)NULL;
341}
342
343wxNodeBase *wxListBase::Find(void *object) const
344{
345 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
346 {
347 if ( current->GetData() == object )
348 return current;
349 }
350
351 // not found
352 return (wxNodeBase *)NULL;
353}
354
355int wxListBase::IndexOf(void *object) const
356{
357 wxNodeBase *node = Find( object );
358
359 return node ? node->IndexOf() : wxNOT_FOUND;
360}
361
362void wxListBase::DoDeleteNode(wxNodeBase *node)
363{
364 // free node's data
365 if ( m_keyType == wxKEY_STRING )
366 {
367 free(node->m_key.string);
368 }
369
370 if ( m_destroy )
371 {
372 node->DeleteData();
373 }
374
375 // so that the node knows that it's being deleted by the list
376 node->m_list = NULL;
377 delete node;
378}
379
380wxNodeBase *wxListBase::DetachNode(wxNodeBase *node)
381{
382 wxCHECK_MSG( node, NULL, wxT("detaching NULL wxNodeBase") );
383 wxCHECK_MSG( node->m_list == this, NULL,
384 wxT("detaching node which is not from this list") );
385
386 // update the list
387 wxNodeBase **prevNext = node->GetPrevious() ? &node->GetPrevious()->m_next
388 : &m_nodeFirst;
389 wxNodeBase **nextPrev = node->GetNext() ? &node->GetNext()->m_previous
390 : &m_nodeLast;
391
392 *prevNext = node->GetNext();
393 *nextPrev = node->GetPrevious();
394
395 m_count--;
396
397 // mark the node as not belonging to this list any more
398 node->m_list = NULL;
399
400 return node;
401}
402
403bool wxListBase::DeleteNode(wxNodeBase *node)
404{
405 if ( !DetachNode(node) )
406 return FALSE;
407
408 DoDeleteNode(node);
409
410 return TRUE;
411}
412
413bool wxListBase::DeleteObject(void *object)
414{
415 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
416 {
417 if ( current->GetData() == object )
418 {
419 DeleteNode(current);
420 return TRUE;
421 }
422 }
423
424 // not found
425 return FALSE;
426}
427
428void wxListBase::Clear()
429{
430 wxNodeBase *current = m_nodeFirst;
431 while ( current )
432 {
433 wxNodeBase *next = current->GetNext();
434 DoDeleteNode(current);
435 current = next;
436 }
437
438 m_nodeFirst =
439 m_nodeLast = (wxNodeBase *)NULL;
440
441 m_count = 0;
442}
443
444void wxListBase::ForEach(wxListIterateFunction F)
445{
446 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
447 {
448 (*F)(current->GetData());
449 }
450}
451
452void *wxListBase::FirstThat(wxListIterateFunction F)
453{
454 for ( wxNodeBase *current = GetFirst(); current; current = current->GetNext() )
455 {
456 if ( (*F)(current->GetData()) )
457 return current->GetData();
458 }
459
460 return (wxNodeBase *)NULL;
461}
462
463void *wxListBase::LastThat(wxListIterateFunction F)
464{
465 for ( wxNodeBase *current = GetLast(); current; current = current->GetPrevious() )
466 {
467 if ( (*F)(current->GetData()) )
468 return current->GetData();
469 }
470
471 return (wxNodeBase *)NULL;
472}
473
474// (stefan.hammes@urz.uni-heidelberg.de)
475//
476// function for sorting lists. the concept is borrowed from 'qsort'.
477// by giving a sort function, arbitrary lists can be sorted.
478// method:
479// - put wxObject pointers into an array
480// - sort the array with qsort
481// - put back the sorted wxObject pointers into the list
482//
483// CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
484// so dereference right!
485// EXAMPLE:
486// int listcompare(const void *arg1, const void *arg2)
487// {
488// return(compare(**(wxString **)arg1,
489// **(wxString **)arg2));
490// }
491//
492// void main()
493// {
494// wxListBase list;
495//
496// list.Append(new wxString("DEF"));
497// list.Append(new wxString("GHI"));
498// list.Append(new wxString("ABC"));
499// list.Sort(listcompare);
500// }
501
502void wxListBase::Sort(const wxSortCompareFunction compfunc)
503{
504 // allocate an array for the wxObject pointers of the list
505 const size_t num = GetCount();
506 void **objArray = new void *[num];
507 void **objPtr = objArray;
508
509 // go through the list and put the pointers into the array
510 wxNodeBase *node;
511 for ( node = GetFirst(); node; node = node->Next() )
512 {
513 *objPtr++ = node->Data();
514 }
515
516 // sort the array
517 qsort((void *)objArray,num,sizeof(wxObject *),compfunc);
518
519 // put the sorted pointers back into the list
520 objPtr = objArray;
521 for ( node = GetFirst(); node; node = node->Next() )
522 {
523 node->SetData(*objPtr++);
524 }
525
526 // free the array
527 delete[] objArray;
528}
529
530// -----------------------------------------------------------------------------
531// wxList (a.k.a. wxObjectList)
532// -----------------------------------------------------------------------------
533
534void wxObjectListNode::DeleteData()
535{
536 delete (wxObject *)GetData();
537}
538
539// -----------------------------------------------------------------------------
540// wxStringList
541// -----------------------------------------------------------------------------
542
543// instead of WX_DEFINE_LIST(wxStringListBase) we define this function
544// ourselves
545void wxStringListNode::DeleteData()
546{
547 delete [] (char *)GetData();
548}
549
550bool wxStringList::Delete(const wxChar *s)
551{
552 wxStringListNode *current;
553
554 for ( current = GetFirst(); current; current = current->GetNext() )
555 {
556 if ( wxStrcmp(current->GetData(), s) == 0 )
557 {
558 DeleteNode(current);
559 return TRUE;
560 }
561 }
562
563 // not found
564 return FALSE;
565}
566
567void wxStringList::DoCopy(const wxStringList& other)
568{
569 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
570
571 size_t count = other.GetCount();
572 for ( size_t n = 0; n < count; n++ )
573 {
574 Add(other.Item(n)->GetData());
575 }
576}
577
578// Variable argument list, terminated by a zero
579// Makes new storage for the strings
580wxStringList::wxStringList (const wxChar *first, ...)
581{
582 DeleteContents(TRUE);
583 if ( !first )
584 return;
585
586 va_list ap;
587 va_start(ap, first);
588
589 const wxChar *s = first;
590 for (;;)
591 {
592 Add(s);
593
594 s = va_arg(ap, const wxChar *);
595 // if (s == NULL)
596#ifdef __WXMSW__
597 if ((int)(long) s == 0)
598#else
599 if ((long) s == 0)
600#endif
601 break;
602 }
603
604 va_end(ap);
605}
606
607// Only makes new strings if arg is TRUE
608wxChar **wxStringList::ListToArray(bool new_copies) const
609{
610 wxChar **string_array = new wxChar *[GetCount()];
611 wxStringListNode *node = GetFirst();
612 for (size_t i = 0; i < GetCount(); i++)
613 {
614 wxChar *s = node->GetData();
615 if ( new_copies )
616 string_array[i] = copystring(s);
617 else
618 string_array[i] = s;
619 node = node->GetNext();
620 }
621
622 return string_array;
623}
624
625// Checks whether s is a member of the list
626bool wxStringList::Member(const wxChar *s) const
627{
628 for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() )
629 {
630 const wxChar *s1 = node->GetData();
631 if (s == s1 || wxStrcmp (s, s1) == 0)
632 return TRUE;
633 }
634
635 return FALSE;
636}
637
638static int LINKAGEMODE
639wx_comparestrings(const void *arg1, const void *arg2)
640{
641 wxChar **s1 = (wxChar **) arg1;
642 wxChar **s2 = (wxChar **) arg2;
643
644 return wxStrcmp (*s1, *s2);
645}
646
647// Sort a list of strings - deallocates old nodes, allocates new
648void wxStringList::Sort()
649{
650 size_t N = GetCount();
651 wxChar **array = new wxChar *[N];
652 wxStringListNode *node;
653
654 size_t i = 0;
655 for ( node = GetFirst(); node; node = node->GetNext() )
656 {
657 array[i++] = node->GetData();
658 }
659
660 qsort (array, N, sizeof (wxChar *), wx_comparestrings);
661
662 i = 0;
663 for ( node = GetFirst(); node; node = node->GetNext() )
664 node->SetData( array[i++] );
665
666 delete [] array;
667}