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 // But XView is no longer supported.
43 #if defined (SUN_CC) || defined(__SUNCC__) && defined(__XVIEW__)
51 // =============================================================================
53 // =============================================================================
55 // -----------------------------------------------------------------------------
57 // -----------------------------------------------------------------------------
59 wxListKey wxDefaultListKey
;
61 bool wxListKey::operator==(wxListKeyValue value
) const
66 wxFAIL_MSG("bad key type.");
67 // let compiler optimize the line above away in release build
68 // by not putting return here...
71 return strcmp(m_key
.string
, value
.string
) == 0;
74 return m_key
.integer
== value
.integer
;
78 // -----------------------------------------------------------------------------
80 // -----------------------------------------------------------------------------
82 wxNodeBase::wxNodeBase(wxListBase
*list
,
83 wxNodeBase
*previous
, wxNodeBase
*next
,
84 void *data
, const wxListKey
& key
)
88 m_previous
= previous
;
91 switch ( key
.GetKeyType() )
97 m_key
.integer
= key
.GetNumber();
101 // to be free()d later
102 m_key
.string
= strdup(key
.GetString());
106 wxFAIL_MSG("invalid key type");
110 previous
->m_next
= this;
113 next
->m_previous
= this;
116 wxNodeBase::~wxNodeBase()
118 // handle the case when we're being deleted from the list by the user (i.e.
119 // not by the list itself from DeleteNode) - we must do it for
120 // compatibility with old code
121 if ( m_list
!= NULL
)
123 m_list
->DetachNode(this);
127 int wxNodeBase::IndexOf() const
129 wxCHECK_MSG( m_list
, NOT_FOUND
, "node doesn't belong to a list in IndexOf");
131 // It would be more efficient to implement IndexOf() completely inside
132 // wxListBase (only traverse the list once), but this is probably a more
133 // reusable way of doing it. Can always be optimized at a later date (since
134 // IndexOf() resides in wxListBase as well) if efficiency is a problem.
136 wxNodeBase
*prev
= m_previous
;
138 for( i
= 0; prev
; i
++ )
140 prev
= prev
->m_previous
;
146 // -----------------------------------------------------------------------------
148 // -----------------------------------------------------------------------------
150 void wxListBase::Init(wxKeyType keyType
)
153 m_nodeLast
= (wxNodeBase
*) NULL
;
159 wxListBase::wxListBase(size_t count
, void *elements
[])
163 for ( size_t n
= 0; n
< count
; n
++ )
169 void wxListBase::DoCopy(const wxListBase
& list
)
171 wxASSERT_MSG( !list
.m_destroy
,
172 "copying list which owns it's elements is a bad idea" );
174 m_count
= list
.m_count
;
175 m_destroy
= list
.m_destroy
;
176 m_keyType
= list
.m_keyType
;
178 m_nodeLast
= (wxNodeBase
*) NULL
;
180 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
186 wxListBase::~wxListBase()
188 wxNodeBase
*each
= m_nodeFirst
;
189 while ( each
!= NULL
)
191 wxNodeBase
*next
= each
->GetNext();
197 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
202 m_nodeLast
= m_nodeFirst
;
206 m_nodeLast
->m_next
= node
;
215 wxNodeBase
*wxListBase::Append(void *object
)
217 // all objects in a keyed list should have a key
218 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
219 "need a key for the object to append" );
221 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
223 return AppendCommon(node
);
226 wxNodeBase
*wxListBase::Append(long key
, void *object
)
228 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
229 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
231 "can't append object with numeric key to this list" );
233 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
234 return AppendCommon(node
);
237 wxNodeBase
*wxListBase::Append (const char *key
, void *object
)
239 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
240 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
242 "can't append object with string key to this list" );
244 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
245 return AppendCommon(node
);
248 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
250 // all objects in a keyed list should have a key
251 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
252 "need a key for the object to insert" );
254 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
255 "can't insert before a node from another list" );
257 // previous and next node for the node being inserted
258 wxNodeBase
*prev
, *next
;
261 prev
= position
->GetPrevious();
266 // inserting in the beginning of the list
267 prev
= (wxNodeBase
*)NULL
;
271 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
287 wxNodeBase
*wxListBase::Item(size_t n
) const
289 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
297 wxFAIL_MSG( "invalid index in wxListBase::Item" );
299 return (wxNodeBase
*)NULL
;
302 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
304 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
305 "this list is not keyed on the type of this key" );
307 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
309 if ( key
== current
->m_key
)
316 return (wxNodeBase
*)NULL
;
319 wxNodeBase
*wxListBase::Find(void *object
) const
321 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
323 if ( current
->GetData() == object
)
328 return (wxNodeBase
*)NULL
;
331 int wxListBase::IndexOf(void *object
) const
333 wxNodeBase
*node
= Find( object
);
335 return node
? node
->IndexOf() : NOT_FOUND
;
338 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
341 if ( m_keyType
== wxKEY_STRING
)
343 free(node
->m_key
.string
);
354 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
356 wxCHECK_MSG( node
, NULL
, "detaching NULL wxNodeBase" );
357 wxCHECK_MSG( node
->m_list
== this, NULL
,
358 "detaching node which is not from this list" );
361 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
363 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
366 *prevNext
= node
->GetNext();
367 *nextPrev
= node
->GetPrevious();
371 // mark the node as not belonging to this list any more
377 bool wxListBase::DeleteNode(wxNodeBase
*node
)
379 if ( !DetachNode(node
) )
387 bool wxListBase::DeleteObject(void *object
)
389 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
391 if ( current
->GetData() == object
)
402 void wxListBase::Clear()
404 wxNodeBase
*current
= m_nodeFirst
;
407 wxNodeBase
*next
= current
->GetNext();
408 DoDeleteNode(current
);
413 m_nodeLast
= (wxNodeBase
*)NULL
;
418 void wxListBase::ForEach(wxListIterateFunction F
)
420 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
422 (*F
)(current
->GetData());
426 void *wxListBase::FirstThat(wxListIterateFunction F
)
428 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
430 if ( (*F
)(current
->GetData()) )
431 return current
->GetData();
434 return (wxNodeBase
*)NULL
;
437 void *wxListBase::LastThat(wxListIterateFunction F
)
439 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
441 if ( (*F
)(current
->GetData()) )
442 return current
->GetData();
445 return (wxNodeBase
*)NULL
;
448 // (stefan.hammes@urz.uni-heidelberg.de)
450 // function for sorting lists. the concept is borrowed from 'qsort'.
451 // by giving a sort function, arbitrary lists can be sorted.
453 // - put wxObject pointers into an array
454 // - sort the array with qsort
455 // - put back the sorted wxObject pointers into the list
457 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
458 // so dereference right!
460 // int listcompare(const void *arg1, const void *arg2)
462 // return(compare(**(wxString **)arg1,
463 // **(wxString **)arg2));
470 // list.Append(new wxString("DEF"));
471 // list.Append(new wxString("GHI"));
472 // list.Append(new wxString("ABC"));
473 // list.Sort(listcompare);
476 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
478 // allocate an array for the wxObject pointers of the list
479 const size_t num
= GetCount();
480 void **objArray
= new void *[num
];
481 void **objPtr
= objArray
;
483 // go through the list and put the pointers into the array
485 for ( node
= GetFirst(); node
; node
= node
->Next() )
487 *objPtr
++ = node
->Data();
491 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
493 // put the sorted pointers back into the list
495 for ( node
= GetFirst(); node
; node
= node
->Next() )
497 node
->SetData(*objPtr
++);
504 // -----------------------------------------------------------------------------
505 // wxList (a.k.a. wxObjectList)
506 // -----------------------------------------------------------------------------
508 void wxObjectListNode::DeleteData()
510 delete (wxObject
*)GetData();
513 // -----------------------------------------------------------------------------
515 // -----------------------------------------------------------------------------
517 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
519 void wxStringListNode::DeleteData()
521 delete [] (char *)GetData();
524 bool wxStringList::Delete(const char *s
)
526 wxStringListNode
*current
;
528 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
530 if ( strcmp(current
->GetData(), s
) == 0 )
541 void wxStringList::DoCopy(const wxStringList
& other
)
543 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
545 size_t count
= other
.GetCount();
546 for ( size_t n
= 0; n
< count
; n
++ )
548 Add(other
.Item(n
)->GetData());
552 // Variable argument list, terminated by a zero
553 // Makes new storage for the strings
554 wxStringList::wxStringList (const char *first
, ...)
562 const char *s
= first
;
567 s
= va_arg(ap
, const char *);
580 // Only makes new strings if arg is TRUE
581 char **wxStringList::ListToArray(bool new_copies
) const
583 char **string_array
= new char *[GetCount()];
584 wxStringListNode
*node
= GetFirst();
585 for (size_t i
= 0; i
< GetCount(); i
++)
587 char *s
= node
->GetData();
589 string_array
[i
] = copystring(s
);
592 node
= node
->GetNext();
598 // Checks whether s is a member of the list
599 bool wxStringList::Member(const char *s
) const
601 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
603 const char *s1
= node
->GetData();
604 if (s
== s1
|| strcmp (s
, s1
) == 0)
612 wx_comparestrings(const void *arg1
, const void *arg2
)
614 char **s1
= (char **) arg1
;
615 char **s2
= (char **) arg2
;
617 return strcmp (*s1
, *s2
);
620 // Sort a list of strings - deallocates old nodes, allocates new
621 void wxStringList::Sort()
623 size_t N
= GetCount();
624 char **array
= new char *[N
];
625 wxStringListNode
*node
;
628 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
630 array
[i
++] = node
->GetData();
633 qsort (array
, N
, sizeof (char *), wx_comparestrings
);
636 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
637 node
->SetData( array
[i
++] );