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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
#ifndef List_HPP_
#define List_HPP_
// Exception class
#include "Exception.h"
template< class T > class List;
template< class T > class FullList;
template< class T > class ListIterator
{
public:
ListIterator(const List< T > &list) :
head(list.head),
curr(list.curr) { }
T & operator()()const;
ListIterator & operator++();
ListIterator & operator--();
ListIterator & operator=(const ListIterator< T > &li);
int operator!() const;
int operator==(const ListIterator&) const;
int operator!=(const ListIterator&) const;
T & current() const;
int last() const;
int header() const;
T & previous() const;
int first() const;
int end();
T & next() const;
int beginning();
class Link
{
public:
Link *nxt;
Link *prev;
T element;
Link(Link *p = 0, Link *n = 0){
prev= p;
nxt = n;
if (prev)
prev->nxt = this;
if (nxt)
nxt->prev = this;
}
~Link(){
if(prev)
prev->nxt = nxt;
if(nxt)
nxt->prev = prev;
}
};
protected:
ListIterator(){ head = curr = 0; }
Link *head;
Link *curr;
friend class List< T >;
friend class FullList< T >;
};
template< class T > class List: public ListIterator< T >
{
public:
List();
List(const List &l);
~List(){
flush();
delete head;
}
List & operator = (const List &l);
List & operator = (const ListIterator< T >&li);
List operator + (const List &l) const;
List & operator +=(const List &l);
T & operator [](int index);
void insert(const T &el);
void insert(ListIterator< T > &li, const T &el);
int nElements() const;
void remove();
void remove(ListIterator< T > &li);
void flush();
protected:
int nels;
T **ptrArray;
};
template< class T > class FullList : public List< T >
{
public:
FullList():List< T >(){}
FullList(const List< T > &list):List< T >(list){}
FullList & operator = (const ListIterator< T > &li);
int search(const T &el);
int search(ListIterator< T >&, const T &el) const;
void write(ostream &out = cout) const;
void read(istream &in = cin);
friend ostream & operator<<(ostream &out, const FullList< T > &l);
friend istream & operator>>(istream &in, FullList< T > &l);
};
template< class T >
inline T& ListIterator< T >::operator()() const
{
if (curr == head)
throw Exception("No current element!");
return curr->element;
}
template< class T >
inline ListIterator< T >& ListIterator< T >::operator++()
{
curr = curr->nxt;
return *this;
}
template< class T >
inline ListIterator< T >& ListIterator< T >::operator--()
{
curr = curr->prev;
return *this;
}
template< class T >
ListIterator< T >& ListIterator< T >::operator =(const ListIterator< T > &li)
{
if (head != li.head)
throw Exception("Invalid iterators!");
curr = li.curr;
return *this;
}
template< class T >
inline int ListIterator< T >:: operator !() const
{
return curr != head;
}
template< class T >
int ListIterator< T >::operator ==(const ListIterator< T > &li) const
{
return (head == li.head && curr == li.curr);
}
template< class T >
int ListIterator< T >::operator !=(const ListIterator< T > &li) const
{
return (head != li.head || curr != li.curr);
}
template< class T >
inline T& ListIterator< T >::current() const
{
if(curr == head)
throw Exception("No current element!");
return curr->element;
}
template< class T >
inline int ListIterator< T >::last() const
{
return (curr != head && curr == head->prev);
}
template< class T >
inline int ListIterator< T >::header() const
{
return curr == head;
}
template< class T >
inline T& ListIterator< T >::previous() const
{
if (curr->prev == head)
throw Exception("No previous element!");
return curr->prev->element;
}
template< class T >
inline int ListIterator< T >::first() const
{
return (curr != head && curr == head->nxt);
}
template< class T >
inline int ListIterator< T >::end()
{
curr = head->prev;
return curr != head;
}
template< class T >
inline T& ListIterator< T >::next() const
{
if (curr->nxt == head)
throw Exception("No next element");
return curr->nxt->element;
}
template< class T >
inline int ListIterator< T >::beginning()
{
curr = head->nxt;
return curr != head;
}
template< class T >
List< T >::List() : ListIterator< T >(*this)
{
head = curr = new Link;
curr->nxt = curr->prev = head;
ptrArray = 0;
nels = 0;
}
template< class T >
List< T >::List(const List< T > &l) : ListIterator< T >(*this)
{
head = curr = new Link;
curr->nxt = curr->prev = head;
ptrArray = 0;
nels = 0;
operator =(l);
}
template< class T >
List< T > & List< T >::operator =(const List< T > &l)
{
if (&l == this)
return *this;
flush();
ListIterator< T > tmp(l);
for (tmp.beginning(); !tmp; ++tmp)
insert(tmp() );
return *this;
}
template< class T >
List< T > & List< T >::operator=(const ListIterator< T > &li)
{
ListIterator< T >::operator =(li);
return *this;
}
template< class T >
List< T > List< T >::operator +(const List< T > &right) const
{
List< T > res = (*this);
res.end();
ListIterator< T > tmp(right);
for(tmp.beginning(); !tmp; ++tmp)
res.insert(tmp() );
return res;
}
template< class T >
List< T > & List< T >::operator +=(const List< T > &right)
{
end();
ListIterator< T > tmp(right);
for(tmp.beginning(); !tmp; ++tmp)
insert(tmp() );
return *this;
}
template< class T >
inline T & List< T >::operator[](int index)
{
if (!ptrArray){
ptrArray = new T*[nels];
if (! ptrArray)
throw Exception("Out of memory!");
ListIterator< T > tmp (*this);
int i = 0;
for (tmp.beginning(); !tmp; ++tmp)
ptrArray[ i++ ] = &tmp();
}
if (index < 0 || index >= nels)
throw Exception("Invalid array index");
return *ptrArray[ index ];
}
template< class T >
void List< T >::insert(const T &el)
{
if(ptrArray){
delete ptrArray;
ptrArray = 0;
}
curr = new Link(curr, curr->nxt);
if (curr == 0)
throw Exception("Out of memory!");
curr->element = el;
nels++;
}
template< class T >
void List< T >::insert(ListIterator< T > &li, const T &el)
{
if (head != li.head)
throw Exception("Incompatible iterators!");
if (ptrArray){
delete ptrArray;
ptrArray = 0;
}
li.curr = new Link(li.curr, li.curr->nxt);
if (li.curr == 0)
throw Exception("Out of memory!");
li.curr->element = el;
nels++;
}
template< class T >
inline int List< T >::nElements() const
{
return nels;
}
template< class T >
void List< T >::remove()
{
if (ptrArray){
delete ptrArray;
ptrArray = 0;
}
if (curr == head)
throw Exception("No current element!");
curr = curr->prev;
Link *tmp = curr->nxt;
delete tmp;
nels--;
}
template< class T >
void List< T >::remove(ListIterator< T > &li)
{
if( head != li.head)
throw Exception("Incompatible iterators");
if (ptrArray){
delete ptrArray;
ptrArray = 0;
}
if (li.curr == head)
throw Exception("No current element!");
if (curr = li.curr)
curr = li.curr->prev;
li.curr = li.curr->prev;
Link *tmp = li.curr->nxt;
delete tmp;
nels--;
}
template< class T >
void List< T >::flush()
{
if (ptrArray){
delete ptrArray;
ptrArray = 0;
}
curr = head;
while (curr->nxt != head){
Link *tmp = curr->nxt;
delete tmp;
}
nels = 0;
}
template< class T >
FullList< T > & FullList< T >::operator =(const ListIterator< T > &li)
{
ListIterator< T >::operator =(li);
return *this;
}
template< class T >
int FullList< T >::search(const T &el)
{
for( ; curr != head; curr = curr->nxt)
if (el == curr->element)
break;
return curr != head;
}
template< class T >
int FullList< T >::search(ListIterator< T > &li, const T &el) const
{
if (head != li.head)
throw Exception("Incompatible iterators!");
for ( ; !li; ++li)
if (el == li() )
break;
return !li;
}
template< class T >
void FullList< T >::write(ostream &out) const
{
out << '(' << nels << ')' << endl;
ListIterator< T > li(*this);
for (li.beginning(); !li; ++li)
out << li() << endl;
out << endl;
}
template< class T >
void FullList< T >::read(istream &in)
{
int n;
char c1 = ' ', c2 = ' ';
in >> c1 >> n >> c2;
if (c1 != '(' || c2 != ')' || n < 0)
throw Exception("Invalid format!");
flush();
T tmp;
for (int i = 0; i < n; i++){
if ( !(in >> tmp) )
throw Exception("Invalid format!");
insert(tmp);
}
}
template< class T >
ostream & operator <<(ostream &out, const FullList< T > &fl)
{
fl.write(out);
return out;
}
template< class T >
istream & operator >>(istream &in, FullList< T > &fl)
{
fl.read(in);
return in;
}
#endif |