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 wxNodeBase::wxNodeBase(wxListBase
*list
,
57 wxNodeBase
*previous
, wxNodeBase
*next
,
58 void *data
, const wxListKey
& key
)
62 m_previous
= previous
;
65 switch ( key
.GetKeyType() )
71 m_key
.integer
= key
.GetNumber();
75 // to be free()d later
76 m_key
.string
= strdup(key
.GetString());
80 wxFAIL_MSG("invalid key type");
84 previous
->m_next
= this;
87 next
->m_previous
= this;
90 wxNodeBase::~wxNodeBase()
92 // handle the case when we're being deleted from the list by the user (i.e.
93 // not by the list itself from DeleteNode) - we must do it for
94 // compatibility with old code
97 m_list
->DetachNode(this);
101 // -----------------------------------------------------------------------------
103 // -----------------------------------------------------------------------------
105 void wxListBase::Init(wxKeyType keyType
= wxKEY_NONE
)
108 m_nodeLast
= (wxNodeBase
*) NULL
;
114 wxListBase::wxListBase(size_t count
, void *elements
[])
118 for ( size_t n
= 0; n
< count
; n
++ )
124 void wxListBase::DoCopy(const wxListBase
& list
)
126 wxASSERT_MSG( !list
.m_destroy
,
127 "copying list which owns it's elements is a bad idea" );
129 m_count
= list
.m_count
;
130 m_destroy
= list
.m_destroy
;
131 m_keyType
= list
.m_keyType
;
133 m_nodeLast
= (wxNodeBase
*) NULL
;
135 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
141 wxListBase::~wxListBase()
143 wxNodeBase
*each
= m_nodeFirst
;
144 while ( each
!= NULL
)
146 wxNodeBase
*next
= each
->GetNext();
152 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
157 m_nodeLast
= m_nodeFirst
;
161 m_nodeLast
->m_next
= node
;
170 wxNodeBase
*wxListBase::Append(void *object
)
172 // all objects in a keyed list should have a key
173 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
174 "need a key for the object to append" );
176 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
178 return AppendCommon(node
);
181 wxNodeBase
*wxListBase::Append(long key
, void *object
)
183 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
184 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
186 "can't append object with numeric key to this list" );
188 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
189 return AppendCommon(node
);
192 wxNodeBase
*wxListBase::Append (const char *key
, void *object
)
194 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
195 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
197 "can't append object with string key to this list" );
199 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
200 return AppendCommon(node
);
203 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
205 // all objects in a keyed list should have a key
206 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
207 "need a key for the object to insert" );
209 wxNodeBase
*prev
= (wxNodeBase
*)NULL
;
211 prev
= position
->GetPrevious();
213 // inserting in the beginning of the list
215 wxNodeBase
*node
= CreateNode(prev
, position
, object
);
232 wxNodeBase
*wxListBase::Item(size_t n
) const
234 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
242 wxFAIL_MSG( "invalid index in wxListBase::Item" );
247 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
249 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
250 "this list is not keyed on the type of this key" );
252 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
254 if ( key
== current
->m_key
)
261 return (wxNodeBase
*)NULL
;
264 wxNodeBase
*wxListBase::Find(void *object
) const
266 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
268 if ( current
->GetData() == object
)
273 return (wxNodeBase
*)NULL
;
276 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
279 if ( m_keyType
== wxKEY_STRING
)
281 free(node
->m_key
.string
);
292 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
294 wxCHECK_MSG( node
, NULL
, "detaching NULL wxNodeBase" );
295 wxCHECK_MSG( node
->m_list
== this, NULL
,
296 "detaching node which is not from this list" );
299 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
301 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
304 *prevNext
= node
->GetNext();
305 *nextPrev
= node
->GetPrevious();
309 // mark the node as not belonging to this list any more
315 bool wxListBase::DeleteNode(wxNodeBase
*node
)
317 if ( !DetachNode(node
) )
325 bool wxListBase::DeleteObject(void *object
)
327 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
329 if ( current
->GetData() == object
)
341 void wxListBase::Clear()
343 wxNodeBase
*current
= m_nodeFirst
;
346 wxNodeBase
*next
= current
->GetNext();
347 DoDeleteNode(current
);
352 m_nodeLast
= (wxNodeBase
*)NULL
;
357 void wxListBase::ForEach(wxListIterateFunction F
)
359 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
361 (*F
)(current
->GetData());
365 void *wxListBase::FirstThat(wxListIterateFunction F
)
367 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
369 if ( (*F
)(current
->GetData()) )
370 return current
->GetData();
373 return (wxNodeBase
*)NULL
;
376 void *wxListBase::LastThat(wxListIterateFunction F
)
378 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
380 if ( (*F
)(current
->GetData()) )
381 return current
->GetData();
384 return (wxNodeBase
*)NULL
;
387 // (stefan.hammes@urz.uni-heidelberg.de)
389 // function for sorting lists. the concept is borrowed from 'qsort'.
390 // by giving a sort function, arbitrary lists can be sorted.
392 // - put wxObject pointers into an array
393 // - sort the array with qsort
394 // - put back the sorted wxObject pointers into the list
396 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
397 // so dereference right!
399 // int listcompare(const void *arg1, const void *arg2)
401 // return(compare(**(wxString **)arg1,
402 // **(wxString **)arg2));
409 // list.Append(new wxString("DEF"));
410 // list.Append(new wxString("GHI"));
411 // list.Append(new wxString("ABC"));
412 // list.Sort(listcompare);
415 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
417 // allocate an array for the wxObject pointers of the list
418 const size_t num
= GetCount();
419 void **objArray
= new void *[num
];
420 void **objPtr
= objArray
;
422 // go through the list and put the pointers into the array
424 for ( node
= GetFirst(); node
; node
= node
->Next() )
426 *objPtr
++ = node
->Data();
430 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
432 // put the sorted pointers back into the list
434 for ( node
= GetFirst(); node
; node
= node
->Next() )
436 node
->SetData(*objPtr
++);
443 bool wxListKey::operator==(wxListKeyValue value
) const
448 wxFAIL_MSG("bad key type.");
449 // let compiler optimize the line above away in release build
450 // by not putting return here...
453 return strcmp(m_key
.string
, value
.string
) == 0;
456 return m_key
.integer
== value
.integer
;
460 // -----------------------------------------------------------------------------
461 // wxList (a.k.a. wxObjectList)
462 // -----------------------------------------------------------------------------
464 void wxObjectListNode::DeleteData()
466 delete (wxObject
*)GetData();
469 // -----------------------------------------------------------------------------
471 // -----------------------------------------------------------------------------
473 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
475 void wxStringListNode::DeleteData()
477 delete [] (char *)GetData();
480 // Variable argument list, terminated by a zero
481 // Makes new storage for the strings
482 wxStringList::wxStringList (const char *first
, ...)
490 const char *s
= first
;
495 s
= va_arg(ap
, const char *);
508 // Only makes new strings if arg is TRUE
509 char **wxStringList::ListToArray(bool new_copies
) const
511 char **string_array
= new char *[GetCount()];
512 wxStringListNode
*node
= GetFirst();
513 for (size_t i
= 0; i
< GetCount(); i
++)
515 char *s
= node
->GetData();
517 string_array
[i
] = copystring(s
);
520 node
= node
->GetNext();
526 // Checks whether s is a member of the list
527 bool wxStringList::Member(const char *s
) const
529 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
531 const char *s1
= node
->GetData();
532 if (s
== s1
|| strcmp (s
, s1
) == 0)
540 wx_comparestrings(const void *arg1
, const void *arg2
)
542 char **s1
= (char **) arg1
;
543 char **s2
= (char **) arg2
;
545 return strcmp (*s1
, *s2
);
548 // Sort a list of strings - deallocates old nodes, allocates new
549 void wxStringList::Sort()
551 size_t N
= GetCount();
552 char **array
= new char *[N
];
555 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
557 array
[i
++] = node
->GetData();
560 qsort (array
, N
, sizeof (char *), wx_comparestrings
);
563 for (i
= 0; i
< N
; i
++)