1 ////////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/list.cpp
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
9 // Licence: wxWindows licence
10 ////////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
37 // =============================================================================
39 // =============================================================================
41 // -----------------------------------------------------------------------------
43 // -----------------------------------------------------------------------------
44 wxListKey wxDefaultListKey
;
46 bool wxListKey::operator==(wxListKeyValue value
) const
51 wxFAIL_MSG(wxT("bad key type."));
52 // let compiler optimize the line above away in release build
53 // by not putting return here...
56 return *m_key
.string
== *value
.string
;
59 return m_key
.integer
== value
.integer
;
63 // -----------------------------------------------------------------------------
65 // -----------------------------------------------------------------------------
67 wxNodeBase::wxNodeBase(wxListBase
*list
,
68 wxNodeBase
*previous
, wxNodeBase
*next
,
69 void *data
, const wxListKey
& key
)
73 m_previous
= previous
;
76 switch ( key
.GetKeyType() )
82 m_key
.integer
= key
.GetNumber();
86 // to be free()d later
87 m_key
.string
= new wxString(key
.GetString());
91 wxFAIL_MSG(wxT("invalid key type"));
95 previous
->m_next
= this;
98 next
->m_previous
= this;
101 wxNodeBase::~wxNodeBase()
103 // handle the case when we're being deleted from the list by the user (i.e.
104 // not by the list itself from DeleteNode) - we must do it for
105 // compatibility with old code
106 if ( m_list
!= NULL
)
108 if ( m_list
->m_keyType
== wxKEY_STRING
)
113 m_list
->DetachNode(this);
117 int wxNodeBase::IndexOf() const
119 wxCHECK_MSG( m_list
, wxNOT_FOUND
, wxT("node doesn't belong to a list in IndexOf"));
121 // It would be more efficient to implement IndexOf() completely inside
122 // wxListBase (only traverse the list once), but this is probably a more
123 // reusable way of doing it. Can always be optimized at a later date (since
124 // IndexOf() resides in wxListBase as well) if efficiency is a problem.
126 wxNodeBase
*prev
= m_previous
;
128 for( i
= 0; prev
; i
++ )
130 prev
= prev
->m_previous
;
136 // -----------------------------------------------------------------------------
138 // -----------------------------------------------------------------------------
140 void wxListBase::Init(wxKeyType keyType
)
143 m_nodeLast
= (wxNodeBase
*) NULL
;
149 wxListBase::wxListBase(size_t count
, void *elements
[])
153 for ( size_t n
= 0; n
< count
; n
++ )
159 void wxListBase::DoCopy(const wxListBase
& list
)
161 wxASSERT_MSG( !list
.m_destroy
,
162 wxT("copying list which owns it's elements is a bad idea") );
164 m_destroy
= list
.m_destroy
;
165 m_keyType
= list
.m_keyType
;
167 m_nodeLast
= (wxNodeBase
*) NULL
;
174 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
176 key
= node
->GetKeyInteger();
177 Append(key
, node
->GetData());
185 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
187 key
= node
->GetKeyString();
188 Append(key
, node
->GetData());
195 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
197 Append(node
->GetData());
203 wxASSERT_MSG( m_count
== list
.m_count
, _T("logic error in wxList::DoCopy") );
206 wxListBase::~wxListBase()
208 wxNodeBase
*each
= m_nodeFirst
;
209 while ( each
!= NULL
)
211 wxNodeBase
*next
= each
->GetNext();
217 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
222 m_nodeLast
= m_nodeFirst
;
226 m_nodeLast
->m_next
= node
;
235 wxNodeBase
*wxListBase::Append(void *object
)
237 // all objects in a keyed list should have a key
238 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
239 wxT("need a key for the object to append") );
241 // we use wxDefaultListKey even though it is the default parameter value
242 // because gcc under Mac OS X seems to miscompile this call otherwise
243 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 wxString
& 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 // wxDefaultListKey: see comment in Append() above
295 wxNodeBase
*node
= CreateNode(prev
, next
, object
, wxDefaultListKey
);
311 wxNodeBase
*wxListBase::Item(size_t n
) const
313 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
321 wxFAIL_MSG( wxT("invalid index in wxListBase::Item") );
323 return (wxNodeBase
*)NULL
;
326 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
328 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
329 wxT("this list is not keyed on the type of this key") );
331 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
333 if ( key
== current
->m_key
)
340 return (wxNodeBase
*)NULL
;
343 wxNodeBase
*wxListBase::Find(const void *object
) const
345 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
347 if ( current
->GetData() == object
)
352 return (wxNodeBase
*)NULL
;
355 int wxListBase::IndexOf(void *object
) const
357 wxNodeBase
*node
= Find( object
);
359 return node
? node
->IndexOf() : wxNOT_FOUND
;
362 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
365 if ( m_keyType
== wxKEY_STRING
)
367 free(node
->m_key
.string
);
375 // so that the node knows that it's being deleted by the list
380 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
382 wxCHECK_MSG( node
, NULL
, wxT("detaching NULL wxNodeBase") );
383 wxCHECK_MSG( node
->m_list
== this, NULL
,
384 wxT("detaching node which is not from this list") );
387 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
389 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
392 *prevNext
= node
->GetNext();
393 *nextPrev
= node
->GetPrevious();
397 // mark the node as not belonging to this list any more
403 bool wxListBase::DeleteNode(wxNodeBase
*node
)
405 if ( !DetachNode(node
) )
413 bool wxListBase::DeleteObject(void *object
)
415 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
417 if ( current
->GetData() == object
)
428 void wxListBase::Clear()
430 wxNodeBase
*current
= m_nodeFirst
;
433 wxNodeBase
*next
= current
->GetNext();
434 DoDeleteNode(current
);
439 m_nodeLast
= (wxNodeBase
*)NULL
;
444 void wxListBase::ForEach(wxListIterateFunction F
)
446 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
448 (*F
)(current
->GetData());
452 void *wxListBase::FirstThat(wxListIterateFunction F
)
454 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
456 if ( (*F
)(current
->GetData()) )
457 return current
->GetData();
460 return (wxNodeBase
*)NULL
;
463 void *wxListBase::LastThat(wxListIterateFunction F
)
465 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
467 if ( (*F
)(current
->GetData()) )
468 return current
->GetData();
471 return (wxNodeBase
*)NULL
;
474 // (stefan.hammes@urz.uni-heidelberg.de)
476 // function for sorting lists. the concept is borrowed from 'qsort'.
477 // by giving a sort function, arbitrary lists can be sorted.
479 // - put wxObject pointers into an array
480 // - sort the array with qsort
481 // - put back the sorted wxObject pointers into the list
483 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
484 // so dereference right!
486 // int listcompare(const void *arg1, const void *arg2)
488 // return(compare(**(wxString **)arg1,
489 // **(wxString **)arg2));
496 // list.Append(new wxString("DEF"));
497 // list.Append(new wxString("GHI"));
498 // list.Append(new wxString("ABC"));
499 // list.Sort(listcompare);
502 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
504 // allocate an array for the wxObject pointers of the list
505 const size_t num
= GetCount();
506 void **objArray
= new void *[num
];
507 void **objPtr
= objArray
;
509 // go through the list and put the pointers into the array
511 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
513 *objPtr
++ = node
->GetData();
517 qsort((void *)objArray
,num
,sizeof(wxObject
*),
519 (int (__cdecl
*)(const void *,const void *))
523 // put the sorted pointers back into the list
525 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
527 node
->SetData(*objPtr
++);
534 void wxListBase::Reverse()
536 wxNodeBase
* node
= m_nodeFirst
;
541 // swap prev and next pointers
543 node
->m_next
= node
->m_previous
;
544 node
->m_previous
= tmp
;
546 // this is the node that was next before swapping
550 // swap first and last node
551 tmp
= m_nodeFirst
; m_nodeFirst
= m_nodeLast
; m_nodeLast
= tmp
;
554 void wxListBase::DeleteNodes(wxNodeBase
* first
, wxNodeBase
* last
)
556 wxNodeBase
* node
= first
;
560 wxNodeBase
* next
= node
->GetNext();
566 // ============================================================================
567 // compatibility section from now on
568 // ============================================================================
570 #ifdef wxLIST_COMPATIBILITY
572 // -----------------------------------------------------------------------------
573 // wxList (a.k.a. wxObjectList)
574 // -----------------------------------------------------------------------------
576 IMPLEMENT_DYNAMIC_CLASS(wxList
, wxObject
)
578 wxList::wxList( int key_type
)
579 : wxObjectList( (wxKeyType
)key_type
)
583 void wxObjectListNode::DeleteData()
585 delete (wxObject
*)GetData();
588 // ----------------------------------------------------------------------------
590 // ----------------------------------------------------------------------------
592 static inline wxChar
* MYcopystring(const wxChar
* s
)
594 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
595 return wxStrcpy(copy
, s
);
598 IMPLEMENT_DYNAMIC_CLASS(wxStringList
, wxObject
)
600 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
602 void wxStringListNode::DeleteData()
604 delete [] (char *)GetData();
607 bool wxStringList::Delete(const wxChar
*s
)
609 wxStringListNode
*current
;
611 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
613 if ( wxStrcmp(current
->GetData(), s
) == 0 )
624 void wxStringList::DoCopy(const wxStringList
& other
)
626 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
628 size_t count
= other
.GetCount();
629 for ( size_t n
= 0; n
< count
; n
++ )
631 Add(other
.Item(n
)->GetData());
635 wxStringList::wxStringList()
637 DeleteContents(true);
640 // Variable argument list, terminated by a zero
641 // Makes new storage for the strings
642 wxStringList::wxStringList (const wxChar
*first
, ...)
644 DeleteContents(true);
651 const wxChar
*s
= first
;
656 // icc gives this warning in its own va_arg() macro, argh
658 #pragma warning(push)
659 #pragma warning(disable: 1684)
662 s
= va_arg(ap
, const wxChar
*);
675 // Only makes new strings if arg is true
676 wxChar
**wxStringList::ListToArray(bool new_copies
) const
678 wxChar
**string_array
= new wxChar
*[GetCount()];
679 wxStringListNode
*node
= GetFirst();
680 for (size_t i
= 0; i
< GetCount(); i
++)
682 wxChar
*s
= node
->GetData();
684 string_array
[i
] = MYcopystring(s
);
687 node
= node
->GetNext();
693 // Checks whether s is a member of the list
694 bool wxStringList::Member(const wxChar
*s
) const
696 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
698 const wxChar
*s1
= node
->GetData();
699 if (s
== s1
|| wxStrcmp (s
, s1
) == 0)
707 extern "C" int __cdecl
709 extern "C" int LINKAGEMODE
712 wx_comparestrings(const void *arg1
, const void *arg2
)
714 wxChar
**s1
= (wxChar
**) arg1
;
715 wxChar
**s2
= (wxChar
**) arg2
;
717 return wxStrcmp (*s1
, *s2
);
720 // Sort a list of strings - deallocates old nodes, allocates new
721 void wxStringList::Sort()
723 size_t N
= GetCount();
724 wxChar
**array
= new wxChar
*[N
];
725 wxStringListNode
*node
;
728 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
730 array
[i
++] = node
->GetData();
733 qsort (array
, N
, sizeof (wxChar
*), wx_comparestrings
);
736 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
737 node
->SetData( array
[i
++] );
742 wxNode
*wxStringList::Add(const wxChar
*s
)
744 return (wxNode
*)(wxStringListBase::Node
*)
745 wxStringListBase::Append(MYcopystring(s
));
748 wxNode
*wxStringList::Prepend(const wxChar
*s
)
750 return (wxNode
*)(wxStringListBase::Node
*)
751 wxStringListBase::Insert(MYcopystring(s
));
754 #endif // wxLIST_COMPATIBILITY
756 #else // wxUSE_STL = 1
758 #include "wx/listimpl.cpp"
759 WX_DEFINE_LIST(wxObjectList
)
761 // with wxUSE_STL wxStringList contains wxString objects, not pointers
762 void _WX_LIST_HELPER_wxStringListBase::DeleteFunction( wxString
WXUNUSED(X
) )
766 wxStringListBase::BaseListType
wxStringListBase::EmptyList
;