bool GrGLExtensions::remove(const char ext[]) { SkASSERT(fInitialized); int idx = find_string(*fStrings, ext); if (idx >= 0) { // This is not terribly effecient but we really only expect this function to be called at // most a handful of times when our test programs start. SkAutoTDelete< SkTArray<SkString> > oldStrings(fStrings.detach()); fStrings.reset(SkNEW(SkTArray<SkString>(oldStrings->count() - 1))); fStrings->push_back_n(idx, &oldStrings->front()); fStrings->push_back_n(oldStrings->count() - idx - 1, &(*oldStrings)[idx] + 1); return true; } else { return false; } }
// add item at the given position void wxArrayString::Insert(const wxString& str, size_t nIndex, size_t nInsert) { 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") ); wxScopedArray<wxString> oldStrings(Grow(nInsert)); for (int j = m_nCount - nIndex - 1; j >= 0; j--) m_pItems[nIndex + nInsert + j] = m_pItems[nIndex + j]; for (size_t i = 0; i < nInsert; i++) { m_pItems[nIndex + i] = str; } m_nCount += nInsert; }
// add item at the end 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 size_t lo = 0, hi = m_nCount; while ( lo < hi ) { size_t i; i = (lo + hi)/2; int res; res = m_compareFunction ? m_compareFunction(str, m_pItems[i]) : str.Cmp(m_pItems[i]); if ( res < 0 ) hi = i; else if ( res > 0 ) lo = i + 1; else { lo = hi = i; break; } } wxASSERT_MSG( lo == hi, wxT("binary search broken") ); Insert(str, lo, nInsert); return (size_t)lo; } else { // Now that we must postpone freeing the old memory until we don't need it // any more, i.e. don't reference "str" which could be a reference to one // of our own strings. wxScopedArray<wxString> oldStrings(Grow(nInsert)); for (size_t i = 0; i < nInsert; i++) { // just append m_pItems[m_nCount + i] = str; } size_t ret = m_nCount; m_nCount += nInsert; return ret; } }
// range insert (STL 23.2.4.3) void wxArrayString::insert(iterator it, const_iterator first, const_iterator last) { const int idx = it - begin(); // grow it once wxScopedArray<wxString> oldStrings(Grow(last - first)); // reset "it" since it can change inside Grow() it = begin() + idx; while ( first != last ) { it = insert(it, *first); // insert returns an iterator to the last element inserted but we need // insert the next after this one, that is before the next one ++it; ++first; } }