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 bool wxListKey::operator==(wxListKeyValue value
) const
64 wxFAIL_MSG("bad key type.");
65 // let compiler optimize the line above away in release build
66 // by not putting return here...
69 return strcmp(m_key
.string
, value
.string
) == 0;
72 return m_key
.integer
== value
.integer
;
76 // -----------------------------------------------------------------------------
78 // -----------------------------------------------------------------------------
80 wxNodeBase::wxNodeBase(wxListBase
*list
,
81 wxNodeBase
*previous
, wxNodeBase
*next
,
82 void *data
, const wxListKey
& key
)
86 m_previous
= previous
;
89 switch ( key
.GetKeyType() )
95 m_key
.integer
= key
.GetNumber();
99 // to be free()d later
100 m_key
.string
= strdup(key
.GetString());
104 wxFAIL_MSG("invalid key type");
108 previous
->m_next
= this;
111 next
->m_previous
= this;
114 wxNodeBase::~wxNodeBase()
116 // handle the case when we're being deleted from the list by the user (i.e.
117 // not by the list itself from DeleteNode) - we must do it for
118 // compatibility with old code
119 if ( m_list
!= NULL
)
121 m_list
->DetachNode(this);
125 int wxNodeBase::IndexOf() const
127 wxCHECK_MSG( m_list
, NOT_FOUND
, "node doesn't belong to a list in IndexOf");
129 // It would be more efficient to implement IndexOf() completely inside
130 // wxListBase (only traverse the list once), but this is probably a more
131 // reusable way of doing it. Can always be optimized at a later date (since
132 // IndexOf() resides in wxListBase as well) if efficiency is a problem.
134 wxNodeBase
*prev
= m_previous
;
136 for( i
= 0; prev
; i
++ )
138 prev
= prev
->m_previous
;
144 // -----------------------------------------------------------------------------
146 // -----------------------------------------------------------------------------
148 void wxListBase::Init(wxKeyType keyType
)
151 m_nodeLast
= (wxNodeBase
*) NULL
;
157 wxListBase::wxListBase(size_t count
, void *elements
[])
161 for ( size_t n
= 0; n
< count
; n
++ )
167 void wxListBase::DoCopy(const wxListBase
& list
)
169 wxASSERT_MSG( !list
.m_destroy
,
170 "copying list which owns it's elements is a bad idea" );
172 m_count
= list
.m_count
;
173 m_destroy
= list
.m_destroy
;
174 m_keyType
= list
.m_keyType
;
176 m_nodeLast
= (wxNodeBase
*) NULL
;
178 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
184 wxListBase::~wxListBase()
186 wxNodeBase
*each
= m_nodeFirst
;
187 while ( each
!= NULL
)
189 wxNodeBase
*next
= each
->GetNext();
195 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
200 m_nodeLast
= m_nodeFirst
;
204 m_nodeLast
->m_next
= node
;
213 wxNodeBase
*wxListBase::Append(void *object
)
215 // all objects in a keyed list should have a key
216 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
217 "need a key for the object to append" );
219 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
221 return AppendCommon(node
);
224 wxNodeBase
*wxListBase::Append(long key
, void *object
)
226 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
227 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
229 "can't append object with numeric key to this list" );
231 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
232 return AppendCommon(node
);
235 wxNodeBase
*wxListBase::Append (const char *key
, void *object
)
237 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
238 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
240 "can't append object with string key to this list" );
242 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
243 return AppendCommon(node
);
246 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
248 // all objects in a keyed list should have a key
249 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
250 "need a key for the object to insert" );
252 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
253 "can't insert before a node from another list" );
255 // previous and next node for the node being inserted
256 wxNodeBase
*prev
, *next
;
259 prev
= position
->GetPrevious();
264 // inserting in the beginning of the list
265 prev
= (wxNodeBase
*)NULL
;
269 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
285 wxNodeBase
*wxListBase::Item(size_t n
) const
287 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
295 wxFAIL_MSG( "invalid index in wxListBase::Item" );
297 return (wxNodeBase
*)NULL
;
300 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
302 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
303 "this list is not keyed on the type of this key" );
305 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
307 if ( key
== current
->m_key
)
314 return (wxNodeBase
*)NULL
;
317 wxNodeBase
*wxListBase::Find(void *object
) const
319 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
321 if ( current
->GetData() == object
)
326 return (wxNodeBase
*)NULL
;
329 int wxListBase::IndexOf(void *object
) const
331 wxNodeBase
*node
= Find( object
);
333 return node
? node
->IndexOf() : NOT_FOUND
;
336 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
339 if ( m_keyType
== wxKEY_STRING
)
341 free(node
->m_key
.string
);
352 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
354 wxCHECK_MSG( node
, NULL
, "detaching NULL wxNodeBase" );
355 wxCHECK_MSG( node
->m_list
== this, NULL
,
356 "detaching node which is not from this list" );
359 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
361 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
364 *prevNext
= node
->GetNext();
365 *nextPrev
= node
->GetPrevious();
369 // mark the node as not belonging to this list any more
375 bool wxListBase::DeleteNode(wxNodeBase
*node
)
377 if ( !DetachNode(node
) )
385 bool wxListBase::DeleteObject(void *object
)
387 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
389 if ( current
->GetData() == object
)
400 void wxListBase::Clear()
402 wxNodeBase
*current
= m_nodeFirst
;
405 wxNodeBase
*next
= current
->GetNext();
406 DoDeleteNode(current
);
411 m_nodeLast
= (wxNodeBase
*)NULL
;
416 void wxListBase::ForEach(wxListIterateFunction F
)
418 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
420 (*F
)(current
->GetData());
424 void *wxListBase::FirstThat(wxListIterateFunction F
)
426 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
428 if ( (*F
)(current
->GetData()) )
429 return current
->GetData();
432 return (wxNodeBase
*)NULL
;
435 void *wxListBase::LastThat(wxListIterateFunction F
)
437 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
439 if ( (*F
)(current
->GetData()) )
440 return current
->GetData();
443 return (wxNodeBase
*)NULL
;
446 // (stefan.hammes@urz.uni-heidelberg.de)
448 // function for sorting lists. the concept is borrowed from 'qsort'.
449 // by giving a sort function, arbitrary lists can be sorted.
451 // - put wxObject pointers into an array
452 // - sort the array with qsort
453 // - put back the sorted wxObject pointers into the list
455 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
456 // so dereference right!
458 // int listcompare(const void *arg1, const void *arg2)
460 // return(compare(**(wxString **)arg1,
461 // **(wxString **)arg2));
468 // list.Append(new wxString("DEF"));
469 // list.Append(new wxString("GHI"));
470 // list.Append(new wxString("ABC"));
471 // list.Sort(listcompare);
474 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
476 // allocate an array for the wxObject pointers of the list
477 const size_t num
= GetCount();
478 void **objArray
= new void *[num
];
479 void **objPtr
= objArray
;
481 // go through the list and put the pointers into the array
483 for ( node
= GetFirst(); node
; node
= node
->Next() )
485 *objPtr
++ = node
->Data();
489 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
491 // put the sorted pointers back into the list
493 for ( node
= GetFirst(); node
; node
= node
->Next() )
495 node
->SetData(*objPtr
++);
502 // -----------------------------------------------------------------------------
503 // wxList (a.k.a. wxObjectList)
504 // -----------------------------------------------------------------------------
506 void wxObjectListNode::DeleteData()
508 delete (wxObject
*)GetData();
511 // -----------------------------------------------------------------------------
513 // -----------------------------------------------------------------------------
515 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
517 void wxStringListNode::DeleteData()
519 delete [] (char *)GetData();
522 bool wxStringList::Delete(const char *s
)
524 wxStringListNode
*current
;
526 for ( current
= GetFirst(); current
; current
= current
->GetNext() )
528 if ( strcmp(current
->GetData(), s
) == 0 )
539 void wxStringList::DoCopy(const wxStringList
& other
)
541 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
543 size_t count
= other
.GetCount();
544 for ( size_t n
= 0; n
< count
; n
++ )
546 Add(other
.Item(n
)->GetData());
550 // Variable argument list, terminated by a zero
551 // Makes new storage for the strings
552 wxStringList::wxStringList (const char *first
, ...)
560 const char *s
= first
;
565 s
= va_arg(ap
, const char *);
578 // Only makes new strings if arg is TRUE
579 char **wxStringList::ListToArray(bool new_copies
) const
581 char **string_array
= new char *[GetCount()];
582 wxStringListNode
*node
= GetFirst();
583 for (size_t i
= 0; i
< GetCount(); i
++)
585 char *s
= node
->GetData();
587 string_array
[i
] = copystring(s
);
590 node
= node
->GetNext();
596 // Checks whether s is a member of the list
597 bool wxStringList::Member(const char *s
) const
599 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
601 const char *s1
= node
->GetData();
602 if (s
== s1
|| strcmp (s
, s1
) == 0)
610 wx_comparestrings(const void *arg1
, const void *arg2
)
612 char **s1
= (char **) arg1
;
613 char **s2
= (char **) arg2
;
615 return strcmp (*s1
, *s2
);
618 // Sort a list of strings - deallocates old nodes, allocates new
619 void wxStringList::Sort()
621 size_t N
= GetCount();
622 char **array
= new char *[N
];
623 wxStringListNode
*node
;
626 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
628 array
[i
++] = node
->GetData();
631 qsort (array
, N
, sizeof (char *), wx_comparestrings
);
634 for ( node
= GetFirst(); node
; node
= node
->GetNext() )
635 node
->SetData( array
[i
++] );