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 // =============================================================================
42 // =============================================================================
44 // -----------------------------------------------------------------------------
46 // -----------------------------------------------------------------------------
48 wxListKey wxDefaultListKey
;
50 bool wxListKey::operator==(wxListKeyValue value
) const
55 wxFAIL_MSG(_T("bad key type."));
56 // let compiler optimize the line above away in release build
57 // by not putting return here...
60 return wxStrcmp(m_key
.string
, value
.string
) == 0;
63 return m_key
.integer
== value
.integer
;
67 // -----------------------------------------------------------------------------
69 // -----------------------------------------------------------------------------
71 wxNodeBase::wxNodeBase(wxListBase
*list
,
72 wxNodeBase
*previous
, wxNodeBase
*next
,
73 void *data
, const wxListKey
& key
)
77 m_previous
= previous
;
80 switch ( key
.GetKeyType() )
86 m_key
.integer
= key
.GetNumber();
90 // to be free()d later
91 m_key
.string
= wxStrdup(key
.GetString());
95 wxFAIL_MSG(_T("invalid key type"));
99 previous
->m_next
= this;
102 next
->m_previous
= this;
105 wxNodeBase::~wxNodeBase()
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
)
112 if ( m_list
->m_keyType
== wxKEY_STRING
)
117 m_list
->DetachNode(this);
121 int wxNodeBase::IndexOf() const
123 wxCHECK_MSG( m_list
, wxNOT_FOUND
, _T("node doesn't belong to a list in IndexOf"));
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.
130 wxNodeBase
*prev
= m_previous
;
132 for( i
= 0; prev
; i
++ )
134 prev
= prev
->m_previous
;
140 // -----------------------------------------------------------------------------
142 // -----------------------------------------------------------------------------
144 void wxListBase::Init(wxKeyType keyType
)
147 m_nodeLast
= (wxNodeBase
*) NULL
;
153 wxListBase::wxListBase(size_t count
, void *elements
[])
157 for ( size_t n
= 0; n
< count
; n
++ )
163 void wxListBase::DoCopy(const wxListBase
& list
)
165 wxASSERT_MSG( !list
.m_destroy
,
166 _T("copying list which owns it's elements is a bad idea") );
168 m_count
= list
.m_count
;
169 m_destroy
= list
.m_destroy
;
170 m_keyType
= list
.m_keyType
;
172 m_nodeLast
= (wxNodeBase
*) NULL
;
174 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
180 wxListBase::~wxListBase()
182 wxNodeBase
*each
= m_nodeFirst
;
183 while ( each
!= NULL
)
185 wxNodeBase
*next
= each
->GetNext();
191 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
196 m_nodeLast
= m_nodeFirst
;
200 m_nodeLast
->m_next
= node
;
209 wxNodeBase
*wxListBase::Append(void *object
)
211 // all objects in a keyed list should have a key
212 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
213 _T("need a key for the object to append") );
215 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
217 return AppendCommon(node
);
220 wxNodeBase
*wxListBase::Append(long key
, void *object
)
222 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
223 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
225 _T("can't append object with numeric key to this list") );
227 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
228 return AppendCommon(node
);
231 wxNodeBase
*wxListBase::Append (const wxChar
*key
, void *object
)
233 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
234 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
236 _T("can't append object with string key to this list") );
238 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
239 return AppendCommon(node
);
242 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
244 // all objects in a keyed list should have a key
245 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
246 _T("need a key for the object to insert") );
248 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
249 _T("can't insert before a node from another list") );
251 // previous and next node for the node being inserted
252 wxNodeBase
*prev
, *next
;
255 prev
= position
->GetPrevious();
260 // inserting in the beginning of the list
261 prev
= (wxNodeBase
*)NULL
;
265 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
281 wxNodeBase
*wxListBase::Item(size_t n
) const
283 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
291 wxFAIL_MSG( _T("invalid index in wxListBase::Item") );
293 return (wxNodeBase
*)NULL
;
296 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
298 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
299 _T("this list is not keyed on the type of this key") );
301 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
303 if ( key
== current
->m_key
)
310 return (wxNodeBase
*)NULL
;
313 wxNodeBase
*wxListBase::Find(void *object
) const
315 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
317 if ( current
->GetData() == object
)
322 return (wxNodeBase
*)NULL
;
325 int wxListBase::IndexOf(void *object
) const
327 wxNodeBase
*node
= Find( object
);
329 return node
? node
->IndexOf() : wxNOT_FOUND
;
332 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
335 if ( m_keyType
== wxKEY_STRING
)
337 free(node
->m_key
.string
);
345 // so that the node knows that it's being deleted by the list
350 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
352 wxCHECK_MSG( node
, NULL
, _T("detaching NULL wxNodeBase") );
353 wxCHECK_MSG( node
->m_list
== this, NULL
,
354 _T("detaching node which is not from this list") );
357 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
359 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
362 *prevNext
= node
->GetNext();
363 *nextPrev
= node
->GetPrevious();
367 // mark the node as not belonging to this list any more
373 bool wxListBase::DeleteNode(wxNodeBase
*node
)
375 if ( !DetachNode(node
) )
383 bool wxListBase::DeleteObject(void *object
)
385 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
387 if ( current
->GetData() == object
)
398 void wxListBase::Clear()
400 wxNodeBase
*current
= m_nodeFirst
;
403 wxNodeBase
*next
= current
->GetNext();
404 DoDeleteNode(current
);
409 m_nodeLast
= (wxNodeBase
*)NULL
;
414 void wxListBase::ForEach(wxListIterateFunction F
)
416 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
418 (*F
)(current
->GetData());
422 void *wxListBase::FirstThat(wxListIterateFunction F
)
424 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
426 if ( (*F
)(current
->GetData()) )
427 return current
->GetData();
430 return (wxNodeBase
*)NULL
;
433 void *wxListBase::LastThat(wxListIterateFunction F
)
435 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
437 if ( (*F
)(current
->GetData()) )
438 return current
->GetData();
441 return (wxNodeBase
*)NULL
;
444 // (stefan.hammes@urz.uni-heidelberg.de)
446 // function for sorting lists. the concept is borrowed from 'qsort'.
447 // by giving a sort function, arbitrary lists can be sorted.
449 // - put wxObject pointers into an array
450 // - sort the array with qsort
451 // - put back the sorted wxObject pointers into the list
453 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
454 // so dereference right!
456 // int listcompare(const void *arg1, const void *arg2)
458 // return(compare(**(wxString **)arg1,
459 // **(wxString **)arg2));
466 // list.Append(new wxString("DEF"));
467 // list.Append(new wxString("GHI"));
468 // list.Append(new wxString("ABC"));
469 // list.Sort(listcompare);
472 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
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
;
479 // go through the list and put the pointers into the array
481 for ( node
= GetFirst(); node
; node
= node
->Next() )
483 *objPtr
++ = node
->Data();
487 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
489 // put the sorted pointers back into the list
491 for ( node
= GetFirst(); node
; node
= node
->Next() )
493 node
->SetData(*objPtr
++);
500 // -----------------------------------------------------------------------------
501 // wxList (a.k.a. wxObjectList)
502 // -----------------------------------------------------------------------------
504 void wxObjectListNode::DeleteData()
506 delete (wxObject
*)GetData();
509 // -----------------------------------------------------------------------------
511 // -----------------------------------------------------------------------------
513 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
515 void wxStringListNode::DeleteData()
517 delete [] (char *)GetData();
520 bool wxStringList::Delete(const wxChar
*s
)
522 wxStringListNode
*current
;
524 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
526 if ( wxStrcmp(current
->GetData(), s
) == 0 )
537 void wxStringList::DoCopy(const wxStringList
& other
)
539 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
541 size_t count
= other
.GetCount();
542 for ( size_t n
= 0; n
< count
; n
++ )
544 Add(other
.Item(n
)->GetData());
548 // Variable argument list, terminated by a zero
549 // Makes new storage for the strings
550 wxStringList::wxStringList (const wxChar
*first
, ...)
558 const wxChar
*s
= first
;
563 s
= va_arg(ap
, const wxChar
*);
576 // Only makes new strings if arg is TRUE
577 wxChar
**wxStringList::ListToArray(bool new_copies
) const
579 wxChar
**string_array
= new wxChar
*[GetCount()];
580 wxStringListNode
*node
= GetFirst();
581 for (size_t i
= 0; i
< GetCount(); i
++)
583 wxChar
*s
= node
->GetData();
585 string_array
[i
] = copystring(s
);
588 node
= node
->GetNext();
594 // Checks whether s is a member of the list
595 bool wxStringList::Member(const wxChar
*s
) const
597 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
599 const wxChar
*s1
= node
->GetData();
600 if (s
== s1
|| wxStrcmp (s
, s1
) == 0)
608 wx_comparestrings(const void *arg1
, const void *arg2
)
610 wxChar
**s1
= (wxChar
**) arg1
;
611 wxChar
**s2
= (wxChar
**) arg2
;
613 return wxStrcmp (*s1
, *s2
);
616 // Sort a list of strings - deallocates old nodes, allocates new
617 void wxStringList::Sort()
619 size_t N
= GetCount();
620 wxChar
**array
= new wxChar
*[N
];
621 wxStringListNode
*node
;
624 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
626 array
[i
++] = node
->GetData();
629 qsort (array
, N
, sizeof (wxChar
*), wx_comparestrings
);
632 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
633 node
->SetData( array
[i
++] );