Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:19:28

0001 /****************************************************************************
0002 **
0003 ** Copyright (c) 2008-2020 C.B. Barber. All rights reserved.
0004 ** $Id: //main/2019/qhull/src/libqhullcpp/QhullIterator.h#4 $$Change: 3001 $
0005 ** $DateTime: 2020/07/24 20:43:28 $$Author: bbarber $
0006 **
0007 ****************************************************************************/
0008 
0009 #ifndef QHULLITERATOR_H
0010 #define QHULLITERATOR_H
0011 
0012 #include "libqhull_r/qhull_ra.h"
0013 
0014 #include <assert.h>
0015 #include <iterator>
0016 #include <string>
0017 #include <vector>
0018 
0019 namespace orgQhull {
0020 
0021 #//!\name Defined here
0022     //! Only QHULL_DECLARE_SEQUENTIAL_ITERATOR is used in libqhullcpp.  The others need further development
0023     //! QHULL_DECLARE_SEQUENTIAL_ITERATOR(C) -- Declare a Java-style iterator
0024     //! QHULL_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) -- Declare a mutable Java-style iterator
0025     //! QHULL_DECLARE_SET_ITERATOR(C) -- Declare a set iterator
0026     //! QHULL_DECLARE_MUTABLE_SET_ITERATOR(C) -- Declare a mutable set iterator
0027     //! Derived from Qt/core/tools/qiterator.h and qset_r.h/FOREACHsetelement_()
0028 
0029 //! Stores C as done in Qt's Q_DECLARE_SEQUENTIAL_ITERATOR.  Allows use with temporaries.
0030 //! Do not use for containers with deep-copy semantics (e.g., STL::vector)
0031 // C::const_iterator is an STL-style iterator that returns T&
0032 #define QHULL_DECLARE_SEQUENTIAL_ITERATOR(C, T) \
0033     \
0034     class C##Iterator \
0035     { \
0036         typedef C::const_iterator const_iterator; \
0037         C c; \
0038         const_iterator i; \
0039         public: \
0040         inline C##Iterator(const C &container) \
0041         : c(container), i(c.constBegin()) {} \
0042         inline C##Iterator &operator=(const C &container) \
0043         { c= container; i= c.constBegin(); return *this; } \
0044         inline void toFront() { i= c.constBegin(); } \
0045         inline void toBack() { i= c.constEnd(); } \
0046         inline bool hasNext() const { return i != c.constEnd(); } \
0047         inline const T &next() { return *i++; } \
0048         inline const T &peekNext() const { return *i; } \
0049         inline bool hasPrevious() const { return i != c.constBegin(); } \
0050         inline const T &previous() { return *--i; } \
0051         inline const T &peekPrevious() const { const_iterator p= i; return *--p; } \
0052         inline bool findNext(const T &t) \
0053         { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
0054         inline bool findPrevious(const T &t) \
0055         { while (i != c.constBegin()) if (*(--i) == t) return true; \
0056         return false;  } \
0057     };//C##Iterator
0058 
0059 // Remove setShareable() from Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR
0060 // Uses QHULL_ASSERT (assert.h)
0061 // Duplicated in MutablePointIterator without insert or remove
0062 // Not used in libqhullcpp.  See Coordinates.h
0063 #define QHULL_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C, T) \
0064     class Mutable##C##Iterator \
0065     { \
0066         typedef C::iterator iterator; \
0067         typedef C::const_iterator const_iterator; \
0068         C *c; \
0069         iterator i, n; \
0070         inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
0071         public: \
0072         inline Mutable##C##Iterator(C &container) \
0073         : c(&container) \
0074         { i= c->begin(); n= c->end(); } \
0075         inline ~Mutable##C##Iterator() \
0076         {} \
0077         inline Mutable##C##Iterator &operator=(C &container) \
0078         { c= &container; \
0079         i= c->begin(); n= c->end(); return *this; } \
0080         inline void toFront() { i= c->begin(); n= c->end(); } \
0081         inline void toBack() { i= c->end(); n= i; } \
0082         inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \
0083         inline T &next() { n= i++; return *n; } \
0084         inline T &peekNext() const { return *i; } \
0085         inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \
0086         inline T &previous() { n= --i; return *n; } \
0087         inline T &peekPrevious() const { iterator p= i; return *--p; } \
0088         inline void remove() \
0089         { if (c->constEnd() != const_iterator(n)) { i= c->erase(n); n= c->end(); } } \
0090         inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n= t; } \
0091         inline T &value() { QHULL_ASSERT(item_exists()); return *n; } \
0092         inline const T &value() const { QHULL_ASSERT(item_exists()); return *n; } \
0093         inline void insert(const T &t) { n= i= c->insert(i, t); ++i; } \
0094         inline bool findNext(const T &t) \
0095         { while (c->constEnd() != const_iterator(n= i)) if (*i++ == t) return true; return false; } \
0096         inline bool findPrevious(const T &t) \
0097         { while (c->constBegin() != const_iterator(i)) if (*(n= --i) == t) return true; \
0098         n= c->end(); return false;  } \
0099     };//Mutable##C##Iterator
0100 
0101 // Not used in libqhullcpp.
0102 #define QHULL_DECLARE_SET_ITERATOR(C) \
0103 \
0104     template <class T> \
0105     class Qhull##C##Iterator \
0106     { \
0107         typedef typename Qhull##C<T>::const_iterator const_iterator; \
0108         Qhull##C<T> c; \
0109         const_iterator i; \
0110     public: \
0111         inline Qhull##C##Iterator(const Qhull##C<T> &container) \
0112         : c(container), i(c.constBegin()) {} \
0113         inline Qhull##C##Iterator &operator=(const Qhull##C<T> &container) \
0114         { c= container; i= c.constBegin(); return *this; } \
0115         inline void toFront() { i= c.constBegin(); } \
0116         inline void toBack() { i= c.constEnd(); } \
0117         inline bool hasNext() const { return i != c.constEnd(); } \
0118         inline const T &next() { return *i++; } \
0119         inline const T &peekNext() const { return *i; } \
0120         inline bool hasPrevious() const { return i != c.constBegin(); } \
0121         inline const T &previous() { return *--i; } \
0122         inline const T &peekPrevious() const { const_iterator p= i; return *--p; } \
0123         inline bool findNext(const T &t) \
0124         { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
0125         inline bool findPrevious(const T &t) \
0126         { while (i != c.constBegin()) if (*(--i) == t) return true; \
0127         return false;  } \
0128     };//Qhull##C##Iterator
0129 
0130 // Not used in libqhullcpp.
0131 #define QHULL_DECLARE_MUTABLE_SET_ITERATOR(C) \
0132 \
0133 template <class T> \
0134 class QhullMutable##C##Iterator \
0135 { \
0136     typedef typename Qhull##C::iterator iterator; \
0137     typedef typename Qhull##C::const_iterator const_iterator; \
0138     Qhull##C *c; \
0139     iterator i, n; \
0140     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
0141 public: \
0142     inline Mutable##C##Iterator(Qhull##C &container) \
0143         : c(&container) \
0144     { c->setSharable(false); i= c->begin(); n= c->end(); } \
0145     inline ~Mutable##C##Iterator() \
0146     { c->setSharable(true); } \
0147     inline Mutable##C##Iterator &operator=(Qhull##C &container) \
0148     { c->setSharable(true); c= &container; c->setSharable(false); \
0149       i= c->begin(); n= c->end(); return *this; } \
0150     inline void toFront() { i= c->begin(); n= c->end(); } \
0151     inline void toBack() { i= c->end(); n= i; } \
0152     inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \
0153     inline T &next() { n= i++; return *n; } \
0154     inline T &peekNext() const { return *i; } \
0155     inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \
0156     inline T &previous() { n= --i; return *n; } \
0157     inline T &peekPrevious() const { iterator p= i; return *--p; } \
0158     inline void remove() \
0159     { if (c->constEnd() != const_iterator(n)) { i= c->erase(n); n= c->end(); } } \
0160     inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n= t; } \
0161     inline T &value() { Q_ASSERT(item_exists()); return *n; } \
0162     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
0163     inline void insert(const T &t) { n= i= c->insert(i, t); ++i; } \
0164     inline bool findNext(const T &t) \
0165     { while (c->constEnd() != const_iterator(n= i)) if (*i++ == t) return true; return false; } \
0166     inline bool findPrevious(const T &t) \
0167     { while (c->constBegin() != const_iterator(i)) if (*(n= --i) == t) return true; \
0168       n= c->end(); return false;  } \
0169 };//QhullMutable##C##Iterator
0170 
0171 }//namespace orgQhull
0172 
0173 #endif // QHULLITERATOR_H
0174