00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _SEQUENCE_H
00027 #define _SEQUENCE_H
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 struct std::bidirectional_iterator_tag;
00060
00061 template <class Elem,
00062 bool elem_ptrfree = false>
00063 class Sequence {
00064 public:
00065 Sequence(int sizeHint = 5) throw ();
00066
00067
00068 Sequence(const Sequence<Elem, elem_ptrfree>& s) throw ();
00069
00070
00071 Sequence(const Elem a[], int n) throw ();
00072
00073
00074
00075 ~Sequence() throw ();
00076
00077
00078 Sequence<Elem, elem_ptrfree>& operator= (const Sequence<Elem, elem_ptrfree>& s)
00079 throw ();
00080
00081
00082 int size() const throw () { return sz; }
00083
00084
00085 void addhi(const Elem &x) throw ();
00086
00087
00088
00089 void addlo(const Elem &x) throw ();
00090
00091
00092
00093 Elem remhi() throw ();
00094
00095
00096
00097
00098 Elem remlo() throw ();
00099
00100
00101
00102
00103
00104 void put(int i, const Elem &x) throw ();
00105
00106
00107
00108
00109 Elem get(int i) const throw ();
00110
00111
00112
00113 Elem &get_ref(int i) throw ();
00114 const Elem &get_ref(int i) const throw ();
00115
00116
00117
00118 Elem gethi() const throw ();
00119
00120
00121
00122 Elem getlo() const throw ();
00123
00124
00125
00126 Sequence<Elem, elem_ptrfree> cat(const Sequence<Elem, elem_ptrfree> &s)
00127 const throw ();
00128
00129
00130
00131 Sequence<Elem, elem_ptrfree> sub(int start, int length = INT_MAX)
00132 const throw ();
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 class Iterator
00145 {
00146 public:
00147
00148 typedef Elem value_type;
00149 typedef Elem *pointer;
00150 typedef Elem &reference;
00151 typedef unsigned int size_type;
00152 typedef int difference_type;
00153 typedef std::bidirectional_iterator_tag iterator_category;
00154
00155 private:
00156 Sequence<Elem, elem_ptrfree> &seq;
00157 unsigned int index;
00158 public:
00159 Iterator(Sequence<Elem, elem_ptrfree> &s,
00160 unsigned int i) throw()
00161 : seq(s), index(i)
00162 { }
00163 Iterator(const Iterator &other) throw()
00164 : seq(other.seq), index(other.index)
00165 { }
00166
00167 bool operator==(const Iterator& other) const
00168 { return (&seq == &(other.seq)) && (index == other.index); }
00169 bool operator!=(const Iterator& other) const
00170 { return (&seq != &(other.seq)) || (index != other.index); }
00171 bool operator<(const Iterator& other) const
00172 { return (&seq == &(other.seq)) && (index < other.index); }
00173
00174 difference_type operator-(const Iterator& other) const
00175 {
00176 assert(&seq == &(other.seq));
00177 return index - other.index;
00178 }
00179 Iterator operator-(difference_type d)
00180 {
00181 Iterator r(*this);
00182 r.index -= d;
00183 return r;
00184 }
00185 Iterator operator+(difference_type d)
00186 {
00187 Iterator r(*this);
00188 r.index += d;
00189 return r;
00190 }
00191
00192 Iterator &operator=(const Iterator& other) throw()
00193 {
00194 assert(&seq == &(other.seq));
00195 index = other.index;
00196 return *this;
00197 }
00198 Iterator& operator+=(difference_type d)
00199 { index += d; return *this; }
00200 Iterator& operator-=(difference_type d)
00201 { index -= d; return *this; }
00202
00203 Elem &operator*() const throw()
00204 { return seq.get_ref(index); }
00205
00206 Iterator& operator++() throw()
00207 { index++; return *this; }
00208 Iterator operator++(int)
00209 { Iterator r = *this; ++*this; return r; }
00210 Iterator& operator--() throw()
00211 { index--; return *this; }
00212 Iterator operator--(int)
00213 { Iterator r = *this; --*this; return r; }
00214 };
00215
00216
00217
00218 Iterator begin() throw()
00219 { return Iterator(*this, 0); }
00220 Iterator end() throw()
00221 { return Iterator(*this, sz); }
00222
00223
00224
00225
00226
00227
00228
00229 private:
00230 int numElem;
00231 Elem* elem;
00232 int sz;
00233 int st;
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 static Elem elemZero;
00246
00247 void expand() throw ();
00248
00249 };
00250
00251
00252
00253 template <class Elem, bool elem_ptrfree>
00254 Elem Sequence<Elem, elem_ptrfree>::elemZero;
00255
00256 template <class Elem, bool elem_ptrfree>
00257 Sequence<Elem, elem_ptrfree>::Sequence(int sizeHint) throw ()
00258 : numElem(max(sizeHint, 1)), sz(0), st(0)
00259 {
00260 elem =(elem_ptrfree
00261 ? NEW_PTRFREE_ARRAY(Elem, numElem)
00262 : NEW_ARRAY(Elem, numElem));
00263 }
00264
00265 template <class Elem, bool elem_ptrfree>
00266 Sequence<Elem, elem_ptrfree>::Sequence(const Sequence<Elem, elem_ptrfree> &s)
00267 throw ()
00268 : numElem(s.numElem), sz(s.sz), st(0)
00269 {
00270 elem =(elem_ptrfree
00271 ? NEW_PTRFREE_ARRAY(Elem, s.numElem)
00272 : NEW_ARRAY(Elem, s.numElem));
00273 for (int i = 0, j = s.st; i < sz; i++, j++) {
00274 if (j == s.numElem) j = 0;
00275 elem[i] = s.elem[j];
00276 }
00277 }
00278
00279 template <class Elem, bool elem_ptrfree>
00280 Sequence<Elem, elem_ptrfree>::Sequence(const Elem a[], int n) throw ()
00281 : numElem(max(n, 1)), sz(n), st(0)
00282 {
00283 elem =(elem_ptrfree
00284 ? NEW_PTRFREE_ARRAY(Elem, numElem)
00285 : NEW_ARRAY(Elem, numElem));
00286 for (int i = 0; i < n; i++)
00287 elem[i] = a[i];
00288 }
00289
00290 template <class Elem, bool elem_ptrfree>
00291 Sequence<Elem, elem_ptrfree>::~Sequence() throw ()
00292 {
00293 delete[] elem;
00294 }
00295
00296 template <class Elem, bool elem_ptrfree>
00297 Sequence<Elem, elem_ptrfree>& Sequence<Elem, elem_ptrfree>::operator= (const Sequence<Elem, elem_ptrfree> &s)
00298 throw ()
00299 {
00300 delete[] elem;
00301 numElem = s.numElem;
00302 elem =(elem_ptrfree
00303 ? NEW_PTRFREE_ARRAY(Elem, numElem)
00304 : NEW_ARRAY(Elem, numElem));
00305 sz = s.sz;
00306 st = 0;
00307 for (int i = 0, j = s.st; i < sz; i++, j++) {
00308 if (j == s.numElem) j = 0;
00309 elem[i] = s.elem[j];
00310 }
00311 return *this;
00312 }
00313
00314 template <class Elem, bool elem_ptrfree>
00315 void Sequence<Elem, elem_ptrfree>::expand() throw ()
00316 {
00317 Elem* elemNew =(elem_ptrfree
00318 ? NEW_PTRFREE_ARRAY(Elem, 2*numElem)
00319 : NEW_ARRAY(Elem, 2*numElem));
00320 int m = numElem - st;
00321 int i,j;
00322 for (i = 0, j = st; i < m; i++, j++) elemNew[i] = elem[j];
00323 for (i = m, j = 0 ; i < numElem; i++, j++) elemNew[i] = elem[j];
00324
00325 numElem = 2 * numElem;
00326 delete[] elem;
00327 elem = elemNew;
00328 st = 0;
00329 }
00330
00331 template <class Elem, bool elem_ptrfree>
00332 void Sequence<Elem, elem_ptrfree>::addhi(const Elem &x) throw ()
00333 {
00334 if (sz == numElem) expand();
00335 int i = st + sz;
00336 if (numElem <= i) i -= numElem;
00337 elem[i] = x;
00338 sz++;
00339 }
00340
00341 template <class Elem, bool elem_ptrfree>
00342 void Sequence<Elem, elem_ptrfree>::addlo(const Elem &x) throw ()
00343 {
00344 if (sz == numElem) expand();
00345 int i = st;
00346 if (i == 0) i = numElem;
00347 i--;
00348 elem[i] = x;
00349 st = i;
00350 sz++;
00351 }
00352
00353 template <class Elem, bool elem_ptrfree>
00354 Elem Sequence<Elem, elem_ptrfree>::remhi() throw ()
00355 {
00356 assert(0 < sz);
00357 int i = st+sz-1;
00358 if (numElem <= i) i -= numElem;
00359 Elem res(elem[i]);
00360 elem[i] = elemZero;
00361 sz--;
00362 return res;
00363 }
00364
00365 template <class Elem, bool elem_ptrfree>
00366 Elem Sequence<Elem, elem_ptrfree>::remlo() throw ()
00367 {
00368 assert(0 < sz);
00369 Elem res(elem[st]);
00370 elem[st] = elemZero;
00371 st++; if (st == numElem) st = 0;
00372 sz--;
00373 return res;
00374 }
00375
00376 template <class Elem, bool elem_ptrfree>
00377 void Sequence<Elem, elem_ptrfree>::put(int i, const Elem &x) throw ()
00378 {
00379 assert((0 <= i) && (i < sz));
00380 int j = st + i;
00381 if (numElem <= j) j -= numElem;
00382 elem[j] = x;
00383 }
00384
00385 template <class Elem, bool elem_ptrfree>
00386 Elem Sequence<Elem, elem_ptrfree>::get(int i) const throw ()
00387 {
00388 assert((0 <= i) && (i < sz));
00389 int j = st + i;
00390 if (numElem <= j) j -= numElem;
00391 return elem[j];
00392 }
00393
00394 template <class Elem, bool elem_ptrfree>
00395 Elem &Sequence<Elem, elem_ptrfree>::get_ref(int i) throw ()
00396 {
00397 assert((0 <= i) && (i < sz));
00398 int j = st + i;
00399 if (numElem <= j) j -= numElem;
00400 return elem[j];
00401 }
00402
00403 template <class Elem, bool elem_ptrfree>
00404 const Elem &Sequence<Elem, elem_ptrfree>::get_ref(int i) const throw ()
00405 {
00406 assert((0 <= i) && (i < sz));
00407 int j = st + i;
00408 if (numElem <= j) j -= numElem;
00409 return elem[j];
00410 }
00411
00412 template <class Elem, bool elem_ptrfree>
00413 Elem Sequence<Elem, elem_ptrfree>::gethi() const throw ()
00414 {
00415 assert(0 < sz);
00416 int j = st + sz - 1;
00417 if (numElem <= j) j -= numElem;
00418 return elem[j];
00419 }
00420
00421 template <class Elem, bool elem_ptrfree>
00422 Elem Sequence<Elem, elem_ptrfree>::getlo() const throw ()
00423 {
00424 assert(0 < sz);
00425 return elem[st];
00426 }
00427
00428 template <class Elem, bool elem_ptrfree>
00429 Sequence<Elem, elem_ptrfree> Sequence<Elem, elem_ptrfree>::cat(const Sequence<Elem, elem_ptrfree> &s)
00430 const throw ()
00431 {
00432 int i;
00433 Sequence<Elem, elem_ptrfree> u(sz + s.sz);
00434 u.sz = sz + s.sz;
00435 for (i = 0; i < sz; i++) u.elem[i] = get(i);
00436 for (i = 0; i < s.sz; i++) u.elem[sz+i] = s.get(i);
00437 return u;
00438 }
00439
00440 template <class Elem, bool elem_ptrfree>
00441 Sequence<Elem, elem_ptrfree> Sequence<Elem, elem_ptrfree>::sub(int start,
00442 int length)
00443 const throw ()
00444 {
00445 int n = (sz <= start) ? 0 : min(length, sz - start);
00446 Sequence<Elem, elem_ptrfree> u(n);
00447 u.sz = n;
00448 for (int i = 0; i < n; i++)
00449 u.elem[i] = get(start + i);
00450 return u;
00451 }
00452
00453 #endif // _SEQUENCE_H