wxArray(int n) mods for wxArrayString

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15285 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2002-04-27 20:02:17 +00:00
parent ebb396715d
commit 2a39c38627
2 changed files with 44 additions and 31 deletions

View File

@ -1036,16 +1036,16 @@ public:
int Index (const wxChar *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const;
// add new element at the end (if the array is not sorted), return its
// index
size_t Add(const wxString& str);
size_t Add(const wxString& str, size_t nInsert = 1);
// add new element at given position
void Insert(const wxString& str, size_t uiIndex);
void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
// expand the array to have count elements
void SetCount(size_t count);
// remove first item matching this value
void Remove(const wxChar *sz);
// remove item by index
void Remove(size_t nIndex);
void RemoveAt(size_t nIndex) { Remove(nIndex); }
void Remove(size_t nIndex, size_t nRemove = 1);
void RemoveAt(size_t nIndex, size_t nRemove = 1) { Remove(nIndex, nRemove); }
// sorting
// sort array elements in alphabetical order (or reversed alphabetical
@ -1065,7 +1065,7 @@ protected:
void Copy(const wxArrayString& src); // copies the contents of another array
private:
void Grow(); // makes array bigger if needed
void Grow(size_t nIncrement = 0); // makes array bigger if needed
void Free(); // free all the strings stored
void DoSort(); // common part of all Sort() variants

View File

@ -1948,7 +1948,7 @@ void wxArrayString::Copy(const wxArrayString& src)
}
// grow the array
void wxArrayString::Grow()
void wxArrayString::Grow(size_t nIncrement)
{
// only do it if no more place
if ( m_nCount == m_nSize ) {
@ -1967,10 +1967,12 @@ void wxArrayString::Grow()
// otherwise when it's called for the first time, nIncrement would be 0
// and the array would never be expanded
// add 50% but not too much
size_t nIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
size_t ndefIncrement = m_nSize < ARRAY_DEFAULT_INITIAL_SIZE
? ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1;
if ( nIncrement > ARRAY_MAXSIZE_INCREMENT )
nIncrement = ARRAY_MAXSIZE_INCREMENT;
if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT )
ndefIncrement = ARRAY_MAXSIZE_INCREMENT;
if ( nIncrement < ndefIncrement )
nIncrement = ndefIncrement;
m_nSize += nIncrement;
wxChar **pNew = new wxChar *[m_nSize];
@ -2113,7 +2115,7 @@ int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const
}
// add item at the end
size_t wxArrayString::Add(const wxString& str)
size_t wxArrayString::Add(const wxString& str, size_t nInsert)
{
if ( m_autoSort ) {
// insert the string at the correct position to keep the array sorted
@ -2137,41 +2139,49 @@ size_t wxArrayString::Add(const wxString& str)
wxASSERT_MSG( lo == hi, wxT("binary search broken") );
Insert(str, lo);
Insert(str, lo, nInsert);
return (size_t)lo;
}
else {
wxASSERT( str.GetStringData()->IsValid() );
Grow();
Grow(nInsert);
// the string data must not be deleted!
str.GetStringData()->Lock();
for (size_t i = 0; i < nInsert; i++)
{
// the string data must not be deleted!
str.GetStringData()->Lock();
// just append
m_pItems[m_nCount] = (wxChar *)str.c_str(); // const_cast
return m_nCount++;
// just append
m_pItems[m_nCount + i] = (wxChar *)str.c_str(); // const_cast
}
size_t ret = m_nCount;
m_nCount += nInsert;
return ret;
}
}
// add item at the given position
void wxArrayString::Insert(const wxString& str, size_t nIndex)
void wxArrayString::Insert(const wxString& str, size_t nIndex, size_t nInsert)
{
wxASSERT( str.GetStringData()->IsValid() );
wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Insert") );
wxCHECK_RET( m_nCount <= m_nCount + nInsert,
wxT("array size overflow in wxArrayString::Insert") );
Grow();
Grow(nInsert);
memmove(&m_pItems[nIndex + 1], &m_pItems[nIndex],
memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex],
(m_nCount - nIndex)*sizeof(wxChar *));
str.GetStringData()->Lock();
m_pItems[nIndex] = (wxChar *)str.c_str();
m_nCount++;
for (size_t i = 0; i < nInsert; i++)
{
str.GetStringData()->Lock();
m_pItems[nIndex + i] = (wxChar *)str.c_str();
}
m_nCount += nInsert;
}
// expand the array
@ -2185,16 +2195,19 @@ void wxArrayString::SetCount(size_t count)
}
// removes item from array (by index)
void wxArrayString::Remove(size_t nIndex)
void wxArrayString::Remove(size_t nIndex, size_t nRemove)
{
wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArrayString::Remove") );
wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArrayString::Remove") );
wxCHECK_RET( nIndex + nRemove <= m_nCount,
wxT("removing too many elements in wxArrayString::Remove") );
// release our lock
Item(nIndex).GetStringData()->Unlock();
for (size_t i = 0; i < nRemove; i++)
Item(nIndex + i).GetStringData()->Unlock();
memmove(&m_pItems[nIndex], &m_pItems[nIndex + 1],
(m_nCount - nIndex - 1)*sizeof(wxChar *));
m_nCount--;
memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove],
(m_nCount - nIndex - nRemove)*sizeof(wxChar *));
m_nCount -= nRemove;
}
// removes item from array (by value)