Logo Search packages:      
Sourcecode: ragel version File versions

template<class T, class Resize>
void SVector< T, Resize >::remove ( long  pos,
long  len 
) [inline]

Remove len elements at position pos.

Destructor is called on all elements removed. Elements to the right of pos are shifted len spaces to the left to take up the free space. If pos is greater than or equal to the length of the vector then undefined behavior results. If pos is negative then it is treated as an offset relative to the length of the vector.

Definition at line 461 of file svector.h.

References STable< T >::data.

{
      /* If there is no data, we can't delete anything anyways. */
      if ( BaseTable::data != 0 ) {
            /* Get the header. */
            STabHead *head = ((STabHead*)BaseTable::data) - 1;

            /* If we are given a negative position to remove at then
             * treat it as a position relative to the length. */
            if ( pos < 0 )
                  pos = head->tabLen + pos;

            /* The first position after the last item deleted. */
            long endPos = pos + len;

            /* The New data length. */
            long i, newLen = head->tabLen - len;

            if ( head->refCount == 1 ) {
                  /* We are the only ones using the data. We can reuse 
                   * the existing space. */

                  /* The place in the data we are deleting at. */
                  T *dst = BaseTable::data + pos;

                  /* Call Destructors. */
                  T *item = BaseTable::data + pos;
                  for ( i = 0; i < len; i += 1, item += 1 )
                        item->~T();

                  /* Shift data over if necessary. */
                  long lenToSlideOver = head->tabLen - endPos;    
                  if ( len > 0 && lenToSlideOver > 0 )
                        memmove(BaseTable::data + pos, dst + len, sizeof(T)*lenToSlideOver);

                  /* Shrink the data if necessary. */
                  downResize( newLen );

                  if ( BaseTable::data != 0 ) {
                        /* Get the header again (because of the resize) and set the
                         * new data length. */
                        head = ((STabHead*)BaseTable::data) - 1;
                        head->tabLen = newLen;
                  }
            }
            else {
                  /* Must detach from the common data. Just copy the non-deleted
                   * items from the common data. */

                  /* Duplicate and grow down to newLen. This will set the length. */
                  downResizeDup( newLen );

                  /* Copy over just the non-deleted parts. */
                  const T *src = (T*) (head + 1);
                  T *dst = BaseTable::data;
                  for ( i = 0; i < pos; i++, dst++, src++ )
                        new(dst) T(*src);

                  /* ... and the second half. */
                  for ( i += len, src += len; i < head->tabLen; i++, src++, dst++ )
                        new(dst) T(*src);
            }
      }
}


Generated by  Doxygen 1.6.0   Back to index