RemoveAt() added to array classes

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@4589 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-11-16 13:28:23 +00:00
parent bffa1c7746
commit 8a729bb860
4 changed files with 37 additions and 14 deletions

View File

@@ -200,6 +200,7 @@ does exactly the same as \helpref{Item()}{wxarrayitem} method.
\helpref{WX\_CLEAR\_ARRAY}{wxcleararray}\\ \helpref{WX\_CLEAR\_ARRAY}{wxcleararray}\\
\helpref{Empty}{wxarrayempty}\\ \helpref{Empty}{wxarrayempty}\\
\helpref{Clear}{wxarrayclear}\\ \helpref{Clear}{wxarrayclear}\\
\helpref{RemoveAt}{wxarrayremoveat}\\
\helpref{Remove}{wxarrayremove} \helpref{Remove}{wxarrayremove}
\membersection{Searching and sorting} \membersection{Searching and sorting}
@@ -412,7 +413,6 @@ it exists only for compatibility.
\func{T *}{Detach}{\param{size\_t }{index}} \func{T *}{Detach}{\param{size\_t }{index}}
Removes the element from the array, but, unlike, Removes the element from the array, but, unlike,
\helpref{Remove()}{wxarrayremove} doesn't delete it. The function returns the \helpref{Remove()}{wxarrayremove} doesn't delete it. The function returns the
pointer to the removed element. pointer to the removed element.
@@ -493,12 +493,13 @@ the array classes.
\membersection{wxArray::Remove}\label{wxarrayremove} \membersection{wxArray::Remove}\label{wxarrayremove}
\func{\void}{Remove}{\param{size\_t }{index}}
\func{\void}{Remove}{\param{T }{item}} \func{\void}{Remove}{\param{T }{item}}
Removes the element from the array either by index or by value. When an element Removes the element from the array either by value: the first item of the
is removed from wxObjArray it is deleted by the array - use array equal to {\it item} is removed, an assert failure will result from an
attempt to remove an item which doesn't exist in the array.
When an element is removed from wxObjArray it is deleted by the array - use
\helpref{Detach()}{wxobjarraydetach} if you don't want this to happen. On the \helpref{Detach()}{wxobjarraydetach} if you don't want this to happen. On the
other hand, when an object is removed from a wxArray nothing happens - you other hand, when an object is removed from a wxArray nothing happens - you
should delete the it manually if required: should delete the it manually if required:
@@ -512,6 +513,25 @@ array.Remove(n)
See also \helpref{WX\_CLEAR\_ARRAY}{wxcleararray} macro which deletes all See also \helpref{WX\_CLEAR\_ARRAY}{wxcleararray} macro which deletes all
elements of a wxArray (supposed to contain pointers). elements of a wxArray (supposed to contain pointers).
\membersection{wxArray::RemoveAt}\label{wxarrayremoveat}
\func{\void}{RemoveAt}{\param{size\_t }{index}}
Removes the element from the array either by index. When an element
is removed from wxObjArray it is deleted by the array - use
\helpref{Detach()}{wxobjarraydetach} if you don't want this to happen. On the
other hand, when an object is removed from a wxArray nothing happens - you
should delete the it manually if required:
\begin{verbatim}
T *item = array[n];
delete item;
array.RemoveAt(n)
\end{verbatim}
See also \helpref{WX\_CLEAR\_ARRAY}{wxcleararray} macro which deletes all
elements of a wxArray (supposed to contain pointers).
\membersection{wxArray::Shrink}\label{wxarrayshrink} \membersection{wxArray::Shrink}\label{wxarrayshrink}
\func{void}{Shrink}{\void} \func{void}{Shrink}{\void}

View File

@@ -21,7 +21,7 @@
*****************************************************************************/ *****************************************************************************/
// needed to resolve the conflict between global T and macro parameter T // needed to resolve the conflict between global T and macro parameter T
#define _WX_ERROR_REMOVE2(x) wxT("bad index in " #x "::Remove()") #define _WX_ERROR_REMOVE2(x) wxT("bad index in " #x "::RemoveAt()")
// macro implements remaining (not inline) methods of template list // macro implements remaining (not inline) methods of template list
// (it's private to this file) // (it's private to this file)
@@ -59,13 +59,13 @@ void name::Empty() \
wxBaseArray::Clear(); \ wxBaseArray::Clear(); \
} \ } \
\ \
void name::Remove(size_t uiIndex) \ void name::RemoveAt(size_t uiIndex) \
{ \ { \
wxCHECK_RET( uiIndex < Count(), _WX_ERROR_REMOVE2(name) ); \ wxCHECK_RET( uiIndex < Count(), _WX_ERROR_REMOVE2(name) ); \
\ \
delete (T*)wxBaseArray::Item(uiIndex); \ delete (T*)wxBaseArray::Item(uiIndex); \
\ \
wxBaseArray::Remove(uiIndex); \ wxBaseArray::RemoveAt(uiIndex); \
} \ } \
\ \
void name::Add(const T& item) \ void name::Add(const T& item) \

View File

@@ -132,7 +132,7 @@ protected:
/// remove first item matching this value /// remove first item matching this value
void Remove(long lItem); void Remove(long lItem);
/// remove item by index /// remove item by index
void Remove(size_t uiIndex); void RemoveAt(size_t uiIndex);
//@} //@}
/// sort array elements using given compare function /// sort array elements using given compare function
@@ -198,7 +198,8 @@ public: \
void Insert(T Item, size_t uiIndex) \ void Insert(T Item, size_t uiIndex) \
{ wxBaseArray::Insert((long)Item, uiIndex) ; } \ { wxBaseArray::Insert((long)Item, uiIndex) ; } \
\ \
void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \ void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
void Remove(T Item) \ void Remove(T Item) \
{ int iIndex = Index(Item); \ { int iIndex = Index(Item); \
wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
@@ -260,7 +261,8 @@ public: \
void Add(T Item) \ void Add(T Item) \
{ wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \ { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
\ \
void Remove(size_t uiIndex) { wxBaseArray::Remove(uiIndex); } \ void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
void Remove(T Item) \ void Remove(T Item) \
{ int iIndex = Index(Item); \ { int iIndex = Index(Item); \
wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
@@ -307,7 +309,8 @@ public: \
T* Detach(size_t uiIndex) \ T* Detach(size_t uiIndex) \
{ T* p = (T*)wxBaseArray::Item(uiIndex); \ { T* p = (T*)wxBaseArray::Item(uiIndex); \
wxBaseArray::Remove(uiIndex); return p; } \ wxBaseArray::Remove(uiIndex); return p; } \
void Remove(size_t uiIndex); \ void Remove(size_t uiIndex) { RemoveAt(uiIndex); } \
void RemoveAt(size_t uiIndex); \
\ \
void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \ void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
\ \

View File

@@ -261,9 +261,9 @@ void wxBaseArray::Insert(long lItem, size_t nIndex)
} }
// removes item from array (by index) // removes item from array (by index)
void wxBaseArray::Remove(size_t nIndex) void wxBaseArray::RemoveAt(size_t nIndex)
{ {
wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Remove") ); wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::RemoveAt") );
memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1], memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
(m_nCount - nIndex - 1)*sizeof(long)); (m_nCount - nIndex - 1)*sizeof(long));