added wxNode::IndexOf and wxList::IndexOf (patch by Frank McIngvale)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1096 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1998-12-02 10:32:03 +00:00
parent 7b67869804
commit 77c5eefb1f
4 changed files with 59 additions and 11 deletions

View File

@@ -144,6 +144,13 @@ Returns the node whose stored key matches {\it key}. Use on a keyed list only.
Returns the first node in the list (NULL if the list is empty). Returns the first node in the list (NULL if the list is empty).
\membersection{wxList::IndexOf}
\func{int}{IndexOf}{\param{wxObject*}{ obj }}
Returns the index of {\it obj} within the list or NOT\_FOUND if {\it obj}
is not found in the list.
\membersection{wxList::Insert} \membersection{wxList::Insert}
\func{wxNode *}{Insert}{\param{wxObject *}{object}} \func{wxNode *}{Insert}{\param{wxObject *}{object}}

View File

@@ -38,4 +38,10 @@ Retrieves the previous node (NULL if at start of list).
Sets the data associated with the node (usually the pointer will have been Sets the data associated with the node (usually the pointer will have been
set when the node was created). set when the node was created).
\membersection{wxNode::IndexOf}
\func{int}{IndexOf}{\void}
Returns the zero-based index of this node within the list. The return value
will be NOT\_FOUND if the node has not been added to a list yet.

View File

@@ -6,7 +6,7 @@
// Created: 29/01/98 // Created: 29/01/98
// RCS-ID: $Id$ // RCS-ID: $Id$
// Copyright: (c) 1998 Julian Smart // Copyright: (c) 1998 Julian Smart
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
/* /*
@@ -163,6 +163,9 @@ protected:
void *GetData() const { return m_data; } void *GetData() const { return m_data; }
void SetData(void *data) { m_data = data; } void SetData(void *data) { m_data = data; }
// get 0-based index of this node within the list or NOT_FOUND
int IndexOf() const;
virtual void DeleteData() { } virtual void DeleteData() { }
private: private:
@@ -248,7 +251,7 @@ protected:
bool DeleteNode(wxNodeBase *node); bool DeleteNode(wxNodeBase *node);
// finds object pointer and deletes node (and object if DeleteContents // finds object pointer and deletes node (and object if DeleteContents
// is on), returns FALSE if object not found // is on), returns FALSE if object not found
bool DeleteObject(void *object); bool DeleteObject(void *object);
// search (all return NULL if item not found) // search (all return NULL if item not found)
// by data // by data
@@ -257,6 +260,9 @@ protected:
// by key // by key
wxNodeBase *Find(const wxListKey& key) const; wxNodeBase *Find(const wxListKey& key) const;
// get 0-based index of object or NOT_FOUND
int IndexOf( void *object ) const;
// this function allows the sorting of arbitrary lists by giving // this function allows the sorting of arbitrary lists by giving
// a function to compare two list elements. The list is sorted in place. // a function to compare two list elements. The list is sorted in place.
void Sort(const wxSortCompareFunction compfunc); void Sort(const wxSortCompareFunction compfunc);
@@ -377,6 +383,9 @@ private:
virtual nodetype *Find(const wxListKey& key) const \ virtual nodetype *Find(const wxListKey& key) const \
{ return (nodetype *)wxListBase::Find(key); } \ { return (nodetype *)wxListBase::Find(key); } \
\ \
int IndexOf( T *object ) const \
{ return wxListBase::IndexOf(object); } \
\
void Sort(wxSortFuncFor_##name func) \ void Sort(wxSortFuncFor_##name func) \
{ wxListBase::Sort((wxSortCompareFunction)func); } \ { wxListBase::Sort((wxSortCompareFunction)func); } \
\ \
@@ -430,7 +439,7 @@ public:
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// wxStringList class for compatibility with the old code // wxStringList class for compatibility with the old code
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
WX_DECLARE_LIST_2(char, wxStringListBase, wxStringListNode); WX_DECLARE_LIST_2(char, wxStringListBase, wxStringListNode);
class WXDLLEXPORT wxStringList : public wxStringListBase class WXDLLEXPORT wxStringList : public wxStringListBase

View File

@@ -68,7 +68,7 @@ bool wxListKey::operator==(wxListKeyValue value) const
case wxKEY_INTEGER: case wxKEY_INTEGER:
return m_key.integer == value.integer; return m_key.integer == value.integer;
} }
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// wxNodeBase // wxNodeBase
@@ -82,28 +82,28 @@ wxNodeBase::wxNodeBase(wxListBase *list,
m_data = data; m_data = data;
m_previous = previous; m_previous = previous;
m_next = next; m_next = next;
switch ( key.GetKeyType() ) switch ( key.GetKeyType() )
{ {
case wxKEY_NONE: case wxKEY_NONE:
break; break;
case wxKEY_INTEGER: case wxKEY_INTEGER:
m_key.integer = key.GetNumber(); m_key.integer = key.GetNumber();
break; break;
case wxKEY_STRING: case wxKEY_STRING:
// to be free()d later // to be free()d later
m_key.string = strdup(key.GetString()); m_key.string = strdup(key.GetString());
break; break;
default: default:
wxFAIL_MSG("invalid key type"); wxFAIL_MSG("invalid key type");
} }
if ( previous ) if ( previous )
previous->m_next = this; previous->m_next = this;
if ( next ) if ( next )
next->m_previous = this; next->m_previous = this;
} }
@@ -119,6 +119,25 @@ wxNodeBase::~wxNodeBase()
} }
} }
int wxNodeBase::IndexOf() const
{
wxCHECK_MSG( m_list, NOT_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 // wxListBase
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -304,6 +323,13 @@ wxNodeBase *wxListBase::Find(void *object) const
return (wxNodeBase *)NULL; return (wxNodeBase *)NULL;
} }
int wxListBase::IndexOf(void *object) const
{
wxNodeBase *node = Find( object );
return node ? node->IndexOf() : NOT_FOUND;
}
void wxListBase::DoDeleteNode(wxNodeBase *node) void wxListBase::DoDeleteNode(wxNodeBase *node)
{ {
// free node's data // free node's data
@@ -498,7 +524,7 @@ void wxStringList::DoCopy(const wxStringList& other)
size_t count = other.GetCount(); size_t count = other.GetCount();
for ( size_t n = 0; n < count; n++ ) for ( size_t n = 0; n < count; n++ )
{ {
Add(other.Item(n)->GetData()); Add(other.Item(n)->GetData());
} }
} }