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