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(wxT("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(wxT("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
, wxT("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 wxT("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
;
179 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
181 key
= node
->GetKeyInteger();
182 Append(key
, node
->GetData());
190 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
192 key
= node
->GetKeyString();
193 Append(key
, node
->GetData());
200 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
202 Append(node
->GetData());
209 wxListBase::~wxListBase()
211 wxNodeBase
*each
= m_nodeFirst
;
212 while ( each
!= NULL
)
214 wxNodeBase
*next
= each
->GetNext();
220 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
225 m_nodeLast
= m_nodeFirst
;
229 m_nodeLast
->m_next
= node
;
238 wxNodeBase
*wxListBase::Append(void *object
)
240 // all objects in a keyed list should have a key
241 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
242 wxT("need a key for the object to append") );
244 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
246 return AppendCommon(node
);
249 wxNodeBase
*wxListBase::Append(long key
, void *object
)
251 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
252 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
254 wxT("can't append object with numeric key to this list") );
256 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
257 return AppendCommon(node
);
260 wxNodeBase
*wxListBase::Append (const wxChar
*key
, void *object
)
262 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
263 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
265 wxT("can't append object with string key to this list") );
267 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
268 return AppendCommon(node
);
271 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
273 // all objects in a keyed list should have a key
274 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
275 wxT("need a key for the object to insert") );
277 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
278 wxT("can't insert before a node from another list") );
280 // previous and next node for the node being inserted
281 wxNodeBase
*prev
, *next
;
284 prev
= position
->GetPrevious();
289 // inserting in the beginning of the list
290 prev
= (wxNodeBase
*)NULL
;
294 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
310 wxNodeBase
*wxListBase::Item(size_t n
) const
312 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
320 wxFAIL_MSG( wxT("invalid index in wxListBase::Item") );
322 return (wxNodeBase
*)NULL
;
325 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
327 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
328 wxT("this list is not keyed on the type of this key") );
330 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
332 if ( key
== current
->m_key
)
339 return (wxNodeBase
*)NULL
;
342 wxNodeBase
*wxListBase::Find(void *object
) const
344 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
346 if ( current
->GetData() == object
)
351 return (wxNodeBase
*)NULL
;
354 int wxListBase::IndexOf(void *object
) const
356 wxNodeBase
*node
= Find( object
);
358 return node
? node
->IndexOf() : wxNOT_FOUND
;
361 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
364 if ( m_keyType
== wxKEY_STRING
)
366 free(node
->m_key
.string
);
374 // so that the node knows that it's being deleted by the list
379 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
381 wxCHECK_MSG( node
, NULL
, wxT("detaching NULL wxNodeBase") );
382 wxCHECK_MSG( node
->m_list
== this, NULL
,
383 wxT("detaching node which is not from this list") );
386 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
388 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
391 *prevNext
= node
->GetNext();
392 *nextPrev
= node
->GetPrevious();
396 // mark the node as not belonging to this list any more
402 bool wxListBase::DeleteNode(wxNodeBase
*node
)
404 if ( !DetachNode(node
) )
412 bool wxListBase::DeleteObject(void *object
)
414 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
416 if ( current
->GetData() == object
)
427 void wxListBase::Clear()
429 wxNodeBase
*current
= m_nodeFirst
;
432 wxNodeBase
*next
= current
->GetNext();
433 DoDeleteNode(current
);
438 m_nodeLast
= (wxNodeBase
*)NULL
;
443 void wxListBase::ForEach(wxListIterateFunction F
)
445 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
447 (*F
)(current
->GetData());
451 void *wxListBase::FirstThat(wxListIterateFunction F
)
453 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
455 if ( (*F
)(current
->GetData()) )
456 return current
->GetData();
459 return (wxNodeBase
*)NULL
;
462 void *wxListBase::LastThat(wxListIterateFunction F
)
464 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
466 if ( (*F
)(current
->GetData()) )
467 return current
->GetData();
470 return (wxNodeBase
*)NULL
;
473 // (stefan.hammes@urz.uni-heidelberg.de)
475 // function for sorting lists. the concept is borrowed from 'qsort'.
476 // by giving a sort function, arbitrary lists can be sorted.
478 // - put wxObject pointers into an array
479 // - sort the array with qsort
480 // - put back the sorted wxObject pointers into the list
482 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
483 // so dereference right!
485 // int listcompare(const void *arg1, const void *arg2)
487 // return(compare(**(wxString **)arg1,
488 // **(wxString **)arg2));
495 // list.Append(new wxString("DEF"));
496 // list.Append(new wxString("GHI"));
497 // list.Append(new wxString("ABC"));
498 // list.Sort(listcompare);
501 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
503 // allocate an array for the wxObject pointers of the list
504 const size_t num
= GetCount();
505 void **objArray
= new void *[num
];
506 void **objPtr
= objArray
;
508 // go through the list and put the pointers into the array
510 for ( node
= GetFirst(); node
; node
= node
->Next() )
512 *objPtr
++ = node
->Data();
516 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
518 // put the sorted pointers back into the list
520 for ( node
= GetFirst(); node
; node
= node
->Next() )
522 node
->SetData(*objPtr
++);
529 // -----------------------------------------------------------------------------
530 // wxList (a.k.a. wxObjectList)
531 // -----------------------------------------------------------------------------
533 void wxObjectListNode::DeleteData()
535 delete (wxObject
*)GetData();
538 // -----------------------------------------------------------------------------
540 // -----------------------------------------------------------------------------
542 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
544 void wxStringListNode::DeleteData()
546 delete [] (char *)GetData();
549 bool wxStringList::Delete(const wxChar
*s
)
551 wxStringListNode
*current
;
553 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
555 if ( wxStrcmp(current
->GetData(), s
) == 0 )
566 void wxStringList::DoCopy(const wxStringList
& other
)
568 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
570 size_t count
= other
.GetCount();
571 for ( size_t n
= 0; n
< count
; n
++ )
573 Add(other
.Item(n
)->GetData());
577 // Variable argument list, terminated by a zero
578 // Makes new storage for the strings
579 wxStringList::wxStringList (const wxChar
*first
, ...)
587 const wxChar
*s
= first
;
592 s
= va_arg(ap
, const wxChar
*);
595 if ((int)(long) s
== 0)
605 // Only makes new strings if arg is TRUE
606 wxChar
**wxStringList::ListToArray(bool new_copies
) const
608 wxChar
**string_array
= new wxChar
*[GetCount()];
609 wxStringListNode
*node
= GetFirst();
610 for (size_t i
= 0; i
< GetCount(); i
++)
612 wxChar
*s
= node
->GetData();
614 string_array
[i
] = copystring(s
);
617 node
= node
->GetNext();
623 // Checks whether s is a member of the list
624 bool wxStringList::Member(const wxChar
*s
) const
626 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
628 const wxChar
*s1
= node
->GetData();
629 if (s
== s1
|| wxStrcmp (s
, s1
) == 0)
636 static int LINKAGEMODE
637 wx_comparestrings(const void *arg1
, const void *arg2
)
639 wxChar
**s1
= (wxChar
**) arg1
;
640 wxChar
**s2
= (wxChar
**) arg2
;
642 return wxStrcmp (*s1
, *s2
);
645 // Sort a list of strings - deallocates old nodes, allocates new
646 void wxStringList::Sort()
648 size_t N
= GetCount();
649 wxChar
**array
= new wxChar
*[N
];
650 wxStringListNode
*node
;
653 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
655 array
[i
++] = node
->GetData();
658 qsort (array
, N
, sizeof (wxChar
*), wx_comparestrings
);
661 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
662 node
->SetData( array
[i
++] );