Main Page | Class Hierarchy | Class List | File List | Class Members

VectorClass.h

00001 // $Id: VectorClass.h,v 1.2 2004/01/06 22:25:27 jbryan Exp $
00002 
00003 /***************************************************************
00004  *                FLU - FLTK Utility Widgets 
00005  *  Copyright (C) 2002 Ohio Supercomputer Center, Ohio State University
00006  *
00007  * This file and its content is protected by a software license.
00008  * You should have received a copy of this license with this file.
00009  * If not, please contact the Ohio Supercomputer Center immediately:
00010  * Attn: Jason Bryan Re: FLU 1224 Kinnear Rd, Columbus, Ohio 43212
00011  * 
00012  ***************************************************************/
00013 
00014 
00015 
00016 #ifndef _FLU_VECTOR_CLASS_H
00017 #define _FLU_VECTOR_CLASS_H
00018 
00019 #define MakeVectorClass( T, C ) \
00020 class C \
00021 { \
00022 public: \
00023  \
00024   C() { _array = NULL; _size = 0; } \
00025  \
00026   ~C() { clear(); } \
00027  \
00028   inline void add( const T& item ) { insert( size(), item ); } \
00029  \
00030   inline T& operator [](int i) { return _array[i]; } \
00031  \
00032   inline T operator [](int i) const { return _array[i]; } \
00033  \
00034   inline unsigned int size() const { return _size; } \
00035  \
00036   C& operator =( const C &v ) \
00037   { \
00038     clear(); \
00039     if( v.size() ) \
00040       { \
00041         _array = new T[v.size()]; \
00042         for( unsigned int i = 0; i < v.size(); i++ ) \
00043           _array[i] = v._array[i]; \
00044       } \
00045     return *this; \
00046   } \
00047  \
00048   void insert( unsigned int pos, const T &item ) \
00049   { \
00050     if( pos > _size ) \
00051       pos = _size; \
00052     if( _size == 0 ) \
00053       { \
00054         _array = new T[1]; \
00055       } \
00056     else \
00057       { \
00058         if( !( _size & (_size-1) ) ) \
00059           { \
00060             T* temp = new T[_size*2]; \
00061             for( unsigned int i = 0; i < _size; i++ ) \
00062               temp[i] = _array[i]; \
00063             delete[] _array; \
00064             _array = temp; \
00065           } \
00066         for( unsigned int s = _size; s > pos; s-- ) \
00067           _array[s] = _array[s-1]; \
00068       } \
00069     _size++; \
00070     _array[pos] = item; \
00071   } \
00072  \
00073   void erase( unsigned int pos ) \
00074   { \
00075     if( pos >= _size ) \
00076       return; \
00077     _size--; \
00078     if( _size == 0 ) \
00079       { \
00080         delete[] _array; \
00081         _array = NULL; \
00082       } \
00083     else \
00084       { \
00085         for( ; pos < _size; pos++ ) \
00086           _array[pos] = _array[pos+1]; \
00087       } \
00088   } \
00089  \
00090   void clear() \
00091   { \
00092     if( _array ) \
00093       delete[] _array; \
00094     _array = NULL; \
00095     _size = 0; \
00096   } \
00097  \
00098 protected: \
00099   T *_array; \
00100   unsigned int _size; \
00101 }
00102 
00103 #endif

Generated on Mon Aug 2 10:27:46 2004 for FLTK Utility Library and Widget Collection (FLU) by doxygen 1.3.5