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