Coordinates.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (c) 2009-2011 C.B. Barber. All rights reserved.
00004 ** $Id: //main/2011/qhull/src/libqhullcpp/Coordinates.h#5 $$Change: 1382 $
00005 ** $DateTime: 2011/05/14 10:45:42 $$Author: bbarber $
00006 **
00007 ****************************************************************************/
00008 
00009 #ifndef QHCOORDINATES_H
00010 #define QHCOORDINATES_H
00011 
00012 #include "QhullError.h"
00013 #include "QhullIterator.h"
00014 extern "C" {
00015     #include "libqhull/qhull_a.h"
00016 }
00017 
00018 
00019 #include <cstddef> // ptrdiff_t, size_t
00020 #include <ostream>
00021 #include <vector>
00022 
00023 namespace orgQhull {
00024 
00025 #//Types
00026 
00027 
00028 
00029     class  Coordinates;
00030     class  MutableCoordinatesIterator;
00031 
00032 
00033 class Coordinates {
00034 
00035 private:
00036 #//Fields
00037     std::vector<coordT> coordinate_array;
00038 
00039 public:
00040 #//Subtypes
00041 
00042     class                       const_iterator;
00043     class                       iterator;
00044     typedef iterator Iterator;
00045     typedef const_iterator ConstIterator;
00046 
00047     typedef coordT              value_type;
00048     typedef const value_type   *const_pointer;
00049     typedef const value_type   &const_reference;
00050     typedef value_type         *pointer;
00051     typedef value_type         &reference;
00052     typedef ptrdiff_t           difference_type;
00053     typedef int                 size_type;
00054 
00055 #//Construct
00056                         Coordinates() {};
00057     explicit            Coordinates(const std::vector<coordT> &other) : coordinate_array(other) {}
00058                         Coordinates(const Coordinates &other) : coordinate_array(other.coordinate_array) {}
00059     Coordinates        &operator=(const Coordinates &other) { coordinate_array= other.coordinate_array; return *this; }
00060     Coordinates        &operator=(const std::vector<coordT> &other) { coordinate_array= other; return *this; }
00061                        ~Coordinates() {}
00062 
00063 #//Conversion
00064 
00065     coordT             *data() { return isEmpty() ? 0 : &at(0); }
00066     const coordT       *data() const { return const_cast<const pointT*>(isEmpty() ? 0 : &at(0)); }
00067 
00068 #ifndef QHULL_NO_STL
00069     std::vector<coordT> toStdVector() const { return coordinate_array; }
00070 #endif //QHULL_NO_STL
00071 #ifdef QHULL_USES_QT
00072     QList<coordT>      toQList() const;
00073 #endif //QHULL_USES_QT
00074 
00075 #//GetSet
00076     int                count() const { return static_cast<int>(size()); }
00077     bool               empty() const { return coordinate_array.empty(); }
00078     bool               isEmpty() const { return empty(); }
00079     bool               operator==(const Coordinates &other) const  { return coordinate_array==other.coordinate_array; }
00080     bool               operator!=(const Coordinates &other) const  { return coordinate_array!=other.coordinate_array; }
00081     size_t             size() const { return coordinate_array.size(); }
00082 
00083 #//Element access
00084     coordT             &at(int idx) { return coordinate_array.at(idx); }
00085     const coordT       &at(int idx) const { return coordinate_array.at(idx); }
00086     coordT             &back() { return coordinate_array.back(); }
00087     const coordT       &back() const { return coordinate_array.back(); }
00088     coordT             &first() { return front(); }
00089     const coordT       &first() const { return front(); }
00090     coordT             &front() { return coordinate_array.front(); }
00091     const coordT       &front() const { return coordinate_array.front(); }
00092     coordT             &last() { return back(); }
00093     const coordT       &last() const { return back(); }
00094     Coordinates        mid(int idx, int length= -1) const;
00095     coordT            &operator[](int idx) { return coordinate_array.operator[](idx); }
00096     const coordT      &operator[](int idx) const { return coordinate_array.operator[](idx); }
00097     coordT             value(int idx, const coordT &defaultValue) const;
00098 
00099 #//Iterator
00100     iterator            begin() { return iterator(coordinate_array.begin()); }
00101     const_iterator      begin() const { return const_iterator(coordinate_array.begin()); }
00102     const_iterator      constBegin() const { return begin(); }
00103     const_iterator      constEnd() const { return end(); }
00104     iterator            end() { return iterator(coordinate_array.end()); }
00105     const_iterator      end() const { return const_iterator(coordinate_array.end()); }
00106 
00107 #//Read-only
00108     Coordinates         operator+(const Coordinates &other) const;
00109 
00110 #//Modify
00111     void                append(const coordT &c) { push_back(c); }
00112     void                clear() { coordinate_array.clear(); }
00113     iterator            erase(iterator idx) { return iterator(coordinate_array.erase(idx.base())); }
00114     iterator            erase(iterator beginIterator, iterator endIterator) { return iterator(coordinate_array.erase(beginIterator.base(), endIterator.base())); }
00115     void                insert(int before, const coordT &c) { insert(begin()+before, c); }
00116     iterator            insert(iterator before, const coordT &c) { return iterator(coordinate_array.insert(before.base(), c)); }
00117     void                move(int from, int to) { insert(to, takeAt(from)); }
00118     Coordinates        &operator+=(const Coordinates &other);
00119     Coordinates        &operator+=(const coordT &c) { append(c); return *this; }
00120     Coordinates        &operator<<(const Coordinates &other) { return *this += other; }
00121     Coordinates        &operator<<(const coordT &c) { return *this += c; }
00122     void                pop_back() { coordinate_array.pop_back(); }
00123     void                pop_front() { removeFirst(); }
00124     void                prepend(const coordT &c) { insert(begin(), c); }
00125     void                push_back(const coordT &c) { coordinate_array.push_back(c); }
00126     void                push_front(const coordT &c) { insert(begin(), c); }
00127                         //removeAll below
00128     void                removeAt(int idx) { erase(begin()+idx); }
00129     void                removeFirst() { erase(begin()); }
00130     void                removeLast() { erase(--end()); }
00131     void                replace(int idx, const coordT &c) { (*this)[idx]= c; }
00132     void                reserve(int i) { coordinate_array.reserve(i); }
00133     void                swap(int idx, int other);
00134     coordT              takeAt(int idx);
00135     coordT              takeFirst() { return takeAt(0); }
00136     coordT              takeLast();
00137 
00138 #//Search
00139     bool                contains(const coordT &t) const;
00140     int                 count(const coordT &t) const;
00141     int                 indexOf(const coordT &t, int from = 0) const;
00142     int                 lastIndexOf(const coordT &t, int from = -1) const;
00143     void                removeAll(const coordT &t);
00144 
00145 #//Coordinates::iterator -- from QhullPoints, forwarding to coordinate_array
00146     // before const_iterator for conversion with comparison operators
00147     class iterator {
00148 
00149     private:
00150         std::vector<coordT>::iterator i;
00151         friend class    const_iterator;
00152 
00153     public:
00154         typedef std::random_access_iterator_tag  iterator_category;
00155         typedef coordT      value_type;
00156         typedef value_type *pointer;
00157         typedef value_type &reference;
00158         typedef ptrdiff_t   difference_type;
00159 
00160                         iterator() {}
00161                         iterator(const iterator &other) { i= other.i; }
00162         explicit        iterator(const std::vector<coordT>::iterator &vi) { i= vi; }
00163         iterator       &operator=(const iterator &other) { i= other.i; return *this; }
00164         std::vector<coordT>::iterator &base() { return i; }
00165                         // No operator-> for base types
00166         coordT         &operator*() const { return *i; }
00167         coordT         &operator[](int idx) const { return i[idx]; }
00168 
00169         bool            operator==(const iterator &other) const { return i==other.i; }
00170         bool            operator!=(const iterator &other) const { return i!=other.i; }
00171         bool            operator<(const iterator &other) const { return i<other.i; }
00172         bool            operator<=(const iterator &other) const { return i<=other.i; }
00173         bool            operator>(const iterator &other) const { return i>other.i; }
00174         bool            operator>=(const iterator &other) const { return i>=other.i; }
00175               // reinterpret_cast to break circular dependency
00176         bool            operator==(const Coordinates::const_iterator &other) const { return *this==reinterpret_cast<const iterator &>(other); }
00177         bool            operator!=(const Coordinates::const_iterator &other) const { return *this!=reinterpret_cast<const iterator &>(other); }
00178         bool            operator<(const Coordinates::const_iterator &other) const { return *this<reinterpret_cast<const iterator &>(other); }
00179         bool            operator<=(const Coordinates::const_iterator &other) const { return *this<=reinterpret_cast<const iterator &>(other); }
00180         bool            operator>(const Coordinates::const_iterator &other) const { return *this>reinterpret_cast<const iterator &>(other); }
00181         bool            operator>=(const Coordinates::const_iterator &other) const { return *this>=reinterpret_cast<const iterator &>(other); }
00182 
00183         iterator        operator++() { return iterator(++i); } //FIXUP QH11012 Should return reference, but get reference to temporary
00184         iterator        operator++(int) { return iterator(i++); }
00185         iterator        operator--() { return iterator(--i); }
00186         iterator        operator--(int) { return iterator(i--); }
00187         iterator        operator+=(int idx) { return iterator(i += idx); }
00188         iterator        operator-=(int idx) { return iterator(i -= idx); }
00189         iterator        operator+(int idx) const { return iterator(i+idx); }
00190         iterator        operator-(int idx) const { return iterator(i-idx); }
00191         difference_type operator-(iterator other) const { return i-other.i; }
00192     };//Coordinates::iterator
00193 
00194 #//Coordinates::const_iterator
00195     class const_iterator {
00196 
00197     private:
00198         std::vector<coordT>::const_iterator i;
00199 
00200     public:
00201         typedef std::random_access_iterator_tag  iterator_category;
00202         typedef coordT            value_type;
00203         typedef const value_type *pointer;
00204         typedef const value_type &reference;
00205         typedef ptrdiff_t         difference_type;
00206 
00207                         const_iterator() {}
00208                         const_iterator(const const_iterator &other) { i= other.i; }
00209                         const_iterator(iterator o) : i(o.i) {}
00210         explicit        const_iterator(const std::vector<coordT>::const_iterator &vi) { i= vi; }
00211         const_iterator &operator=(const const_iterator &other) { i= other.i; return *this; }
00212                         // No operator-> for base types
00213                         // No reference to a base type for () and []
00214         const coordT   &operator*() const { return *i; }
00215         const coordT   &operator[](int idx) const { return i[idx]; }
00216 
00217         bool            operator==(const const_iterator &other) const { return i==other.i; }
00218         bool            operator!=(const const_iterator &other) const { return i!=other.i; }
00219         bool            operator<(const const_iterator &other) const { return i<other.i; }
00220         bool            operator<=(const const_iterator &other) const { return i<=other.i; }
00221         bool            operator>(const const_iterator &other) const { return i>other.i; }
00222         bool            operator>=(const const_iterator &other) const { return i>=other.i; }
00223 
00224         const_iterator  operator++() { return const_iterator(++i); } //FIXUP QH11014 -- too much copying
00225         const_iterator  operator++(int) { return const_iterator(i++); }
00226         const_iterator  operator--() { return const_iterator(--i); }
00227         const_iterator  operator--(int) { return const_iterator(i--); }
00228         const_iterator  operator+=(int idx) { return const_iterator(i += idx); }
00229         const_iterator  operator-=(int idx) { return const_iterator(i -= idx); }
00230         const_iterator  operator+(int idx) const { return const_iterator(i+idx); }
00231         const_iterator  operator-(int idx) const { return const_iterator(i-idx); }
00232         difference_type operator-(const_iterator other) const { return i-other.i; }
00233     };//Coordinates::const_iterator
00234 
00235 };//Coordinates
00236 
00237 //class CoordinatesIterator
00238 //QHULL_DECLARE_SEQUENTIAL_ITERATOR(Coordinates, coordT)
00239 
00240 class CoordinatesIterator
00241 {
00242     typedef Coordinates::const_iterator const_iterator;
00243     const Coordinates *c;
00244     const_iterator i;
00245     public:
00246     inline CoordinatesIterator(const Coordinates &container)
00247     : c(&container), i(c->constBegin()) {}
00248     inline CoordinatesIterator &operator=(const Coordinates &container)
00249     { c = &container; i = c->constBegin(); return *this; }
00250     inline void toFront() { i = c->constBegin(); }
00251     inline void toBack() { i = c->constEnd(); }
00252     inline bool hasNext() const { return i != c->constEnd(); }
00253     inline const coordT &next() { return *i++; }
00254     inline const coordT &peekNext() const { return *i; }
00255     inline bool hasPrevious() const { return i != c->constBegin(); }
00256     inline const coordT &previous() { return *--i; }
00257     inline const coordT &peekPrevious() const { const_iterator p = i; return *--p; }
00258     inline bool findNext(const coordT &t)
00259     { while (i != c->constEnd()) if (*i++ == t) return true; return false; }
00260     inline bool findPrevious(const coordT &t)
00261     { while (i != c->constBegin()) if (*(--i) == t) return true;
00262     return false;  }
00263 };//CoordinatesIterator
00264 
00265 //class MutableCoordinatesIterator
00266 //QHULL_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Coordinates, coordT)
00267 class MutableCoordinatesIterator
00268 {
00269     typedef Coordinates::iterator iterator;
00270     typedef Coordinates::const_iterator const_iterator;
00271     Coordinates *c;
00272     iterator i, n;
00273     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); }
00274     public:
00275     inline MutableCoordinatesIterator(Coordinates &container)
00276     : c(&container)
00277     { i = c->begin(); n = c->end(); }
00278     inline ~MutableCoordinatesIterator()
00279     {}
00280     inline MutableCoordinatesIterator &operator=(Coordinates &container)
00281     { c = &container;
00282     i = c->begin(); n = c->end(); return *this; }
00283     inline void toFront() { i = c->begin(); n = c->end(); }
00284     inline void toBack() { i = c->end(); n = i; }
00285     inline bool hasNext() const { return c->constEnd() != const_iterator(i); }
00286     inline coordT &next() { n = i++; return *n; }
00287     inline coordT &peekNext() const { return *i; }
00288     inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); }
00289     inline coordT &previous() { n = --i; return *n; }
00290     inline coordT &peekPrevious() const { iterator p = i; return *--p; }
00291     inline void remove()
00292     { if (c->constEnd() != const_iterator(n)) { i = c->erase(n); n = c->end(); } }
00293     inline void setValue(const coordT &t) const { if (c->constEnd() != const_iterator(n)) *n = t; }
00294     inline coordT &value() { QHULL_ASSERT(item_exists()); return *n; }
00295     inline const coordT &value() const { QHULL_ASSERT(item_exists()); return *n; }
00296     inline void insert(const coordT &t) { n = i = c->insert(i, t); ++i; }
00297     inline bool findNext(const coordT &t)
00298     { while (c->constEnd() != const_iterator(n = i)) if (*i++ == t) return true; return false; }
00299     inline bool findPrevious(const coordT &t)
00300     { while (c->constBegin() != const_iterator(i)) if (*(n = --i) == t) return true;
00301     n = c->end(); return false;  }
00302 };//MutableCoordinatesIterator
00303 
00304 
00305 }//namespace orgQhull
00306 
00307 #//Global functions
00308 
00309 std::ostream &operator<<(std::ostream &os, const orgQhull::Coordinates &c);
00310 
00311 #endif // QHCOORDINATES_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


libqhull
Author(s): Robert Krug
autogenerated on Tue Jun 18 2013 12:38:49