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
9 // Licence: wxWindows licence
10 ////////////////////////////////////////////////////////////////////////////////
12 // =============================================================================
14 // =============================================================================
16 // -----------------------------------------------------------------------------
18 // -----------------------------------------------------------------------------
21 #pragma implementation "list.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
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_destroy
= list
.m_destroy
;
169 m_keyType
= list
.m_keyType
;
171 m_nodeLast
= (wxNodeBase
*) NULL
;
178 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
180 key
= node
->GetKeyInteger();
181 Append(key
, node
->GetData());
189 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
191 key
= node
->GetKeyString();
192 Append(key
, node
->GetData());
199 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
201 Append(node
->GetData());
207 wxASSERT_MSG( m_count
== list
.m_count
, _T("logic error in wxList::DoCopy") );
210 wxListBase::~wxListBase()
212 wxNodeBase
*each
= m_nodeFirst
;
213 while ( each
!= NULL
)
215 wxNodeBase
*next
= each
->GetNext();
221 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
226 m_nodeLast
= m_nodeFirst
;
230 m_nodeLast
->m_next
= node
;
239 wxNodeBase
*wxListBase::Append(void *object
)
241 // all objects in a keyed list should have a key
242 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
243 wxT("need a key for the object to append") );
245 // we use wxDefaultListKey even though it is the default parameter value
246 // because gcc under Mac OS X seems to miscompile this call otherwise
247 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
,
250 return AppendCommon(node
);
253 wxNodeBase
*wxListBase::Append(long key
, void *object
)
255 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
256 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
258 wxT("can't append object with numeric key to this list") );
260 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
261 return AppendCommon(node
);
264 wxNodeBase
*wxListBase::Append (const wxChar
*key
, void *object
)
266 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
267 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
269 wxT("can't append object with string key to this list") );
271 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
272 return AppendCommon(node
);
275 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
277 // all objects in a keyed list should have a key
278 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
279 wxT("need a key for the object to insert") );
281 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
282 wxT("can't insert before a node from another list") );
284 // previous and next node for the node being inserted
285 wxNodeBase
*prev
, *next
;
288 prev
= position
->GetPrevious();
293 // inserting in the beginning of the list
294 prev
= (wxNodeBase
*)NULL
;
298 // wxDefaultListKey: see comment in Append() above
299 wxNodeBase
*node
= CreateNode(prev
, next
, object
, wxDefaultListKey
);
315 wxNodeBase
*wxListBase::Item(size_t n
) const
317 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
325 wxFAIL_MSG( wxT("invalid index in wxListBase::Item") );
327 return (wxNodeBase
*)NULL
;
330 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
332 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
333 wxT("this list is not keyed on the type of this key") );
335 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
337 if ( key
== current
->m_key
)
344 return (wxNodeBase
*)NULL
;
347 wxNodeBase
*wxListBase::Find(void *object
) const
349 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
351 if ( current
->GetData() == object
)
356 return (wxNodeBase
*)NULL
;
359 int wxListBase::IndexOf(void *object
) const
361 wxNodeBase
*node
= Find( object
);
363 return node
? node
->IndexOf() : wxNOT_FOUND
;
366 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
369 if ( m_keyType
== wxKEY_STRING
)
371 free(node
->m_key
.string
);
379 // so that the node knows that it's being deleted by the list
384 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
386 wxCHECK_MSG( node
, NULL
, wxT("detaching NULL wxNodeBase") );
387 wxCHECK_MSG( node
->m_list
== this, NULL
,
388 wxT("detaching node which is not from this list") );
391 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
393 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
396 *prevNext
= node
->GetNext();
397 *nextPrev
= node
->GetPrevious();
401 // mark the node as not belonging to this list any more
407 bool wxListBase::DeleteNode(wxNodeBase
*node
)
409 if ( !DetachNode(node
) )
417 bool wxListBase::DeleteObject(void *object
)
419 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
421 if ( current
->GetData() == object
)
432 void wxListBase::Clear()
434 wxNodeBase
*current
= m_nodeFirst
;
437 wxNodeBase
*next
= current
->GetNext();
438 DoDeleteNode(current
);
443 m_nodeLast
= (wxNodeBase
*)NULL
;
448 void wxListBase::ForEach(wxListIterateFunction F
)
450 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
452 (*F
)(current
->GetData());
456 void *wxListBase::FirstThat(wxListIterateFunction F
)
458 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
460 if ( (*F
)(current
->GetData()) )
461 return current
->GetData();
464 return (wxNodeBase
*)NULL
;
467 void *wxListBase::LastThat(wxListIterateFunction F
)
469 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
471 if ( (*F
)(current
->GetData()) )
472 return current
->GetData();
475 return (wxNodeBase
*)NULL
;
478 // (stefan.hammes@urz.uni-heidelberg.de)
480 // function for sorting lists. the concept is borrowed from 'qsort'.
481 // by giving a sort function, arbitrary lists can be sorted.
483 // - put wxObject pointers into an array
484 // - sort the array with qsort
485 // - put back the sorted wxObject pointers into the list
487 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
488 // so dereference right!
490 // int listcompare(const void *arg1, const void *arg2)
492 // return(compare(**(wxString **)arg1,
493 // **(wxString **)arg2));
500 // list.Append(new wxString("DEF"));
501 // list.Append(new wxString("GHI"));
502 // list.Append(new wxString("ABC"));
503 // list.Sort(listcompare);
506 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
508 // allocate an array for the wxObject pointers of the list
509 const size_t num
= GetCount();
510 void **objArray
= new void *[num
];
511 void **objPtr
= objArray
;
513 // go through the list and put the pointers into the array
515 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
517 *objPtr
++ = node
->GetData();
521 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
523 // put the sorted pointers back into the list
525 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
527 node
->SetData(*objPtr
++);
534 // ============================================================================
535 // compatibility section from now on
536 // ============================================================================
538 #ifdef wxLIST_COMPATIBILITY
540 // -----------------------------------------------------------------------------
541 // wxNodeBase deprecated methods
542 // -----------------------------------------------------------------------------
544 wxNode
*wxNodeBase::Next() const { return (wxNode
*)GetNext(); }
545 wxNode
*wxNodeBase::Previous() const { return (wxNode
*)GetPrevious(); }
546 wxObject
*wxNodeBase::Data() const { return (wxObject
*)GetData(); }
548 // -----------------------------------------------------------------------------
549 // wxListBase deprecated methods
550 // -----------------------------------------------------------------------------
552 int wxListBase::Number() const { return GetCount(); }
553 wxNode
*wxListBase::First() const { return (wxNode
*)GetFirst(); }
554 wxNode
*wxListBase::Last() const { return (wxNode
*)GetLast(); }
555 wxNode
*wxListBase::Nth(size_t n
) const { return (wxNode
*)Item(n
); }
556 wxListBase::operator wxList
&() const { return *(wxList
*)this; }
558 // -----------------------------------------------------------------------------
559 // wxList (a.k.a. wxObjectList)
560 // -----------------------------------------------------------------------------
562 IMPLEMENT_DYNAMIC_CLASS(wxList
, wxObject
)
564 wxList::wxList( int key_type
)
565 : wxObjectList( (wxKeyType
)key_type
)
569 void wxObjectListNode::DeleteData()
571 delete (wxObject
*)GetData();
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 static inline wxChar
* MYcopystring(const wxString
& s
)
580 wxChar
* copy
= new wxChar
[s
.length() + 1];
581 return wxStrcpy(copy
, s
.c_str());
584 static inline wxChar
* MYcopystring(const wxChar
* s
)
586 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
587 return wxStrcpy(copy
, s
);
590 IMPLEMENT_DYNAMIC_CLASS(wxStringList
, wxObject
)
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 s
= va_arg(ap
, const wxChar
*);
651 if ((int)(long) s
== 0)
661 // Only makes new strings if arg is TRUE
662 wxChar
**wxStringList::ListToArray(bool new_copies
) const
664 wxChar
**string_array
= new wxChar
*[GetCount()];
665 wxStringListNode
*node
= GetFirst();
666 for (size_t i
= 0; i
< GetCount(); i
++)
668 wxChar
*s
= node
->GetData();
670 string_array
[i
] = MYcopystring(s
);
673 node
= node
->GetNext();
679 // Checks whether s is a member of the list
680 bool wxStringList::Member(const wxChar
*s
) const
682 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
684 const wxChar
*s1
= node
->GetData();
685 if (s
== s1
|| wxStrcmp (s
, s1
) == 0)
692 extern "C" int LINKAGEMODE
693 wx_comparestrings(const void *arg1
, const void *arg2
)
695 wxChar
**s1
= (wxChar
**) arg1
;
696 wxChar
**s2
= (wxChar
**) arg2
;
698 return wxStrcmp (*s1
, *s2
);
701 // Sort a list of strings - deallocates old nodes, allocates new
702 void wxStringList::Sort()
704 size_t N
= GetCount();
705 wxChar
**array
= new wxChar
*[N
];
706 wxStringListNode
*node
;
709 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
711 array
[i
++] = node
->GetData();
714 qsort (array
, N
, sizeof (wxChar
*), wx_comparestrings
);
717 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
718 node
->SetData( array
[i
++] );
723 wxNode
*wxStringList::Add(const wxChar
*s
)
725 return (wxNode
*)wxStringListBase::Append(MYcopystring(s
));
728 wxNode
*wxStringList::Prepend(const wxChar
*s
)
730 return (wxNode
*)wxStringListBase::Insert(MYcopystring(s
));
733 #endif // wxLIST_COMPATIBILITY