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 // -----------------------------------------------------------------------------
124 // -----------------------------------------------------------------------------
126 void wxListBase::Init(wxKeyType keyType
= wxKEY_NONE
)
129 m_nodeLast
= (wxNodeBase
*) NULL
;
135 wxListBase::wxListBase(size_t count
, void *elements
[])
139 for ( size_t n
= 0; n
< count
; n
++ )
145 void wxListBase::DoCopy(const wxListBase
& list
)
147 wxASSERT_MSG( !list
.m_destroy
,
148 "copying list which owns it's elements is a bad idea" );
150 m_count
= list
.m_count
;
151 m_destroy
= list
.m_destroy
;
152 m_keyType
= list
.m_keyType
;
154 m_nodeLast
= (wxNodeBase
*) NULL
;
156 for ( wxNodeBase
*node
= list
.GetFirst(); node
; node
= node
->GetNext() )
162 wxListBase::~wxListBase()
164 wxNodeBase
*each
= m_nodeFirst
;
165 while ( each
!= NULL
)
167 wxNodeBase
*next
= each
->GetNext();
173 wxNodeBase
*wxListBase::AppendCommon(wxNodeBase
*node
)
178 m_nodeLast
= m_nodeFirst
;
182 m_nodeLast
->m_next
= node
;
191 wxNodeBase
*wxListBase::Append(void *object
)
193 // all objects in a keyed list should have a key
194 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
195 "need a key for the object to append" );
197 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
);
199 return AppendCommon(node
);
202 wxNodeBase
*wxListBase::Append(long key
, void *object
)
204 wxCHECK_MSG( (m_keyType
== wxKEY_INTEGER
) ||
205 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
207 "can't append object with numeric key to this list" );
209 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
210 return AppendCommon(node
);
213 wxNodeBase
*wxListBase::Append (const char *key
, void *object
)
215 wxCHECK_MSG( (m_keyType
== wxKEY_STRING
) ||
216 (m_keyType
== wxKEY_NONE
&& m_count
== 0),
218 "can't append object with string key to this list" );
220 wxNodeBase
*node
= CreateNode(m_nodeLast
, (wxNodeBase
*)NULL
, object
, key
);
221 return AppendCommon(node
);
224 wxNodeBase
*wxListBase::Insert(wxNodeBase
*position
, void *object
)
226 // all objects in a keyed list should have a key
227 wxCHECK_MSG( m_keyType
== wxKEY_NONE
, (wxNodeBase
*)NULL
,
228 "need a key for the object to insert" );
230 wxCHECK_MSG( !position
|| position
->m_list
== this, (wxNodeBase
*)NULL
,
231 "can't insert before a node from another list" );
233 // previous and next node for the node being inserted
234 wxNodeBase
*prev
, *next
;
237 prev
= position
->GetPrevious();
242 // inserting in the beginning of the list
243 prev
= (wxNodeBase
*)NULL
;
247 wxNodeBase
*node
= CreateNode(prev
, next
, object
);
263 wxNodeBase
*wxListBase::Item(size_t n
) const
265 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
273 wxFAIL_MSG( "invalid index in wxListBase::Item" );
278 wxNodeBase
*wxListBase::Find(const wxListKey
& key
) const
280 wxASSERT_MSG( m_keyType
== key
.GetKeyType(),
281 "this list is not keyed on the type of this key" );
283 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
285 if ( key
== current
->m_key
)
292 return (wxNodeBase
*)NULL
;
295 wxNodeBase
*wxListBase::Find(void *object
) const
297 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
299 if ( current
->GetData() == object
)
304 return (wxNodeBase
*)NULL
;
307 void wxListBase::DoDeleteNode(wxNodeBase
*node
)
310 if ( m_keyType
== wxKEY_STRING
)
312 free(node
->m_key
.string
);
323 wxNodeBase
*wxListBase::DetachNode(wxNodeBase
*node
)
325 wxCHECK_MSG( node
, NULL
, "detaching NULL wxNodeBase" );
326 wxCHECK_MSG( node
->m_list
== this, NULL
,
327 "detaching node which is not from this list" );
330 wxNodeBase
**prevNext
= node
->GetPrevious() ? &node
->GetPrevious()->m_next
332 wxNodeBase
**nextPrev
= node
->GetNext() ? &node
->GetNext()->m_previous
335 *prevNext
= node
->GetNext();
336 *nextPrev
= node
->GetPrevious();
340 // mark the node as not belonging to this list any more
346 bool wxListBase::DeleteNode(wxNodeBase
*node
)
348 if ( !DetachNode(node
) )
356 bool wxListBase::DeleteObject(void *object
)
358 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
360 if ( current
->GetData() == object
)
372 void wxListBase::Clear()
374 wxNodeBase
*current
= m_nodeFirst
;
377 wxNodeBase
*next
= current
->GetNext();
378 DoDeleteNode(current
);
383 m_nodeLast
= (wxNodeBase
*)NULL
;
388 void wxListBase::ForEach(wxListIterateFunction F
)
390 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
392 (*F
)(current
->GetData());
396 void *wxListBase::FirstThat(wxListIterateFunction F
)
398 for ( wxNodeBase
*current
= GetFirst(); current
; current
= current
->GetNext() )
400 if ( (*F
)(current
->GetData()) )
401 return current
->GetData();
404 return (wxNodeBase
*)NULL
;
407 void *wxListBase::LastThat(wxListIterateFunction F
)
409 for ( wxNodeBase
*current
= GetLast(); current
; current
= current
->GetPrevious() )
411 if ( (*F
)(current
->GetData()) )
412 return current
->GetData();
415 return (wxNodeBase
*)NULL
;
418 // (stefan.hammes@urz.uni-heidelberg.de)
420 // function for sorting lists. the concept is borrowed from 'qsort'.
421 // by giving a sort function, arbitrary lists can be sorted.
423 // - put wxObject pointers into an array
424 // - sort the array with qsort
425 // - put back the sorted wxObject pointers into the list
427 // CAVE: the sort function receives pointers to wxObject pointers (wxObject **),
428 // so dereference right!
430 // int listcompare(const void *arg1, const void *arg2)
432 // return(compare(**(wxString **)arg1,
433 // **(wxString **)arg2));
440 // list.Append(new wxString("DEF"));
441 // list.Append(new wxString("GHI"));
442 // list.Append(new wxString("ABC"));
443 // list.Sort(listcompare);
446 void wxListBase::Sort(const wxSortCompareFunction compfunc
)
448 // allocate an array for the wxObject pointers of the list
449 const size_t num
= GetCount();
450 void **objArray
= new void *[num
];
451 void **objPtr
= objArray
;
453 // go through the list and put the pointers into the array
455 for ( node
= GetFirst(); node
; node
= node
->Next() )
457 *objPtr
++ = node
->Data();
461 qsort((void *)objArray
,num
,sizeof(wxObject
*),compfunc
);
463 // put the sorted pointers back into the list
465 for ( node
= GetFirst(); node
; node
= node
->Next() )
467 node
->SetData(*objPtr
++);
474 // -----------------------------------------------------------------------------
475 // wxList (a.k.a. wxObjectList)
476 // -----------------------------------------------------------------------------
478 void wxObjectListNode::DeleteData()
480 delete (wxObject
*)GetData();
483 // -----------------------------------------------------------------------------
485 // -----------------------------------------------------------------------------
487 // instead of WX_DEFINE_LIST(wxStringListBase) we define this function
489 void wxStringListNode::DeleteData()
491 delete [] (char *)GetData();
494 void wxStringList::DoCopy(const wxStringList
& other
)
496 wxASSERT( GetCount() == 0 ); // this list must be empty before copying!
498 size_t count
= other
.GetCount();
499 for ( size_t n
= 0; n
< count
; n
++ )
501 Add(other
.Item(n
)->GetData());
505 // Variable argument list, terminated by a zero
506 // Makes new storage for the strings
507 wxStringList::wxStringList (const char *first
, ...)
515 const char *s
= first
;
520 s
= va_arg(ap
, const char *);
533 // Only makes new strings if arg is TRUE
534 char **wxStringList::ListToArray(bool new_copies
) const
536 char **string_array
= new char *[GetCount()];
537 wxStringListNode
*node
= GetFirst();
538 for (size_t i
= 0; i
< GetCount(); i
++)
540 char *s
= node
->GetData();
542 string_array
[i
] = copystring(s
);
545 node
= node
->GetNext();
551 // Checks whether s is a member of the list
552 bool wxStringList::Member(const char *s
) const
554 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
556 const char *s1
= node
->GetData();
557 if (s
== s1
|| strcmp (s
, s1
) == 0)
565 wx_comparestrings(const void *arg1
, const void *arg2
)
567 char **s1
= (char **) arg1
;
568 char **s2
= (char **) arg2
;
570 return strcmp (*s1
, *s2
);
573 // Sort a list of strings - deallocates old nodes, allocates new
574 void wxStringList::Sort()
576 size_t N
= GetCount();
577 char **array
= new char *[N
];
580 for ( wxStringListNode
*node
= GetFirst(); node
; node
= node
->GetNext() )
582 array
[i
++] = node
->GetData();
585 qsort (array
, N
, sizeof (char *), wx_comparestrings
);
588 for (i
= 0; i
< N
; i
++)