Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MNArrayList.h

Go to the documentation of this file.
00001 /* Copyright (C) 2000 KOM/Darmstadt University of Technology 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, code may be used in unmodified form provided 00006 * that this copyright notice and this permission notice appear in 00007 * supporting documentation. 00008 * 00009 * This software is provided "as is" and without any express or implied 00010 * warranties, including, without limitation, the implied warranty of 00011 * fitness for a particular purpose. 00012 * 00013 * The code may be subjected to the GNU General Public License, Version 2, 00014 * and re-distributed under the terms of this license. 00015 * As a special exception, permission is granted to link this code 00016 * with the Qt library and distribute executables, as long as you 00017 * follow the requirements of the GNU GPL in regard to all of the 00018 * software in the executable aside from Qt. 00019 * 00020 * Commercial use other than under the terms of the GNU General Public 00021 * License is allowed only after express negotiation of conditions 00022 * with the authors. 00023 */ 00024 #ifndef MN_ARRAY_LIST_H 00025 #define MN_ARRAY_LIST_H 00026 00027 #include <assert.h> 00028 #include <stdio.h> 00029 #include <sys/types.h> 00030 #include <string.h> 00031 #include <strings.h> 00032 00033 #include "MNValid.h" 00034 00040 template <class T> class MNArrayList 00041 { 00042 DECLARE_VALID 00043 private: 00044 size_t _use_size; 00045 size_t _real_size; 00046 T* _array; 00047 00048 typedef T Item; 00049 00050 void clone( const MNArrayList<T>& orig ) { 00051 if( _array ) delete [] _array; 00052 00053 _use_size = orig._use_size; 00054 _real_size = orig._real_size; 00055 _array = new Item[ _real_size ]; 00056 memcpy ( _array, orig._array, _real_size*sizeof(Item) ); 00057 } 00058 00059 void grow( ) { 00060 if ( _real_size == 0 ) 00061 { 00062 _real_size = 10; 00063 _array = new Item[ _real_size ]; 00064 memset ( _array, 0, _real_size*sizeof(Item) ); 00065 } else { 00066 Item* _old_array = _array; 00067 _real_size += 10; 00068 _array = new Item[ _real_size ]; 00069 memcpy ( _array, _old_array, (_real_size-10)*sizeof(Item) ); 00070 delete [] _old_array; 00071 } 00072 } 00073 00074 void private_del( size_t it ) { 00075 size_t rest = _use_size - it; 00076 if ( rest > 1 ) 00077 { 00078 memmove( &_array[it], 00079 &_array[it+1], 00080 (rest-1)*sizeof(Item) ); 00081 } 00082 // else nothing to do, it pointed to the last item 00083 _use_size--; 00084 } 00085 00086 public: 00087 typedef int Iterator; 00088 00089 MNArrayList() { 00090 _use_size = 0; 00091 _real_size = 0; 00092 _array = NULL; 00093 // _array = new Item[ x * y ]; 00094 // memset ( _array, '\0', x*y*sizeof(Item) ); 00095 MAKE_VALID 00096 } 00097 00098 MNArrayList( const MNArrayList<T>& orig ) { 00099 _use_size = 0; 00100 _real_size = 0; 00101 _array = NULL; 00102 clone(orig); 00103 MAKE_VALID 00104 } 00105 00106 ~MNArrayList( ) { 00107 delete [] _array; 00108 MAKE_INVALID 00109 } 00110 00111 inline T& operator()(int idx) { 00112 CHECK_VALID 00113 assert( 0 <= idx ); 00114 assert( (size_t)idx < _use_size); 00115 return _array[ idx ]; 00116 } 00117 00118 inline const T& operator()(int idx) const { 00119 CHECK_VALID 00120 assert( 0 <= idx ); 00121 assert( (size_t)idx < _use_size); 00122 return _array[ idx ]; 00123 } 00124 00125 MNArrayList<T>& operator=( const MNArrayList<T>& orig ) { 00126 clone(orig); 00127 return *this; 00128 } 00129 00130 00131 inline size_t size() const { 00132 CHECK_VALID 00133 return _use_size; 00134 } 00135 00136 inline T* array() const { 00137 CHECK_VALID 00138 return _array; 00139 } 00140 00141 void append( const Item& t ) { 00142 CHECK_VALID 00143 if ( _use_size == _real_size ) grow(); 00144 _array[_use_size] = t; 00145 _use_size++; 00146 } 00147 00148 bool empty() const { 00149 CHECK_VALID 00150 return ( _use_size==0 ); 00151 } 00152 00153 Iterator first() { 00154 CHECK_VALID 00155 return 0; 00156 } 00157 00158 const T& inf( size_t it ) const { 00159 CHECK_VALID 00160 assert( it < _use_size ); 00161 return _array[it]; 00162 } 00163 00164 const T& inf( Iterator it ) const { 00165 CHECK_VALID 00166 assert( it >= 0 ); 00167 assert( (size_t)it < _use_size ); 00168 return _array[it]; 00169 } 00170 00171 Iterator next( Iterator& it ) { 00172 CHECK_VALID 00173 assert( it >= 0 ); 00174 it++; 00175 return it; 00176 } 00177 00178 void del( size_t it ) { 00179 CHECK_VALID 00180 assert( (size_t)it < _use_size ); 00181 private_del(it); 00182 } 00183 00184 void del( Iterator& it ) { 00185 CHECK_VALID 00186 assert( it >= 0 ); 00187 assert( (size_t)it < _use_size ); 00188 private_del( (size_t&)it ); 00189 00190 // invalidate the iterator (inf,next,del will zap) 00191 it = -1; 00192 } 00193 00194 void clear() { 00195 _use_size = 0; 00196 } 00197 }; 00198 00199 #endif /* MN_ARRAY_LIST_H */ 00200

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8