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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 ////////////////////////////////////////////////////////////////////////////////
11 // =============================================================================
13 // =============================================================================
15 // -----------------------------------------------------------------------------
17 // -----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
35 #if !wxUSE_STD_CONTAINERS
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
)
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
;
173 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
175 Append(node
->GetKeyInteger(), node
->GetData());
182 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
184 Append(node
->GetKeyString(), node
->GetData());
191 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
193 Append(node
->GetData());
199 wxASSERT_MSG( m_count
== list
.m_count
, wxT("logic error in wxList::DoCopy") );
202 wxListBase::~wxListBase()
204 wxNodeBase
*each
= m_nodeFirst
;
205 while ( each
!= NULL
)
207 wxNodeBase
*next
= each
->GetNext();
213 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
218 m_nodeLast
= m_nodeFirst
;
222 m_nodeLast
->m_next
= node
;
231 wxNodeBase
*wxListBase::Append(void *object
)
233 // all objects in a keyed list should have a key
234 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, NULL
,
235 wxT("need a key for the object to append") );
237 // we use wxDefaultListKey even though it is the default parameter value
238 // because gcc under Mac OS X seems to miscompile this call otherwise
239 wxNodeBase
*node
= CreateNode(m_nodeLast
, NULL
, object
,
242 return AppendCommon(node
);
245 wxNodeBase
*wxListBase::Append(long key
, void *object
)
247 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
248 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
250 wxT("can't append object with numeric key to this list") );
252 wxNodeBase
*node
= CreateNode(m_nodeLast
, NULL
, object
, key
);
253 return AppendCommon(node
);
256 wxNodeBase
*wxListBase::Append (const wxString
& key
, void *object
)
258 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
259 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
261 wxT("can't append object with string key to this list") );
263 wxNodeBase
*node
= CreateNode(m_nodeLast
, NULL
, object
, key
);
264 return AppendCommon(node
);
267 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
269 // all objects in a keyed list should have a key
270 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, NULL
,
271 wxT("need a key for the object to insert") );
273 wxCHECK_MSG( !position
|| position
->m_list
== this, NULL
,
274 wxT("can't insert before a node from another list") );
276 // previous and next node for the node being inserted
277 wxNodeBase
*prev
, *next
;
280 prev
= position
->GetPrevious();
285 // inserting in the beginning of the list
290 // wxDefaultListKey: see comment in Append() above
291 wxNodeBase
*node
= CreateNode(prev
, next
, object
, wxDefaultListKey
);
307 wxNodeBase
*wxListBase::Item(size_t n
) const
309 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
317 wxFAIL_MSG( wxT("invalid index in wxListBase::Item") );
322 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
324 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
325 wxT("this list is not keyed on the type of this key") );
327 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
329 if ( key
== current
->m_key
)
339 wxNodeBase
*wxListBase::Find(const void *object
) const
341 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
343 if ( current
->GetData() == object
)
351 int wxListBase::IndexOf(void *object
) const
353 wxNodeBase
*node
= Find( object
);
355 return node
? node
->IndexOf() : wxNOT_FOUND
;
358 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
361 if ( m_keyType
== wxKEY_STRING
)
363 free(node
->m_key
.string
);
371 // so that the node knows that it's being deleted by the list
376 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
378 wxCHECK_MSG( node
, NULL
, wxT("detaching NULL wxNodeBase") );
379 wxCHECK_MSG( node
->m_list
== this, NULL
,
380 wxT("detaching node which is not from this list") );
383 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
385 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
388 *prevNext
= node
->GetNext();
389 *nextPrev
= node
->GetPrevious();
393 // mark the node as not belonging to this list any more
399 bool wxListBase::DeleteNode(wxNodeBase
*node
)
401 if ( !DetachNode(node
) )
409 bool wxListBase::DeleteObject(void *object
)
411 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
413 if ( current
->GetData() == object
)
424 void wxListBase::Clear()
426 wxNodeBase
*current
= m_nodeFirst
;
429 wxNodeBase
*next
= current
->GetNext();
430 DoDeleteNode(current
);
440 void wxListBase::ForEach(wxListIterateFunction F
)
442 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
444 (*F
)(current
->GetData());
448 void *wxListBase::FirstThat(wxListIterateFunction F
)
450 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
452 if ( (*F
)(current
->GetData()) )
453 return current
->GetData();
459 void *wxListBase::LastThat(wxListIterateFunction F
)
461 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
463 if ( (*F
)(current
->GetData()) )
464 return current
->GetData();
470 // (stefan.hammes@urz.uni-heidelberg.de)
472 // function for sorting lists. the concept is borrowed from 'qsort'.
473 // by giving a sort function, arbitrary lists can be sorted.
475 // - put wxObject pointers into an array
476 // - sort the array with qsort
477 // - put back the sorted wxObject pointers into the list
479 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
480 // so dereference right!
482 // int listcompare(const void *arg1, const void *arg2)
484 // return(compare(**(wxString **)arg1,
485 // **(wxString **)arg2));
492 // list.Append(new wxString("DEF"));
493 // list.Append(new wxString("GHI"));
494 // list.Append(new wxString("ABC"));
495 // list.Sort(listcompare);
498 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
500 // allocate an array for the wxObject pointers of the list
501 const size_t num
= GetCount();
502 void **objArray
= new void *[num
];
503 void **objPtr
= objArray
;
505 // go through the list and put the pointers into the array
507 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
509 *objPtr
++ = node
->GetData();
513 qsort((void *)objArray
,num
,sizeof(wxObject
*),
515 (int (__cdecl
*)(const void *,const void *))
519 // put the sorted pointers back into the list
521 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
523 node
->SetData(*objPtr
++);
530 void wxListBase::Reverse()
532 wxNodeBase
* node
= m_nodeFirst
;
537 // swap prev and next pointers
539 node
->m_next
= node
->m_previous
;
540 node
->m_previous
= tmp
;
542 // this is the node that was next before swapping
546 // swap first and last node
547 tmp
= m_nodeFirst
; m_nodeFirst
= m_nodeLast
; m_nodeLast
= tmp
;
550 void wxListBase::DeleteNodes(wxNodeBase
* first
, wxNodeBase
* last
)
552 wxNodeBase
* node
= first
;
556 wxNodeBase
* next
= node
->GetNext();
562 // ============================================================================
563 // compatibility section from now on
564 // ============================================================================
566 #ifdef wxLIST_COMPATIBILITY
568 // -----------------------------------------------------------------------------
569 // wxList (a.k.a. wxObjectList)
570 // -----------------------------------------------------------------------------
572 wxList::wxList( int key_type
)
573 : wxObjectList( (wxKeyType
)key_type
)
577 void wxObjectListNode::DeleteData()
579 delete (wxObject
*)GetData();
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 static inline wxChar
* MYcopystring(const wxChar
* s
)
588 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
589 return wxStrcpy(copy
, s
);
592 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
594 void wxStringListNode::DeleteData()
596 delete [] (char *)GetData();
599 bool wxStringList::Delete(const wxChar
*s
)
601 wxStringListNode
*current
;
603 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
605 if ( wxStrcmp(current
->GetData(), s
) == 0 )
616 void wxStringList::DoCopy(const wxStringList
& other
)
618 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
620 size_t count
= other
.GetCount();
621 for ( size_t n
= 0; n
< count
; n
++ )
623 Add(other
.Item(n
)->GetData());
627 wxStringList::wxStringList()
629 DeleteContents(true);
632 // Variable argument list, terminated by a zero
633 // Makes new storage for the strings
634 wxStringList::wxStringList (const wxChar
*first
, ...)
636 DeleteContents(true);
643 const wxChar
*s
= first
;
648 // icc gives this warning in its own va_arg() macro, argh
650 #pragma warning(push)
651 #pragma warning(disable: 1684)
654 s
= va_arg(ap
, const wxChar
*);
667 // Only makes new strings if arg is true
668 wxChar
**wxStringList::ListToArray(bool new_copies
) const
670 wxChar
**string_array
= new wxChar
*[GetCount()];
671 wxStringListNode
*node
= GetFirst();
672 for (size_t i
= 0; i
< GetCount(); i
++)
674 wxChar
*s
= node
->GetData();
676 string_array
[i
] = MYcopystring(s
);
679 node
= node
->GetNext();
685 // Checks whether s is a member of the list
686 bool wxStringList::Member(const wxChar
*s
) const
688 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
690 const wxChar
*s1
= node
->GetData();
691 if (s
== s1
|| wxStrcmp (s
, s1
) == 0)
705 static int LINKAGEMODE
708 wx_comparestrings(const void *arg1
, const void *arg2
)
710 wxChar
**s1
= (wxChar
**) arg1
;
711 wxChar
**s2
= (wxChar
**) arg2
;
713 return wxStrcmp (*s1
, *s2
);
716 } // end of extern "C" (required because of GCC Bug c++/33078
718 // Sort a list of strings - deallocates old nodes, allocates new
719 void wxStringList::Sort()
721 size_t N
= GetCount();
722 wxChar
**array
= new wxChar
*[N
];
723 wxStringListNode
*node
;
726 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
728 array
[i
++] = node
->GetData();
731 qsort (array
, N
, sizeof (wxChar
*), wx_comparestrings
);
734 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
735 node
->SetData( array
[i
++] );
740 wxNode
*wxStringList::Add(const wxChar
*s
)
742 return (wxNode
*)(wxStringListBase::Node
*)
743 wxStringListBase::Append(MYcopystring(s
));
746 wxNode
*wxStringList::Prepend(const wxChar
*s
)
748 return (wxNode
*)(wxStringListBase::Node
*)
749 wxStringListBase::Insert(MYcopystring(s
));
752 #endif // wxLIST_COMPATIBILITY
754 #else // wxUSE_STD_CONTAINERS = 1
756 #include "wx/listimpl.cpp"
757 WX_DEFINE_LIST(wxObjectList
)
759 // with wxUSE_STD_CONTAINERS wxStringList contains wxString objects, not pointers
760 void _WX_LIST_HELPER_wxStringListBase::DeleteFunction( wxString
WXUNUSED(X
) )
764 _WX_LIST_HELPER_wxStringListBase::BaseListType
_WX_LIST_HELPER_wxStringListBase::EmptyList
;
766 #endif // !wxUSE_STD_CONTAINERS