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
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OFLIST_H
00036 #define OFLIST_H
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "dcmtk/config/osconfig.h"
00054 #include "dcmtk/ofstd/oftypes.h"
00055 #include "dcmtk/ofstd/ofcast.h"
00056
00057 #ifndef HAVE_CLASS_TEMPLATE
00058 #error Your C++ compiler cannot handle class templates:
00059 #endif
00060
00061 #if defined(HAVE_STL) || defined(HAVE_STL_LIST)
00062
00063
00064
00065
00066
00067 #include <list>
00068
00069 #ifdef HAVE_STD_NAMESPACE
00070 #define OFList std::list
00071 #define OFListIterator(x) std::list< x >::iterator
00072 #define OFListConstIterator(x) std::list< x >::const_iterator
00073 #else
00074 #define OFList list
00075 #define OFListIterator(x) list< x >::iterator
00076 #define OFListConstIterator(x) list< x >::const_iterator
00077 #endif
00078
00079 #define OFListInsert(InputIterator, T, c, pos, first, last) (c).insert((pos), (first), (last))
00080 #define OFListRemoveIf(Predicate, T, c, pred) (c).remove_if((pred))
00081
00082
00083 #if defined(HAVE_TYPENAME)
00084 #define OFLIST_TYPENAME typename
00085 #else
00086 #define OFLIST_TYPENAME
00087 #endif
00088
00089 #else
00090
00091 #define INCLUDE_CASSERT
00092 #define INCLUDE_CSTDDEF
00093 #include "dcmtk/ofstd/ofstdinc.h"
00094
00095 #define OFLIST_TYPENAME
00096
00097
00098
00099
00100
00101
00102
00103
00104 struct OFListLinkBase
00105 {
00106 OFListLinkBase * next;
00107 OFListLinkBase * prev;
00108 OFBool dummy;
00109 OFListLinkBase(): next(NULL), prev(NULL), dummy(OFFalse) { }
00110 virtual ~OFListLinkBase() {}
00111
00112 private:
00113 OFListLinkBase(const OFListLinkBase&);
00114 OFListLinkBase& operator=(const OFListLinkBase&);
00115 };
00116
00117
00118
00119
00120
00121 class OFListBase
00122 {
00123 protected:
00124 OFListLinkBase * afterLast;
00125 size_t listSize;
00126 void base_recalcListSize();
00127 public:
00128 OFListBase();
00129 virtual ~OFListBase();
00130 OFListLinkBase * base_begin() const { return afterLast->next; }
00131 OFListLinkBase * base_end() const { return afterLast; }
00132 OFBool base_empty() const { return afterLast == afterLast->next; }
00133 size_t base_size() const { return listSize; }
00134 OFListLinkBase * base_insert(OFListLinkBase * pos, OFListLinkBase * newElem);
00135 OFListLinkBase * base_erase(OFListLinkBase * pos);
00136 void base_splice(OFListLinkBase * pos,
00137 OFListLinkBase * begin, OFListLinkBase * end);
00138 void base_clear();
00139
00140 private:
00141 OFListBase(const OFListBase&);
00142 OFListBase& operator=(const OFListBase&);
00143 };
00144
00145
00146
00147
00148
00149
00150 template <class T>
00151 struct OFListLink : public OFListLinkBase
00152 {
00153 T info;
00154 OFListLink(const T& i) : OFListLinkBase(), info(i) { }
00155 virtual ~OFListLink() {}
00156 private:
00157 OFListLink(const OFListLink<T>&);
00158 OFListLink<T>& operator=(const OFListLink<T>&);
00159 };
00160
00161
00162
00163
00164 template <class T> class OFList;
00165
00166
00173 template <class T>
00174 class OFIterator
00175 {
00176 friend class OFList<T>;
00177 protected:
00178
00180 OFListLinkBase * node;
00181
00185 OFIterator(OFListLinkBase * x) : node(x) { }
00186 public:
00187
00192 OFIterator() : node(NULL) { }
00193
00196 OFIterator(const OFIterator<T>& x) : node(x.node) { };
00197
00200 OFIterator<T>& operator=(const OFIterator<T>& x)
00201 {
00202 node = x.node;
00203 return *this;
00204 }
00205
00211 OFBool operator==(const OFIterator<T>& x) const { return node == x.node; }
00212
00218 OFBool operator!=(const OFIterator<T>& x) const { return node != x.node; }
00219
00224 T& operator*() const
00225 {
00226 assert(!node->dummy);
00227 return (OFstatic_cast(OFListLink<T> *,node))->info;
00228 }
00229
00235 OFIterator<T>& operator++()
00236 {
00237 node = node->next;
00238 return *this;
00239 }
00240
00247 OFIterator<T> operator++(int)
00248 {
00249 OFIterator<T> tmp(*this);
00250 node = node->next;
00251 return tmp;
00252 }
00253
00259 OFIterator<T>& operator--()
00260 {
00261 node = node->prev;
00262 return *this;
00263 }
00264
00271 OFIterator<T> operator--(int)
00272 {
00273 OFIterator<T> tmp(*this);
00274 node = node->prev;
00275 return tmp;
00276 }
00277 };
00278
00279
00283 template <class T>
00284 class OFList : private OFListBase
00285 {
00286 public:
00292 OFIterator<T> insert(OFIterator<T> position, const T& x)
00293 {
00294 return OFIterator<T>(OFListBase::base_insert(position.node, new OFListLink<T>(x)));
00295 }
00296
00297 private:
00298
00302 void copy(const OFList<T>& oldList)
00303 {
00304 OFIterator<T> vfirst(oldList.begin());
00305 OFIterator<T> vend(oldList.end());
00306 OFIterator<T> vpos(this->end());
00307 while (vfirst != vend)
00308 {
00309 insert(vpos, *vfirst);
00310 ++vfirst;
00311 }
00312 }
00313
00317 void recalcListSize() { OFListBase::base_recalcListSize(); }
00318
00319 public:
00320
00323 OFList() : OFListBase() { }
00324
00327 OFList(const OFList<T>& oldList):OFListBase()
00328 {
00329 copy(oldList);
00330 }
00331
00336 OFIterator<T> begin() const { return OFIterator<T>(OFListBase::base_begin()); }
00337
00342 OFIterator<T> end() const { return OFIterator<T>(OFListBase::base_end()); }
00343
00347 OFBool empty() const { return OFListBase::base_empty(); }
00348
00352 size_t size() const { return OFListBase::base_size(); }
00353
00358 T& front() { return *begin(); }
00359
00364 T& back() { return *(--end()); }
00365
00369 void push_front(const T& x) { insert(begin(), OFconst_cast(T&, x)); }
00370
00371
00376 void pop_front() { erase(begin()); }
00377
00381 void push_back(const T& x) { insert(end(), OFconst_cast(T&, x)); }
00382
00383
00388 void pop_back() { erase(--end()); }
00389
00395 void insert(OFIterator<T> position, size_t n, const T& x)
00396 {
00397 while(n--) OFListBase::base_insert(position.node, new OFListLink<T>(x));
00398 }
00399
00404 OFIterator<T> erase(OFIterator<T> position)
00405 {
00406 return OFIterator<T>(OFListBase::base_erase(position.node));
00407 }
00408
00415 OFIterator<T> erase(OFIterator<T> position, OFIterator<T> last)
00416 {
00417 while (position != last) position = erase(position);
00418 return last;
00419 }
00420
00424 void clear() { OFListBase::base_clear(); }
00425
00431 void splice(OFIterator<T> position, OFList<T>& x)
00432 {
00433 splice(position, x, x.begin(), x.end());
00434 }
00435
00441 void splice(OFIterator<T> position, OFList<T>& x, OFIterator<T> i)
00442 {
00443 OFIterator<T> change(i);
00444 ++i;
00445 splice(position, x, change, i);
00446 }
00447
00455 void splice(OFIterator<T> position, OFList<T>& x,
00456 OFIterator<T> first, OFIterator<T> last)
00457 {
00458 OFListBase::base_splice(position.node, first.node, last.node);
00459 x.recalcListSize();
00460 }
00461
00466 void remove(const T& value)
00467 {
00468 OFIterator<T> first = begin();
00469 OFIterator<T> last = end();
00470 while(first != last)
00471 {
00472 if (*first == value) first = erase(first);
00473 else ++first;
00474 }
00475 }
00476
00477 private:
00478
00481 OFList<T>& operator=(const OFList<T>& arg);
00482 };
00483
00484
00485 #ifdef HAVE_FUNCTION_TEMPLATE
00486
00487 #define OFListInsert(InputIterator, T, c, pos, first, last) OF_ListInsert((c), (pos), (first), (last))
00488
00489 #define OFListRemoveIf(Predicate, T, c, pred) OF_ListRemoveIf((c), (pred))
00490
00491 #elif defined(HAVE_STATIC_TEMPLATE_METHOD)
00492
00493 #define OFListInsert(InputIterator, T, c, pos, first, last) OF_ListInsertClass<InputIterator, T>::OF_ListInsert((c), (pos), (first), (last))
00494
00495 #define OFListRemoveIf(Predicate, T, c, pred) OF_ListRemoveIfClass<Predicate, T>::OF_ListRemoveIf((c), (pred))
00496
00497 #else
00498 #error Your C++ Compiler is not capable of compiling this code
00499 #endif
00500
00501
00502 template <class InputIterator, class T>
00503 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
00504 class OF_ListInsertClass
00505 {
00506 public:
00507 static
00508 #endif
00509 void OF_ListInsert(OFList<T>& c, OFIterator<T> position,
00510 InputIterator first, InputIterator last)
00511 {
00512 while(first != last)
00513 {
00514 c.insert(position, *first);
00515 ++first;
00516 }
00517 }
00518 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
00519 };
00520 #endif
00521
00522
00523
00524 template <class Predicate, class T>
00525 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
00526 class OF_ListRemoveIfClass
00527 {
00528 public:
00529 static
00530 #endif
00531 void OF_ListRemoveIf(OFList<T>& c, Predicate pred)
00532 {
00533 OFIterator<T> first = c.begin();
00534 OFIterator<T> last = c.end();
00535 while (first != last)
00536 {
00537 if (pred(*first))
00538 first = c.erase(first);
00539 else
00540 ++first;
00541 }
00542 }
00543
00544 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
00545 };
00546 #endif
00547
00548 #define OFListIterator(x) OFIterator< x >
00549 #define OFListConstIterator(x) OFIterator< x >
00550
00551 #endif
00552
00553 #endif
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641