3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2000 Manuel Moos (manuel@moosnet.de)
8
**************************************************************************
10
This program is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; either version 2
13
of the License, or (at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
***************************************************************************
28
#ifndef ArmageTron_ARRAY_H
29
#define ArmageTron_ARRAY_H
37
class GrowingArrayBase {
38
int len; // current logical size
39
int size; // current size in memory
40
void *base; // start of memory block
42
const GrowingArrayBase & operator=(const GrowingArrayBase &);
43
GrowingArrayBase(GrowingArrayBase &);
46
void ResizeBase(int i,int size_of_T, bool useMalloc);
47
void *Base() const {return base;}
49
GrowingArrayBase(int firstsize,int size_of_T, bool useMalloc);
50
void Delete( bool useMalloc );
55
void SetSize(int s){size=s;}
56
void SetBase(void *b){base=b;}
61
void ComplainIfFull();
63
void SetLen(int i){len=i;}
64
int Len()const {return len;}
65
int Size() const {return size;}
69
template<class T, bool MALLOC=false> class tArray: public GrowingArrayBase {
73
for(i=Size()-1;i>=0;i--)
74
new(reinterpret_cast<T *>(Base())+i) T();
79
ResizeBase(i,sizeof(T),MALLOC);
80
// dump(low,flow,"Array resize from " << oldsize << " to " << Size());
81
for(i=Size()-1;i>=oldsize;i--)
82
new(reinterpret_cast<T *>(Base())+i) T();
88
for(i=Size()-1;i>=0;i--){
89
// dump(low,flow,"i=" << i);
90
((reinterpret_cast<T *>(Base()))+i)->~T();
96
void CopyFrom(const tArray &A){
98
for(i=Len()-1;i>=0;i--)
99
new(reinterpret_cast<T *>(Base())+i) T(A(i));
100
for(i=Len();i<Size();i++)
101
new(reinterpret_cast<T *>(Base())+i) T();
106
GrowingArrayBase::SetLen(i);
107
if (i>Size()) resize(i);
117
tArray(int firstsize=0)
118
:GrowingArrayBase(firstsize,sizeof(T),MALLOC) {
119
// dump(low,flow,"con:size " << firstsize);
124
tArray(const tArray &A)
125
:GrowingArrayBase(A.Len(),sizeof(T),MALLOC){
130
T& operator[](int i) {
133
tERR_ERROR_INT("Range error;accesed negative element " << i );
139
// dump(low,flow,"[" << i << "]" << "=" << ((T *)Base())[i] << '\n');
141
return((reinterpret_cast<T *>(Base()))[i]);
145
T& operator()(int i) const{
146
tASSERT( i >= 0 && i < Len() );
148
return((reinterpret_cast<T *>(Base()))[i]);
151
T* operator+(int i) const{
153
if (i<0) {tERR_ERROR_INT("Range error;accesed negative element " << i )}
155
{tERR_ERROR_INT("Range error;accesed element "
156
<< i <<" of maximal " <<Len())}
159
return(reinterpret_cast<T *>(Base())+i);
162
const tArray<T> &operator=(const tArray<T> &A){
171
void RemoveAt( int index )
173
int newLen = this->Len()-1;
174
T keep = (*this)[ index ];
175
if ( index < newLen )
176
(*this)[ index ] = (*this)[ newLen ];
177
this->SetLen( newLen );
180
bool Remove( const T& t )
182
for ( int i = this->Len()-1; i >= 0; --i )
184
if ( (*this)[i] == t )
194
void Insert( const T& t )
196
SetLen( this->Len()+1 );
197
(*this)[ this->Len() -1 ] = t;