1 ////////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxList implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 ////////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 #pragma implementation "list.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
37 #include "wx/utils.h" // for copystring() (beurk...)
40 // Sun CC compatibility (interference with xview/pkg.h, apparently...)
41 // But XView is no longer supported.
43 #if defined (SUN_CC) || defined(__SUNCC__) && defined(__XVIEW__)
51 // =============================================================================
53 // =============================================================================
55 // -----------------------------------------------------------------------------
57 // -----------------------------------------------------------------------------
59 wxListKey wxDefaultListKey
;
61 bool wxListKey::operator==(wxListKeyValue value
) const
66 wxFAIL_MSG("bad key type.");
67 // let compiler optimize the line above away in release build
68 // by not putting return here...
71 return strcmp(m_key
.string
, value
.string
) == 0;
74 return m_key
.integer
== value
.integer
;
78 // -----------------------------------------------------------------------------
80 // -----------------------------------------------------------------------------
82 wxNodeBase::wxNodeBase(wxListBase
*list
,
83 wxNodeBase
*previous
, wxNodeBase
*next
,
84 void *data
, const wxListKey
& key
)
88 m_previous
= previous
;
91 switch ( key
.GetKeyType() )
97 m_key
.integer
= key
.GetNumber();
101 // to be free()d later
102 m_key
.string
= strdup(key
.GetString());
106 wxFAIL_MSG("invalid key type");
110 previous
->m_next
= this;
113 next
->m_previous
= this;
116 wxNodeBase::~wxNodeBase()
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
)
123 if ( m_list
->m_keyType
== wxKEY_STRING
)
128 m_list
->DetachNode(this);
132 int wxNodeBase::IndexOf() const
134 wxCHECK_MSG( m_list
, NOT_FOUND
, "node doesn't belong to a list in IndexOf");
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.
141 wxNodeBase
*prev
= m_previous
;
143 for( i
= 0; prev
; i
++ )
145 prev
= prev
->m_previous
;
151 // -----------------------------------------------------------------------------
153 // -----------------------------------------------------------------------------
155 void wxListBase::Init(wxKeyType keyType
)
158 m_nodeLast
= (wxNodeBase
*) NULL
;
164 wxListBase::wxListBase(size_t count
, void *elements
[])
168 for ( size_t n
= 0; n
< count
; n
++ )
174 void wxListBase::DoCopy(const wxListBase
& list
)
176 wxASSERT_MSG( !list
.m_destroy
,
177 "copying list which owns it's elements is a bad idea" );
179 m_count
= list
.m_count
;
180 m_destroy
= list
.m_destroy
;
181 m_keyType
= list
.m_keyType
;
183 m_nodeLast
= (wxNodeBase
*) NULL
;
185 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
191 wxListBase::~wxListBase()
193 wxNodeBase
*each
= m_nodeFirst
;
194 while ( each
!= NULL
)
196 wxNodeBase
*next
= each
->GetNext();
202 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
207 m_nodeLast
= m_nodeFirst
;
211 m_nodeLast
->m_next
= node
;
220 wxNodeBase
*wxListBase::Append(void *object
)
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" );
226 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
228 return AppendCommon(node
);
231 wxNodeBase
*wxListBase::Append(long key
, void *object
)
233 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
234 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
236 "can't append object with numeric key to this list" );
238 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
239 return AppendCommon(node
);
242 wxNodeBase
*wxListBase::Append (const char *key
, void *object
)
244 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
245 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
247 "can't append object with string key to this list" );
249 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
250 return AppendCommon(node
);
253 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
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" );
259 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
260 "can't insert before a node from another list" );
262 // previous and next node for the node being inserted
263 wxNodeBase
*prev
, *next
;
266 prev
= position
->GetPrevious();
271 // inserting in the beginning of the list
272 prev
= (wxNodeBase
*)NULL
;
276 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
292 wxNodeBase
*wxListBase::Item(size_t n
) const
294 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
302 wxFAIL_MSG( "invalid index in wxListBase::Item" );
304 return (wxNodeBase
*)NULL
;
307 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
309 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
310 "this list is not keyed on the type of this key" );
312 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
314 if ( key
== current
->m_key
)
321 return (wxNodeBase
*)NULL
;
324 wxNodeBase
*wxListBase::Find(void *object
) const
326 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
328 if ( current
->GetData() == object
)
333 return (wxNodeBase
*)NULL
;
336 int wxListBase::IndexOf(void *object
) const
338 wxNodeBase
*node
= Find( object
);
340 return node
? node
->IndexOf() : NOT_FOUND
;
343 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
346 if ( m_keyType
== wxKEY_STRING
)
348 free(node
->m_key
.string
);
356 // so that the node knows that it's being deleted by the list
361 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
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" );
368 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
370 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
373 *prevNext
= node
->GetNext();
374 *nextPrev
= node
->GetPrevious();
378 // mark the node as not belonging to this list any more
384 bool wxListBase::DeleteNode(wxNodeBase
*node
)
386 if ( !DetachNode(node
) )
394 bool wxListBase::DeleteObject(void *object
)
396 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
398 if ( current
->GetData() == object
)
409 void wxListBase::Clear()
411 wxNodeBase
*current
= m_nodeFirst
;
414 wxNodeBase
*next
= current
->GetNext();
415 DoDeleteNode(current
);
420 m_nodeLast
= (wxNodeBase
*)NULL
;
425 void wxListBase::ForEach(wxListIterateFunction F
)
427 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
429 (*F
)(current
->GetData());
433 void *wxListBase::FirstThat(wxListIterateFunction F
)
435 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
437 if ( (*F
)(current
->GetData()) )
438 return current
->GetData();
441 return (wxNodeBase
*)NULL
;
444 void *wxListBase::LastThat(wxListIterateFunction F
)
446 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
448 if ( (*F
)(current
->GetData()) )
449 return current
->GetData();
452 return (wxNodeBase
*)NULL
;
455 // (stefan.hammes@urz.uni-heidelberg.de)
457 // function for sorting lists. the concept is borrowed from 'qsort'.
458 // by giving a sort function, arbitrary lists can be sorted.
460 // - put wxObject pointers into an array
461 // - sort the array with qsort
462 // - put back the sorted wxObject pointers into the list
464 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
465 // so dereference right!
467 // int listcompare(const void *arg1, const void *arg2)
469 // return(compare(**(wxString **)arg1,
470 // **(wxString **)arg2));
477 // list.Append(new wxString("DEF"));
478 // list.Append(new wxString("GHI"));
479 // list.Append(new wxString("ABC"));
480 // list.Sort(listcompare);
483 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
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
;
490 // go through the list and put the pointers into the array
492 for ( node
= GetFirst(); node
; node
= node
->Next() )
494 *objPtr
++ = node
->Data();
498 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
500 // put the sorted pointers back into the list
502 for ( node
= GetFirst(); node
; node
= node
->Next() )
504 node
->SetData(*objPtr
++);
511 // -----------------------------------------------------------------------------
512 // wxList (a.k.a. wxObjectList)
513 // -----------------------------------------------------------------------------
515 void wxObjectListNode::DeleteData()
517 delete (wxObject
*)GetData();
520 // -----------------------------------------------------------------------------
522 // -----------------------------------------------------------------------------
524 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
526 void wxStringListNode::DeleteData()
528 delete [] (char *)GetData();
531 bool wxStringList::Delete(const char *s
)
533 wxStringListNode
*current
;
535 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
537 if ( strcmp(current
->GetData(), s
) == 0 )
548 void wxStringList::DoCopy(const wxStringList
& other
)
550 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
552 size_t count
= other
.GetCount();
553 for ( size_t n
= 0; n
< count
; n
++ )
555 Add(other
.Item(n
)->GetData());
559 // Variable argument list, terminated by a zero
560 // Makes new storage for the strings
561 wxStringList::wxStringList (const char *first
, ...)
569 const char *s
= first
;
574 s
= va_arg(ap
, const char *);
587 // Only makes new strings if arg is TRUE
588 char **wxStringList::ListToArray(bool new_copies
) const
590 char **string_array
= new char *[GetCount()];
591 wxStringListNode
*node
= GetFirst();
592 for (size_t i
= 0; i
< GetCount(); i
++)
594 char *s
= node
->GetData();
596 string_array
[i
] = copystring(s
);
599 node
= node
->GetNext();
605 // Checks whether s is a member of the list
606 bool wxStringList::Member(const char *s
) const
608 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
610 const char *s1
= node
->GetData();
611 if (s
== s1
|| strcmp (s
, s1
) == 0)
619 wx_comparestrings(const void *arg1
, const void *arg2
)
621 char **s1
= (char **) arg1
;
622 char **s2
= (char **) arg2
;
624 return strcmp (*s1
, *s2
);
627 // Sort a list of strings - deallocates old nodes, allocates new
628 void wxStringList::Sort()
630 size_t N
= GetCount();
631 char **array
= new char *[N
];
632 wxStringListNode
*node
;
635 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
637 array
[i
++] = node
->GetData();
640 qsort (array
, N
, sizeof (char *), wx_comparestrings
);
643 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
644 node
->SetData( array
[i
++] );