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