Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /****************************************************************************
0002 **
0003 ** Copyright (c) 2009-2020 C.B. Barber. All rights reserved.
0004 ** $Id: //main/2019/qhull/src/libqhullcpp/Coordinates.h#5 $$Change: 3001 $
0005 ** $DateTime: 2020/07/24 20:43:28 $$Author: bbarber $
0006 **
0007 ****************************************************************************/
0008 
0009 #ifndef QHCOORDINATES_H
0010 #define QHCOORDINATES_H
0011 
0012 #include "libqhull_r/qhull_ra.h"
0013 #include "libqhullcpp/QhullError.h"
0014 
0015 #include <cstddef> // ptrdiff_t, size_t
0016 #include <ostream>
0017 // Requires STL vector class.  Can use with another vector class such as QList.
0018 #include <vector>
0019 
0020 namespace orgQhull {
0021 
0022 #//!\name Defined here
0023     //! An std::vector of point coordinates independent of dimension
0024     //! Used by PointCoordinates for RboxPoints and by Qhull for feasiblePoint
0025     //! A QhullPoint refers to previously allocated coordinates
0026     class Coordinates;
0027 
0028     //! Java-style iterators are not implemented for Coordinates.  std::vector has an expensive copy constructor and copy assignment.
0029     //!    A pointer to Coordinates is vulnerable to mysterious overwrites (e.g., deleting a returned value and reusing its memory)
0030     //! Qt's 'foreach' should not be used.  It makes a copy of the std::vector
0031 
0032 class Coordinates {
0033 
0034 private:
0035 #//!\name Fields
0036     std::vector<coordT> coordinate_array;
0037 
0038 public:
0039 #//!\name Subtypes
0040 
0041     class const_iterator;
0042     class iterator;
0043     typedef iterator Iterator;
0044     typedef const_iterator ConstIterator;
0045 
0046     typedef coordT              value_type;
0047     typedef const value_type   *const_pointer;
0048     typedef const value_type &  const_reference;
0049     typedef value_type *        pointer;
0050     typedef value_type &        reference;
0051     typedef ptrdiff_t           difference_type;
0052     typedef countT              size_type;
0053 
0054 #//!\name Construct
0055                         Coordinates() : coordinate_array() {}
0056     explicit            Coordinates(const std::vector<coordT> &other) : coordinate_array(other) {}
0057                         Coordinates(const Coordinates &other) : coordinate_array(other.coordinate_array) {}
0058     Coordinates &       operator=(const Coordinates &other) { coordinate_array= other.coordinate_array; return *this; }
0059     Coordinates &       operator=(const std::vector<coordT> &other) { coordinate_array= other; return *this; }
0060                         ~Coordinates() {}
0061 
0062 #//!\name Conversion
0063 
0064 #ifndef QHULL_NO_STL
0065     std::vector<coordT> toStdVector() const { return coordinate_array; }
0066 #endif //QHULL_NO_STL
0067 #ifdef QHULL_USES_QT
0068     QList<coordT>       toQList() const;
0069 #endif //QHULL_USES_QT
0070 
0071 #//!\name GetSet
0072     countT              count() const { return static_cast<countT>(size()); }
0073     coordT *            data() { return (isEmpty() ? NULL : &at(0)); }
0074     const coordT *      data() const { return (isEmpty() ? NULL : &at(0)); }
0075     bool                isEmpty() const { return coordinate_array.empty(); }
0076     bool                operator==(const Coordinates &other) const  { return coordinate_array==other.coordinate_array; }
0077     bool                operator!=(const Coordinates &other) const  { return coordinate_array!=other.coordinate_array; }
0078     size_t              size() const { return coordinate_array.size(); }
0079 
0080 #//!\name Element access
0081     coordT &            at(countT idx) { return coordinate_array.at(idx); }
0082     const coordT &      at(countT idx) const { return coordinate_array.at(idx); }
0083     coordT &            back() { return coordinate_array.back(); }
0084     const coordT &      back() const { return coordinate_array.back(); }
0085     coordT &            first() { return front(); }
0086     const coordT &      first() const { return front(); }
0087     coordT &            front() { return coordinate_array.front(); }
0088     const coordT &      front() const { return coordinate_array.front(); }
0089     coordT &            last() { return back(); }
0090     const coordT &      last() const { return back(); }
0091     Coordinates         mid(countT idx, countT length= -1) const; //!<\todo countT -1 indicates
0092     coordT &            operator[](countT idx) { return coordinate_array.operator[](idx); }
0093     const coordT &      operator[](countT idx) const { return coordinate_array.operator[](idx); }
0094     coordT              value(countT idx, const coordT &defaultValue) const;
0095 
0096 #//!\name Iterator
0097     iterator            begin() { return iterator(coordinate_array.begin()); }
0098     const_iterator      begin() const { return const_iterator(coordinate_array.begin()); }
0099     const_iterator      constBegin() const { return begin(); }
0100     const_iterator      constEnd() const { return end(); }
0101     iterator            end() { return iterator(coordinate_array.end()); }
0102     const_iterator      end() const { return const_iterator(coordinate_array.end()); }
0103 
0104 #//!\name GetSet
0105     Coordinates         operator+(const Coordinates &other) const;
0106 
0107 #//!\name Modify
0108     void                append(int pointDimension, coordT *c);
0109     void                append(const coordT &c) { push_back(c); }
0110     void                clear() { coordinate_array.clear(); }
0111     iterator            erase(iterator idx) { return iterator(coordinate_array.erase(idx.base())); }
0112     iterator            erase(iterator beginIterator, iterator endIterator) { return iterator(coordinate_array.erase(beginIterator.base(), endIterator.base())); }
0113     void                insert(countT before, const coordT &c) { insert(begin()+before, c); }
0114     iterator            insert(iterator before, const coordT &c) { return iterator(coordinate_array.insert(before.base(), c)); }
0115     void                move(countT from, countT to) { insert(to, takeAt(from)); }
0116     Coordinates &       operator+=(const Coordinates &other);
0117     Coordinates &       operator+=(const coordT &c) { append(c); return *this; }
0118     Coordinates &       operator<<(const Coordinates &other) { return *this += other; }
0119     Coordinates &       operator<<(const coordT &c) { return *this += c; }
0120     void                pop_back() { coordinate_array.pop_back(); }
0121     void                pop_front() { removeFirst(); }
0122     void                prepend(const coordT &c) { insert(begin(), c); }
0123     void                push_back(const coordT &c) { coordinate_array.push_back(c); }
0124     void                push_front(const coordT &c) { insert(begin(), c); }
0125                         //removeAll below
0126     void                removeAt(countT idx) { erase(begin()+idx); }
0127     void                removeFirst() { erase(begin()); }
0128     void                removeLast() { erase(--end()); }
0129     void                replace(countT idx, const coordT &c) { (*this)[idx]= c; }
0130     void                reserve(countT i) { coordinate_array.reserve(i); }
0131     void                swap(countT idx, countT other);
0132     coordT              takeAt(countT idx);
0133     coordT              takeFirst() { return takeAt(0); }
0134     coordT              takeLast();
0135 
0136 #//!\name Search
0137     bool                contains(const coordT &t) const;
0138     countT              count(const coordT &t) const;
0139     countT              indexOf(const coordT &t, countT from= 0) const;
0140     countT              lastIndexOf(const coordT &t, countT from= -1) const;
0141     void                removeAll(const coordT &t);
0142 
0143 #//!\name Coordinates::iterator -- from QhullPoints, forwarding to coordinate_array
0144     // before const_iterator for conversion with comparison operators
0145     // Reviewed corelib/tools/qlist.h and corelib/tools/qvector.h w/o QT_STRICT_ITERATORS
0146     class iterator {
0147 
0148     private:
0149         std::vector<coordT>::iterator i;
0150         friend class const_iterator;
0151 
0152     public:
0153         typedef std::random_access_iterator_tag  iterator_category;
0154         typedef coordT      value_type;
0155         typedef value_type *pointer;
0156         typedef value_type &reference;
0157         typedef ptrdiff_t   difference_type;
0158 
0159                         iterator() : i() {}
0160                         iterator(const iterator &other) : i() { i= other.i; }
0161         explicit        iterator(const std::vector<coordT>::iterator &vi) : i() { i= vi; }
0162         iterator &      operator=(const iterator &other) { i= other.i; return *this; }
0163         std::vector<coordT>::iterator &base() { return i; }
0164         coordT &        operator*() const { return *i; }
0165         // No operator->() when the base type is double
0166         coordT &        operator[](countT idx) const { return i[idx]; }
0167 
0168         bool            operator==(const iterator &other) const { return i==other.i; }
0169         bool            operator!=(const iterator &other) const { return i!=other.i; }
0170         bool            operator<(const iterator &other) const { return i<other.i; }
0171         bool            operator<=(const iterator &other) const { return i<=other.i; }
0172         bool            operator>(const iterator &other) const { return i>other.i; }
0173         bool            operator>=(const iterator &other) const { return i>=other.i; }
0174               // reinterpret_cast to break circular dependency
0175         bool            operator==(const Coordinates::const_iterator &other) const { return *this==reinterpret_cast<const iterator &>(other); }
0176         bool            operator!=(const Coordinates::const_iterator &other) const { return *this!=reinterpret_cast<const iterator &>(other); }
0177         bool            operator<(const Coordinates::const_iterator &other) const { return *this<reinterpret_cast<const iterator &>(other); }
0178         bool            operator<=(const Coordinates::const_iterator &other) const { return *this<=reinterpret_cast<const iterator &>(other); }
0179         bool            operator>(const Coordinates::const_iterator &other) const { return *this>reinterpret_cast<const iterator &>(other); }
0180         bool            operator>=(const Coordinates::const_iterator &other) const { return *this>=reinterpret_cast<const iterator &>(other); }
0181 
0182         iterator &      operator++() { ++i; return *this; }
0183         iterator        operator++(int) { return iterator(i++); }
0184         iterator &      operator--() { --i; return *this; }
0185         iterator        operator--(int) { return iterator(i--); }
0186         iterator &      operator+=(countT idx) { i += idx; return *this; }
0187         iterator &      operator-=(countT idx) { i -= idx; return *this; }
0188         iterator        operator+(countT idx) const { return iterator(i+idx); }
0189         iterator        operator-(countT idx) const { return iterator(i-idx); }
0190         difference_type operator-(iterator other) const { return i-other.i; }
0191     };//Coordinates::iterator
0192 
0193 #//!\name Coordinates::const_iterator
0194     class const_iterator {
0195 
0196     private:
0197         std::vector<coordT>::const_iterator i;
0198 
0199     public:
0200         typedef std::random_access_iterator_tag  iterator_category;
0201         typedef coordT            value_type;
0202         typedef const value_type *pointer;
0203         typedef const value_type &reference;
0204         typedef ptrdiff_t         difference_type;
0205 
0206                         const_iterator() : i() {}
0207                         const_iterator(const const_iterator &other) : i() { i= other.i; }
0208                         const_iterator(const iterator &o) : i(o.i) {}
0209         explicit        const_iterator(const std::vector<coordT>::const_iterator &vi) : i() { i= vi; }
0210         const_iterator &operator=(const const_iterator &other) { i= other.i; return *this; }
0211         const coordT &  operator*() const { return *i; }
0212         // No operator->() when the base type is double
0213         const coordT &  operator[](countT idx) const { return i[idx]; }
0214 
0215         bool            operator==(const const_iterator &other) const { return i==other.i; }
0216         bool            operator!=(const const_iterator &other) const { return i!=other.i; }
0217         bool            operator<(const const_iterator &other) const { return i<other.i; }
0218         bool            operator<=(const const_iterator &other) const { return i<=other.i; }
0219         bool            operator>(const const_iterator &other) const { return i>other.i; }
0220         bool            operator>=(const const_iterator &other) const { return i>=other.i; }
0221 
0222         const_iterator & operator++() { ++i; return *this; }
0223         const_iterator  operator++(int) { return const_iterator(i++); }
0224         const_iterator & operator--() { --i; return *this; }
0225         const_iterator  operator--(int) { return const_iterator(i--); }
0226         const_iterator & operator+=(countT idx) { i += idx; return *this; }
0227         const_iterator & operator-=(countT idx) { i -= idx; return *this; }
0228         const_iterator  operator+(countT idx) const { return const_iterator(i+idx); }
0229         const_iterator  operator-(countT idx) const { return const_iterator(i-idx); }
0230         difference_type operator-(const_iterator other) const { return i-other.i; }
0231     };//Coordinates::const_iterator
0232 
0233 };//Coordinates
0234 
0235 }//namespace orgQhull
0236 
0237 #//!\name Global
0238 
0239 std::ostream &operator<<(std::ostream &os, const orgQhull::Coordinates &c);
0240 
0241 #endif // QHCOORDINATES_H