Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:56

0001 /****************************************************************************
0002 **
0003 ** Copyright (c) 2008-2020 C.B. Barber. All rights reserved.
0004 ** $Id: //main/2019/qhull/src/libqhullcpp/QhullLinkedList.h#6 $$Change: 3009 $
0005 ** $DateTime: 2020/07/30 19:25:22 $$Author: bbarber $
0006 **
0007 ****************************************************************************/
0008 
0009 #ifndef QHULLLINKEDLIST_H
0010 #define QHULLLINKEDLIST_H
0011 
0012 #include "libqhull_r/qhull_ra.h"
0013 #include "libqhullcpp/QhullError.h"
0014 
0015 #include <cstddef>  // ptrdiff_t, size_t
0016 
0017 #ifdef QHULL_USES_QT
0018 #include <QtCore/QList>
0019 #endif
0020 
0021 #ifndef QHULL_NO_STL
0022 #include <algorithm>
0023 #include <iterator>
0024 #include <vector>
0025 #endif
0026 
0027 namespace orgQhull {
0028 
0029 #//!\name Defined here
0030     //! QhullLinkedList<T> -- A linked list modeled on QLinkedList.
0031     //!   T is an opaque type with T(B *b), b=t.getBaseT(), t=t.next(), and t=t.prev().  The end node is a sentinel.
0032     //!   QhullQh/qhT owns the contents.
0033     //!   QhullLinkedList does not define erase(), clear(), removeFirst(), removeLast(), pop_back(), pop_front(), fromStdList()
0034     //!   Derived from Qt/core/tools/qlinkedlist.h and libqhull_r.h/FORALLfacets_()
0035     //! QhullLinkedList<T>::const_iterator -- STL-style iterator
0036     //! QhullLinkedList<T>::iterator -- STL-style iterator
0037     //! QhullLinkedListIterator<T> -- Java-style iterator
0038     //!   Derived from Qt/core/tools/qiterator.h
0039     //!   Works with Qt's foreach keyword [Qt/src/corelib/global/qglobal.h]
0040 
0041 template <typename T>
0042 class QhullLinkedList
0043 {
0044 #//!\name Defined here
0045 public:
0046     class const_iterator;
0047     class iterator;
0048     typedef const_iterator  ConstIterator;
0049     typedef iterator    Iterator;
0050     typedef ptrdiff_t   difference_type;
0051     typedef countT      size_type;
0052     typedef T           value_type;
0053     typedef const value_type *const_pointer;
0054     typedef const value_type &const_reference;
0055     typedef value_type *pointer;
0056     typedef value_type &reference;
0057 
0058 #//!\name Fields
0059 private:
0060     T                   begin_node;
0061     T                   end_node;     //! Sentinel node at end of list
0062 
0063 #//!\name Constructors
0064 public:
0065                         QhullLinkedList<T>(T b, T e) : begin_node(b), end_node(e) {}
0066                         //! Copy constructor copies begin_node and end_node, but not the list elements.  Needed for return by value and parameter passing.
0067                         QhullLinkedList<T>(const QhullLinkedList<T> &other) : begin_node(other.begin_node), end_node(other.end_node) {}
0068                         //! Copy assignment copies begin_node and end_node, but not the list elements.
0069                         QhullLinkedList<T> & operator=(const QhullLinkedList<T> &other) { begin_node= other.begin_node; end_node= other.end_node; return *this; }
0070                         ~QhullLinkedList<T>() {}
0071 
0072 private:
0073                         //!disabled since a sentinel must be allocated as the private type
0074                         QhullLinkedList<T>() {}
0075 
0076 public:
0077 
0078 #//!\name Conversions
0079 #ifndef QHULL_NO_STL
0080     std::vector<T>      toStdVector() const;
0081 #endif
0082 #ifdef QHULL_USES_QT
0083     QList<T>            toQList() const;
0084 #endif
0085 
0086 #//!\name GetSet
0087     countT              count() const;
0088                         //count(t) under #//!\name Search
0089     bool                isEmpty() const { return (begin_node==end_node); }
0090     bool                operator==(const QhullLinkedList<T> &o) const;
0091     bool                operator!=(const QhullLinkedList<T> &o) const { return !operator==(o); }
0092     size_t              size() const { return count(); }
0093 
0094 #//!\name Element access
0095     //! For back() and last(), return T instead of T& (T is computed)
0096     const T             back() const { return last(); }
0097     T                   back() { return last(); }
0098     const T &           first() const { QHULL_ASSERT(!isEmpty()); return begin_node; }
0099     T &                 first() { QHULL_ASSERT(!isEmpty()); return begin_node; }
0100     const T &           front() const { return first(); }
0101     T &                 front() { return first(); }
0102     const T             last() const { QHULL_ASSERT(!isEmpty()); return *--end(); }
0103     T                   last() { QHULL_ASSERT(!isEmpty()); return *--end(); }
0104 
0105 #//!\name Modify -- Allocation of opaque types not implemented.
0106 
0107 #//!\name Search
0108     bool                contains(const T &t) const;
0109     countT              count(const T &t) const;
0110 
0111 #//!\name Iterator
0112     iterator            begin() { return begin_node; }
0113     const_iterator      begin() const { return begin_node; }
0114     const_iterator      constBegin() const { return begin_node; }
0115     const_iterator      constEnd() const { return end_node; }
0116     iterator            end() { return end_node; }
0117     const_iterator      end() const { return end_node; }
0118 
0119     class iterator {
0120 
0121     private:
0122         T               i;
0123         friend class const_iterator;
0124 
0125     public:
0126         typedef std::bidirectional_iterator_tag  iterator_category;
0127         typedef T           value_type;
0128         typedef value_type *pointer;
0129         typedef value_type &reference;
0130         typedef ptrdiff_t   difference_type;
0131 
0132                         iterator() : i() {}
0133                         iterator(const T &t) : i(t) {}  //!< Automatic conversion to iterator
0134                         iterator(const iterator &o) : i(o.i) {}
0135         iterator &      operator=(const iterator &o) { i= o.i; return *this; }
0136 
0137         const T &       operator*() const { return i; }
0138         T &             operator*() { return i; }
0139         // Do not define operator[]
0140         const T *       operator->() const { return &i; }
0141         T *             operator->() { return &i; }
0142         bool            operator==(const iterator &o) const { return i == o.i; }
0143         bool            operator!=(const iterator &o) const { return !operator==(o); }
0144         bool            operator==(const const_iterator &o) const { return i==reinterpret_cast<const iterator &>(o).i; }
0145         bool            operator!=(const const_iterator &o) const { return !operator==(o); }
0146         iterator &      operator++() { i= i.next(); return *this; }
0147         iterator        operator++(int) { iterator o= i; i= i.next(); return o; }
0148         iterator &      operator--() { i= i.previous(); return *this; }
0149         iterator        operator--(int) { iterator o= i; i= i.previous(); return o; }
0150         iterator        operator+(int j) const;
0151         iterator        operator-(int j) const { return operator+(-j); }
0152         iterator &      operator+=(int j) { return (*this= *this + j); }
0153         iterator &      operator-=(int j) { return (*this= *this - j); }
0154     };//QhullLinkedList::iterator
0155 
0156     class const_iterator {
0157 
0158     private:
0159         T               i;
0160 
0161     public:
0162         typedef std::bidirectional_iterator_tag  iterator_category;
0163         typedef T                 value_type;
0164         typedef const value_type *pointer;
0165         typedef const value_type &reference;
0166         typedef ptrdiff_t         difference_type;
0167 
0168                         const_iterator() : i() {}
0169                         const_iterator(const T &t) : i(t) {}  //!< Automatic conversion to const_iterator
0170                         const_iterator(const iterator &o) : i(o.i) {}
0171                         const_iterator(const const_iterator &o) : i(o.i) {}
0172         const_iterator &operator=(const const_iterator &o) { i= o.i; return *this; }
0173 
0174         const T &       operator*() const { return i; }
0175         const T *       operator->() const { return &i; }
0176         bool            operator==(const const_iterator &o) const { return i == o.i; }
0177         bool            operator!=(const const_iterator &o) const { return !operator==(o); }
0178                         // No comparisons or iterator diff
0179         const_iterator &operator++() { i= i.next(); return *this; }
0180         const_iterator  operator++(int) { const_iterator o= i; i= i.next(); return o; }
0181         const_iterator &operator--() { i= i.previous(); return *this; }
0182         const_iterator  operator--(int) { const_iterator o= i; i= i.previous(); return o; }
0183         const_iterator  operator+(int j) const;
0184         const_iterator  operator-(int j) const { return operator+(-j); }
0185         const_iterator &operator+=(int j) { return (*this= *this + j); }
0186         const_iterator &operator-=(int j) { return (*this= *this - j); }
0187     };//QhullLinkedList::const_iterator
0188 
0189 };//QhullLinkedList
0190 
0191 template <typename T>
0192 class QhullLinkedListIterator // QH11016 FIX: define QhullMutableLinkedListIterator
0193 {
0194     typedef typename QhullLinkedList<T>::const_iterator const_iterator;
0195     QhullLinkedList<T>  c;
0196     const_iterator      i;
0197 
0198 public:
0199                         QhullLinkedListIterator(const QhullLinkedList<T> &container) : c(container), i(c.constBegin()) {}
0200     QhullLinkedListIterator & operator=(const QhullLinkedList<T> &container) { c= container; i= c.constBegin(); return *this; }
0201     bool                findNext(const T &t);
0202     bool                findPrevious(const T &t);
0203     bool                hasNext() const { return i != c.constEnd(); }
0204     bool                hasPrevious() const { return i != c.constBegin(); }
0205     T                   next() { return *i++; }
0206     T                   peekNext() const { return *i; }
0207     T                   peekPrevious() const { const_iterator p= i; return *--p; }
0208     T                   previous() { return *--i; }
0209     void                toFront() { i= c.constBegin(); }
0210     void                toBack() { i= c.constEnd(); }
0211 };//QhullLinkedListIterator
0212 
0213 #//!\name == Definitions =========================================
0214 
0215 #//!\name Conversion
0216 
0217 #ifndef QHULL_NO_STL
0218 template <typename T>
0219 std::vector<T> QhullLinkedList<T>::
0220 toStdVector() const
0221 {
0222     std::vector<T> tmp;
0223     std::copy(constBegin(), constEnd(), std::back_inserter(tmp));
0224     return tmp;
0225 }//toStdVector
0226 #endif
0227 
0228 #ifdef QHULL_USES_QT
0229 template <typename T>
0230 QList<T>  QhullLinkedList<T>::
0231 toQList() const
0232 {
0233     QhullLinkedListIterator<T> i(*this);
0234     QList<T> ls;
0235     while(i.hasNext()){
0236         ls.append(i.next());
0237     }
0238     return ls;
0239 }//toQList
0240 #endif
0241 
0242 #//!\name GetSet
0243 
0244 template <typename T>
0245 countT QhullLinkedList<T>::
0246 count() const
0247 {
0248     const_iterator i= begin_node;
0249     countT c= 0;
0250     while(i != end_node){
0251         c++;
0252         i++;
0253     }
0254     return c;
0255 }//count
0256 
0257 #//!\name Search
0258 
0259 template <typename T>
0260 bool QhullLinkedList<T>::
0261 contains(const T &t) const
0262 {
0263     const_iterator i= begin_node;
0264     while(i != end_node){
0265         if(i==t){
0266             return true;
0267         }
0268         i++;
0269     }
0270     return false;
0271 }//contains
0272 
0273 template <typename T>
0274 countT QhullLinkedList<T>::
0275 count(const T &t) const
0276 {
0277     const_iterator i= begin_node;
0278     countT c= 0;
0279     while(i != end_node){
0280         if(i==t){
0281             c++;
0282         }
0283         i++;
0284     }
0285     return c;
0286 }//count
0287 
0288 template <typename T>
0289 bool QhullLinkedList<T>::
0290 operator==(const QhullLinkedList<T> &l) const
0291 {
0292     if(begin_node==l.begin_node){
0293         return (end_node==l.end_node);
0294     }
0295     T i= begin_node;
0296     T il= l.begin_node;
0297     while(i != end_node){
0298         if(i != il){
0299             return false;
0300         }
0301         i= static_cast<T>(i.next());
0302         il= static_cast<T>(il.next());
0303     }
0304     if(il != l.end_node){
0305         return false;
0306     }
0307     return true;
0308 }//operator==
0309 
0310 #//!\name Iterator
0311 
0312 template <typename T>
0313 typename QhullLinkedList<T>::iterator  QhullLinkedList<T>::iterator::
0314 operator+(int j) const
0315 {
0316     T n= i;
0317     if(j>0){
0318         while(j--){
0319             n= n.next();
0320         }
0321     }else{
0322         while(j++){
0323             n= n.previous();
0324         }
0325     }
0326     return iterator(n);
0327 }//operator+
0328 
0329 template <typename T>
0330 typename QhullLinkedList<T>::const_iterator  QhullLinkedList<T>::const_iterator::
0331 operator+(int j) const
0332 {
0333     T n= i;
0334     if(j>0){
0335         while(j--){
0336             n= n.next();
0337         }
0338     }else{
0339         while(j++){
0340             n= n.previous();
0341         }
0342     }
0343     return const_iterator(n);
0344 }//operator+
0345 
0346 #//!\name QhullLinkedListIterator
0347 
0348 template <typename T>
0349 bool QhullLinkedListIterator<T>::
0350 findNext(const T &t)
0351 {
0352     while(i != c->constEnd()){
0353         if(*i++ == t){
0354             return true;
0355         }
0356     }
0357     return false;
0358 }//findNext
0359 
0360 template <typename T>
0361 bool QhullLinkedListIterator<T>::
0362 findPrevious(const T &t)
0363 {
0364     while(i!=c->constBegin()){
0365         if(*(--i)==t){
0366             return true;
0367         }
0368     }
0369     return false;
0370 }//findNext
0371 
0372 }//namespace orgQhull
0373 
0374 #//!\name Global
0375 
0376 template <typename T>
0377 std::ostream &
0378 operator<<(std::ostream &os, const orgQhull::QhullLinkedList<T> &qs)
0379 {
0380     typename orgQhull::QhullLinkedList<T>::const_iterator i;
0381     for(i= qs.begin(); i != qs.end(); ++i){
0382         os << *i;
0383     }
0384     return os;
0385 }//operator<<
0386 
0387 #endif // QHULLLINKEDLIST_H
0388