cgma
VGArray< T > Class Template Reference

#include <VGArray.hpp>

List of all members.

Public Member Functions

 VGArray (int initial_size=0)
 ~VGArray ()
int size () const
void size (int new_size)
void size_end (int new_size)
void push (const T &entry)
const T & pop ()
T & operator[] (int index)
const T & operator[] (int index) const
void remove (int index)
void insert (const T &entry, int index)
int find (const T &entry, int search_from=0) const
void reverse ()

Private Member Functions

const VGArrayoperator= (const VGArray< T > &)
 VGArray (const VGArray< T > &)

Static Private Member Functions

static int storage (int size)

Private Attributes

T * data_
int storage_
int size_

Detailed Description

template<class T>
class VGArray< T >

Definition at line 16 of file VGArray.hpp.


Constructor & Destructor Documentation

template<class T >
VGArray< T >::VGArray ( int  initial_size = 0)

Definition at line 7 of file VGArray.cpp.

{
  if( init_size <= 0 )
  {
    storage_ = 0;
    size_ = 0;
    data_ = 0;
  }
  else
  {
    size_ = init_size;
    storage_ = storage( init_size );
    data_ = new T[storage_];
  }
}
template<class T >
VGArray< T >::~VGArray ( )

Definition at line 24 of file VGArray.cpp.

{
  delete [] data_;
  storage_ = size_ = 0;
  data_ = 0;
}
template<class T>
VGArray< T >::VGArray ( const VGArray< T > &  ) [private]

Member Function Documentation

template<class T>
int VGArray< T >::find ( const T &  entry,
int  search_from = 0 
) const

Definition at line 138 of file VGArray.cpp.

{
  assert( search_from >= 0 );
  
  if( data_ )
  {
    T* ptr = data_ + search_from;
    T* end = data_ + size_;
    for(; ptr < end; ptr++ )
    {
      if( *ptr == entry )
        return ptr - data_;
    }
  }
  return -1;
}
template<class T>
void VGArray< T >::insert ( const T &  entry,
int  index 
)

Definition at line 119 of file VGArray.cpp.

{
  assert( index >= 0 && index <= size_ );

  size( size_ + 1 );
  T* ptr = data_ + size_ - 1;
  T* end = data_ + index;

  while( ptr > end )
  {
    T* next = ptr - 1;
    *ptr = *next;
    ptr = next;
  }

  data_[index] = entry;
}
template<class T>
const VGArray& VGArray< T >::operator= ( const VGArray< T > &  ) [private]
template<class T >
T & VGArray< T >::operator[] ( int  index) [inline]

Definition at line 83 of file VGArray.hpp.

{
  assert( index >= 0 && index < size_ );
  return data_[index];
}
template<class T >
const T & VGArray< T >::operator[] ( int  index) const [inline]

Definition at line 90 of file VGArray.hpp.

{
  assert( index >= 0 && index < size_ );
  return data_[index];
}
template<class T>
const T& VGArray< T >::pop ( ) [inline]
template<class T>
void VGArray< T >::push ( const T &  entry) [inline]

Definition at line 97 of file VGArray.hpp.

{
  int s = size_;
  size( s + 1 );
  data_[s] = entry;
}
template<class T >
void VGArray< T >::remove ( int  index)

Definition at line 104 of file VGArray.cpp.

{
  assert( index >= 0 && index < size_ );
  size_--;
  T* ptr = data_ + index;
  T* end = data_ + size_;
  while( ptr < end )
  {
    T* next = ptr + 1;
    *ptr = *next;
    ptr = next;
  }
}
template<class T >
void VGArray< T >::reverse ( )

Definition at line 156 of file VGArray.cpp.

{
  T* start = data_;
  T* end   = data_ + size_ - 1;
  while (start < end)
  {
    std::swap(*start,*end);
    start++;
    end--;
  }
}
template<class T >
int VGArray< T >::size ( ) const [inline]

Definition at line 79 of file VGArray.hpp.

  { return size_; }
template<class T >
void VGArray< T >::size ( int  new_size)

Definition at line 32 of file VGArray.cpp.

{
  if( new_size <= 0 ) 
  {
    new_size = 0;
  }
  else if( new_size > storage_ )
  {
    int new_stor = storage(new_size);
    assert( new_stor > storage_ );
    T* new_array = new T[new_stor];
    if( data_ )
    {
      memcpy( new_array, data_, size_ * sizeof(T) );
      delete [] data_;
    }
    data_ = new_array;
    storage_ = new_stor;
  }
  size_ = new_size;
}
template<class T >
void VGArray< T >::size_end ( int  new_size)

Definition at line 55 of file VGArray.cpp.

{
  if( new_size <= 0 ) 
  {
    new_size = 0;
  }
  else if( new_size > storage_ )
  {
    int new_stor = storage(new_size);
    assert( new_stor > storage_ );
    T* new_array = new T[new_stor];
    if( data_ )
    {
      memcpy( new_array + new_size - size_, data_, size_ * sizeof(T) );
      delete [] data_;
    }
    data_ = new_array;
    storage_ = new_stor;
  }
  else if (new_size > size_)
  {
    T* read = data_ + size_;
    T* write = data_ + new_size;
    while ( read > data_ )
      *(--write) = *(--read);
  }
  else if (new_size < size_)
  {
    T* read = data_ + size_ - new_size;
    T* write = data_;
    T* end = data_ + size_;
    while ( read < end )
      *(write--) = *(read--);
  }
  size_ = new_size;
}
template<class T >
int VGArray< T >::storage ( int  size) [static, private]

Definition at line 93 of file VGArray.cpp.

{
  assert( size <= 0x40000000 ); //no power of 2 greater than size (overflow)

  int result;
  for( result = 1; result < size; result = result << 1 );
  assert(result>=size);
  return result;
}

Member Data Documentation

template<class T>
T* VGArray< T >::data_ [private]

Definition at line 71 of file VGArray.hpp.

template<class T>
int VGArray< T >::size_ [private]

Definition at line 73 of file VGArray.hpp.

template<class T>
int VGArray< T >::storage_ [private]

Definition at line 72 of file VGArray.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines