X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/6164d85e896743b16a0cf8b0610f5d17ceb47f36..706bb5f973ccb44a924ee1f9aa79c7ee34eb2556:/src/common/list.cpp diff --git a/src/common/list.cpp b/src/common/list.cpp index f7b1ab0543..85448991d2 100644 --- a/src/common/list.cpp +++ b/src/common/list.cpp @@ -37,18 +37,33 @@ #include "wx/utils.h" // for copystring() (beurk...) #endif -// Sun CC compatibility (interference with xview/pkg.h, apparently...) -#if defined(SUN_CC) && defined(__XVIEW__) - #undef va_start - #undef va_end - #undef va_arg - #undef va_list -#endif - // ============================================================================= // implementation // ============================================================================= +// ----------------------------------------------------------------------------- +// wxListKey +// ----------------------------------------------------------------------------- + +wxListKey wxDefaultListKey; + +bool wxListKey::operator==(wxListKeyValue value) const +{ + switch ( m_keyType ) + { + default: + wxFAIL_MSG("bad key type."); + // let compiler optimize the line above away in release build + // by not putting return here... + + case wxKEY_STRING: + return strcmp(m_key.string, value.string) == 0; + + case wxKEY_INTEGER: + return m_key.integer == value.integer; + } +} + // ----------------------------------------------------------------------------- // wxNodeBase // ----------------------------------------------------------------------------- @@ -61,28 +76,28 @@ wxNodeBase::wxNodeBase(wxListBase *list, m_data = data; m_previous = previous; m_next = next; - + switch ( key.GetKeyType() ) { case wxKEY_NONE: break; - + case wxKEY_INTEGER: m_key.integer = key.GetNumber(); break; - + case wxKEY_STRING: // to be free()d later m_key.string = strdup(key.GetString()); break; - + default: wxFAIL_MSG("invalid key type"); } - + if ( previous ) previous->m_next = this; - + if ( next ) next->m_previous = this; } @@ -94,15 +109,39 @@ wxNodeBase::~wxNodeBase() // compatibility with old code if ( m_list != NULL ) { + if ( m_list->m_keyType == wxKEY_STRING ) + { + free(m_key.string); + } + m_list->DetachNode(this); } } +int wxNodeBase::IndexOf() const +{ + wxCHECK_MSG( m_list, wxNOT_FOUND, "node doesn't belong to a list in IndexOf"); + + // It would be more efficient to implement IndexOf() completely inside + // wxListBase (only traverse the list once), but this is probably a more + // reusable way of doing it. Can always be optimized at a later date (since + // IndexOf() resides in wxListBase as well) if efficiency is a problem. + int i; + wxNodeBase *prev = m_previous; + + for( i = 0; prev; i++ ) + { + prev = prev->m_previous; + } + + return i; +} + // ----------------------------------------------------------------------------- // wxListBase // ----------------------------------------------------------------------------- -void wxListBase::Init(wxKeyType keyType = wxKEY_NONE) +void wxListBase::Init(wxKeyType keyType) { m_nodeFirst = m_nodeLast = (wxNodeBase *) NULL; @@ -206,16 +245,26 @@ wxNodeBase *wxListBase::Insert(wxNodeBase *position, void *object) wxCHECK_MSG( m_keyType == wxKEY_NONE, (wxNodeBase *)NULL, "need a key for the object to insert" ); - wxNodeBase *prev = (wxNodeBase *)NULL; + wxCHECK_MSG( !position || position->m_list == this, (wxNodeBase *)NULL, + "can't insert before a node from another list" ); + + // previous and next node for the node being inserted + wxNodeBase *prev, *next; if ( position ) + { prev = position->GetPrevious(); - //else - // inserting in the beginning of the list + next = position; + } + else + { + // inserting in the beginning of the list + prev = (wxNodeBase *)NULL; + next = m_nodeFirst; + } - wxNodeBase *node = CreateNode(prev, position, object); + wxNodeBase *node = CreateNode(prev, next, object); if ( !m_nodeFirst ) { - m_nodeFirst = node; m_nodeLast = node; } @@ -241,7 +290,7 @@ wxNodeBase *wxListBase::Item(size_t n) const wxFAIL_MSG( "invalid index in wxListBase::Item" ); - return NULL; + return (wxNodeBase *)NULL; } wxNodeBase *wxListBase::Find(const wxListKey& key) const @@ -273,6 +322,13 @@ wxNodeBase *wxListBase::Find(void *object) const return (wxNodeBase *)NULL; } +int wxListBase::IndexOf(void *object) const +{ + wxNodeBase *node = Find( object ); + + return node ? node->IndexOf() : wxNOT_FOUND; +} + void wxListBase::DoDeleteNode(wxNodeBase *node) { // free node's data @@ -286,6 +342,8 @@ void wxListBase::DoDeleteNode(wxNodeBase *node) node->DeleteData(); } + // so that the node knows that it's being deleted by the list + node->m_list = NULL; delete node; } @@ -337,7 +395,6 @@ bool wxListBase::DeleteObject(void *object) return FALSE; } - void wxListBase::Clear() { wxNodeBase *current = m_nodeFirst; @@ -440,23 +497,6 @@ void wxListBase::Sort(const wxSortCompareFunction compfunc) delete[] objArray; } -bool wxListKey::operator==(wxListKeyValue value) const - { - switch ( m_keyType ) - { - default: - wxFAIL_MSG("bad key type."); - // let compiler optimize the line above away in release build - // by not putting return here... - - case wxKEY_STRING: - return strcmp(m_key.string, value.string) == 0; - - case wxKEY_INTEGER: - return m_key.integer == value.integer; - } - } - // ----------------------------------------------------------------------------- // wxList (a.k.a. wxObjectList) // ----------------------------------------------------------------------------- @@ -477,6 +517,34 @@ void wxStringListNode::DeleteData() delete [] (char *)GetData(); } +bool wxStringList::Delete(const char *s) +{ + wxStringListNode *current; + + for ( current = GetFirst(); current; current = current->GetNext() ) + { + if ( strcmp(current->GetData(), s) == 0 ) + { + DeleteNode(current); + return TRUE; + } + } + + // not found + return FALSE; +} + +void wxStringList::DoCopy(const wxStringList& other) +{ + wxASSERT( GetCount() == 0 ); // this list must be empty before copying! + + size_t count = other.GetCount(); + for ( size_t n = 0; n < count; n++ ) + { + Add(other.Item(n)->GetData()); + } +} + // Variable argument list, terminated by a zero // Makes new storage for the strings wxStringList::wxStringList (const char *first, ...) @@ -550,19 +618,20 @@ void wxStringList::Sort() { size_t N = GetCount(); char **array = new char *[N]; + wxStringListNode *node; size_t i = 0; - for ( wxStringListNode *node = GetFirst(); node; node = node->GetNext() ) + for ( node = GetFirst(); node; node = node->GetNext() ) { array[i++] = node->GetData(); } qsort (array, N, sizeof (char *), wx_comparestrings); - Clear(); - for (i = 0; i < N; i++) - Append (array[i]); + i = 0; + for ( node = GetFirst(); node; node = node->GetNext() ) + node->SetData( array[i++] ); - delete[]array; + delete [] array; }