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 // Sun CC compatibility (interference with xview/pkg.h, apparently...)
41 #if defined(SUN_CC) && defined(__XVIEW__)
48 // =============================================================================
50 // =============================================================================
52 // -----------------------------------------------------------------------------
54 // -----------------------------------------------------------------------------
56 bool wxListKey::operator==(wxListKeyValue value
) const
61 wxFAIL_MSG("bad key type.");
62 // let compiler optimize the line above away in release build
63 // by not putting return here...
66 return strcmp(m_key
.string
, value
.string
) == 0;
69 return m_key
.integer
== value
.integer
;
73 // -----------------------------------------------------------------------------
75 // -----------------------------------------------------------------------------
77 wxNodeBase::wxNodeBase(wxListBase
*list
,
78 wxNodeBase
*previous
, wxNodeBase
*next
,
79 void *data
, const wxListKey
& key
)
83 m_previous
= previous
;
86 switch ( key
.GetKeyType() )
92 m_key
.integer
= key
.GetNumber();
96 // to be free()d later
97 m_key
.string
= strdup(key
.GetString());
101 wxFAIL_MSG("invalid key type");
105 previous
->m_next
= this;
108 next
->m_previous
= this;
111 wxNodeBase::~wxNodeBase()
113 // handle the case when we're being deleted from the list by the user (i.e.
114 // not by the list itself from DeleteNode) - we must do it for
115 // compatibility with old code
116 if ( m_list
!= NULL
)
118 m_list
->DetachNode(this);
122 int wxNodeBase::IndexOf() const
124 wxCHECK_MSG( m_list
, NOT_FOUND
, "node doesn't belong to a list in IndexOf");
126 // It would be more efficient to implement IndexOf() completely inside
127 // wxListBase (only traverse the list once), but this is probably a more
128 // reusable way of doing it. Can always be optimized at a later date (since
129 // IndexOf() resides in wxListBase as well) if efficiency is a problem.
131 wxNodeBase
*prev
= m_previous
;
133 for( i
= 0; prev
; i
++ )
135 prev
= prev
->m_previous
;
141 // -----------------------------------------------------------------------------
143 // -----------------------------------------------------------------------------
145 void wxListBase::Init(wxKeyType keyType
= wxKEY_NONE
)
148 m_nodeLast
= (wxNodeBase
*) NULL
;
154 wxListBase::wxListBase(size_t count
, void *elements
[])
158 for ( size_t n
= 0; n
< count
; n
++ )
164 void wxListBase::DoCopy(const wxListBase
& list
)
166 wxASSERT_MSG( !list
.m_destroy
,
167 "copying list which owns it's elements is a bad idea" );
169 m_count
= list
.m_count
;
170 m_destroy
= list
.m_destroy
;
171 m_keyType
= list
.m_keyType
;
173 m_nodeLast
= (wxNodeBase
*) NULL
;
175 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
181 wxListBase::~wxListBase()
183 wxNodeBase
*each
= m_nodeFirst
;
184 while ( each
!= NULL
)
186 wxNodeBase
*next
= each
->GetNext();
192 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
197 m_nodeLast
= m_nodeFirst
;
201 m_nodeLast
->m_next
= node
;
210 wxNodeBase
*wxListBase::Append(void *object
)
212 // all objects in a keyed list should have a key
213 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
214 "need a key for the object to append" );
216 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
218 return AppendCommon(node
);
221 wxNodeBase
*wxListBase::Append(long key
, void *object
)
223 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
224 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
226 "can't append object with numeric key to this list" );
228 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
229 return AppendCommon(node
);
232 wxNodeBase
*wxListBase::Append (const char *key
, void *object
)
234 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
235 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
237 "can't append object with string key to this list" );
239 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
240 return AppendCommon(node
);
243 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
245 // all objects in a keyed list should have a key
246 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
247 "need a key for the object to insert" );
249 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
250 "can't insert before a node from another list" );
252 // previous and next node for the node being inserted
253 wxNodeBase
*prev
, *next
;
256 prev
= position
->GetPrevious();
261 // inserting in the beginning of the list
262 prev
= (wxNodeBase
*)NULL
;
266 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
282 wxNodeBase
*wxListBase::Item(size_t n
) const
284 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
292 wxFAIL_MSG( "invalid index in wxListBase::Item" );
297 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
299 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
300 "this list is not keyed on the type of this key" );
302 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
304 if ( key
== current
->m_key
)
311 return (wxNodeBase
*)NULL
;
314 wxNodeBase
*wxListBase::Find(void *object
) const
316 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
318 if ( current
->GetData() == object
)
323 return (wxNodeBase
*)NULL
;
326 int wxListBase::IndexOf(void *object
) const
328 wxNodeBase
*node
= Find( object
);
330 return node
? node
->IndexOf() : NOT_FOUND
;
333 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
336 if ( m_keyType
== wxKEY_STRING
)
338 free(node
->m_key
.string
);
349 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
351 wxCHECK_MSG( node
, NULL
, "detaching NULL wxNodeBase" );
352 wxCHECK_MSG( node
->m_list
== this, NULL
,
353 "detaching node which is not from this list" );
356 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
358 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
361 *prevNext
= node
->GetNext();
362 *nextPrev
= node
->GetPrevious();
366 // mark the node as not belonging to this list any more
372 bool wxListBase::DeleteNode(wxNodeBase
*node
)
374 if ( !DetachNode(node
) )
382 bool wxListBase::DeleteObject(void *object
)
384 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
386 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 void wxStringList::DoCopy(const wxStringList
& other
)
522 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
524 size_t count
= other
.GetCount();
525 for ( size_t n
= 0; n
< count
; n
++ )
527 Add(other
.Item(n
)->GetData());
531 // Variable argument list, terminated by a zero
532 // Makes new storage for the strings
533 wxStringList::wxStringList (const char *first
, ...)
541 const char *s
= first
;
546 s
= va_arg(ap
, const char *);
559 // Only makes new strings if arg is TRUE
560 char **wxStringList::ListToArray(bool new_copies
) const
562 char **string_array
= new char *[GetCount()];
563 wxStringListNode
*node
= GetFirst();
564 for (size_t i
= 0; i
< GetCount(); i
++)
566 char *s
= node
->GetData();
568 string_array
[i
] = copystring(s
);
571 node
= node
->GetNext();
577 // Checks whether s is a member of the list
578 bool wxStringList::Member(const char *s
) const
580 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
582 const char *s1
= node
->GetData();
583 if (s
== s1
|| strcmp (s
, s1
) == 0)
591 wx_comparestrings(const void *arg1
, const void *arg2
)
593 char **s1
= (char **) arg1
;
594 char **s2
= (char **) arg2
;
596 return strcmp (*s1
, *s2
);
599 // Sort a list of strings - deallocates old nodes, allocates new
600 void wxStringList::Sort()
602 size_t N
= GetCount();
603 char **array
= new char *[N
];
606 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
608 array
[i
++] = node
->GetData();
611 qsort (array
, N
, sizeof (char *), wx_comparestrings
);
614 for (i
= 0; i
< N
; i
++)