1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
// $Id: array.hh,v 1.2 1996/03/07 20:33:01 aml Exp $
/**************************************************************
Program: array (lib class)
File: array.hh
Version: 1.0
Author: Arlindo Oliveira (aml@ic)
Created:
Modified:
Comments:
****************************************************************/
#ifndef _ARRAY_DOT_HH_
#define _ARRAY_DOT_HH_
#include "utils.hh"
#define DEFAULT_ARRAY_SIZE 5
#define CHECK_ARRAY_BOUNDS 0
#define NEED_SORT 1
template<class T>
class array {
protected:
int size;
int data_allocated;
T *data;
public:
int cnt;
int &n() {return(cnt);}
T &last() {return(operator[](cnt-1));}
T &pop() {cnt--; return(operator[](cnt));}
/* The HP compiler does not take for loops in inline functions */
#ifndef hpux
array(int isize = DEFAULT_ARRAY_SIZE) {
cnt = 0;
size = isize;
if (isize != 0) {
data = new T[size];
data_allocated = 1;
}
else {
data = NULL;
data_allocated = 0;
}
}
array(const array<T> &right) {
int i;
data = new T[right.size];
size = right.size;
data_allocated = 1;
cnt = right.cnt;
for(i=0; i<size; i++)
data[i] = right.data[i];
}
void
resize(int newsize) {
T *temp;
int i;
temp = new T[newsize];
for(i=0; i<size; i++)
temp[i] = data[i];
if (data_allocated)
delete [] data;
size = newsize;
data = temp;
data_allocated = 1;
}
#else
array(int isize = DEFAULT_ARRAY_SIZE);
array(const array<T> &right) ;
void resize(int newsize) ;
#endif
~array() {if (data_allocated) delete [] data; }
void operator=(const array<T> &right) {
int i;
if (size < right.size)
resize(right.size);
cnt = right.cnt;
for(i=MIN(size,right.size)-1; i>=0; i--)
data[i] = right.data[i];
}
void operator+=(const T &value) {
#if CHECK_ARRAY_BOUNDS
if (cnt < 0 || cnt > size)
internal_error();
#endif
if (cnt == size)
resize(2*size);
data[cnt++] = value;
}
void operator--(int) {cnt--;};
T &operator[](const int index) {
#if CHECK_ARRAY_BOUNDS
if (index < 0) internal_error();
#endif
if (index >= size) {
#if CHECK_ARRAY_BOUNDS
//internal_error();
#endif
resize(MAX(2*size,index+DEFAULT_ARRAY_SIZE));
}
return(data[index]);
}
void sort(int sort_f(const void *f1, const void *d2), int icnt=0) {
if (icnt)
qsort(data,icnt,sizeof(T),sort_f);
else
qsort(data,cnt,sizeof(T),sort_f);
}
} ;
#ifndef hpux
template<class T>
ostream &operator<<(ostream &s, array<T> &arr) {
int i;
s << arr.cnt << "\n";
for(i=0; i<arr.cnt; i++)
s << arr[i] << " ";
return(s);
}
template<class T>
int operator<=(const T &elem, array<T> &arr) {
int i;
for(i=0; i<arr.cnt; i++)
if (arr[i] == elem) return(TRUE);
return(FALSE);
}
template<class T>
ofstream &operator<<(ofstream &s, array<T> &arr) {
int i;
s << arr.cnt << "\n";
for(i=0; i<arr.cnt; i++)
s << arr[i] << " ";
return(s);
}
template<class T>
istream &operator>>(istream &s, array<T> &arr) {
int i;
s >> arr.cnt;
for(i=0; i<arr.cnt; i++)
s >> arr[i];
return(s);
}
template<class T>
ifstream &operator>>(ifstream &s, array<T> &arr) {
int i;
s >> arr.cnt;
for(i=0; i<arr.cnt; i++)
s >> arr[i];
return(s);
}
#else
template<class T>
array<T>::array(const array<T> &right) {
int i;
data = new T[right.size];
size = right.size;
data_allocated = 1;
cnt = right.cnt;
for(i=0; i<size; i++)
data[i] = right.data[i];
}
template<class T>
array<T>::array(int isize) {
cnt = 0;
size = isize;
if (isize != 0) {
data = new T[size];
data_allocated = 1;
}
else {
data = NULL;
data_allocated = 0;
}
}
template<class T>
void array<T>::resize(int newsize) {
T *temp;
int i;
temp = new T[newsize];
for(i=0; i<size; i++)
temp[i] = data[i];
if (data_allocated)
delete [] data;
size = newsize;
data = temp;
data_allocated = 1;
}
template<class T>
int operator<=(const T &elem, array<T> &arr) ;
template<class T>
ostream &operator<<(ostream &s, array<T> &arr) ;
template<class T>
ofstream &operator<<(ofstream &s, array<T> &arr) ;
template<class T>
istream &operator>>(istream &s, array<T> &arr) ;
template<class T>
ifstream &operator>>(ifstream &s, array<T> &arr) ;
#endif
#endif // _ARRAY_DOT_HH_
|