00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00094
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
00191 it = -1;
00192 }
00193
00194 void clear() {
00195 _use_size = 0;
00196 }
00197 };
00198
00199
#endif
00200