QhullSet.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (c) 2008-2015 C.B. Barber. All rights reserved.
4 ** $Id: //main/2015/qhull/src/libqhullcpp/QhullSet.h#5 $$Change: 2066 $
5 ** $DateTime: 2016/01/18 19:29:17 $$Author: bbarber $
6 **
7 ****************************************************************************/
8 
9 #ifndef QhullSet_H
10 #define QhullSet_H
11 
12 extern "C" {
13  #include "libqhull_r/qhull_ra.h"
14 }
15 #include "libqhullcpp/QhullError.h"
16 #include "libqhullcpp/QhullQh.h"
17 
18 #include <cstddef> // ptrdiff_t, size_t
19 
20 #ifndef QHULL_NO_STL
21 #include <vector>
22 #endif
23 
24 #ifdef QHULL_USES_QT
25  #include <QtCore/QList>
26 #endif
27 
28 namespace orgQhull {
29 
30 #
31  class Qhull;
32 
33 #
34  class QhullSetBase;
35 
39 class QhullSetBase {
42 
43 private:
44 #
45  setT * qh_set;
47 
48 #
49  static setT s_empty_set;
50 
51 public:
52 #
53  QhullSetBase(const Qhull &q, setT *s);
54  QhullSetBase(QhullQh *qqh, setT *s) : qh_set(s ? s : &s_empty_set), qh_qh(qqh) {}
56  QhullSetBase(const QhullSetBase &other) : qh_set(other.qh_set), qh_qh(other.qh_qh) {}
57  QhullSetBase & operator=(const QhullSetBase &other) { qh_set= other.qh_set; qh_qh= other.qh_qh; return *this; }
59 
60 private:
63 public:
64 
65 #
66  countT count() const { return QhullSetBase::count(qh_set); }
67  void defineAs(setT *s) { qh_set= s ? s : &s_empty_set; }
69  setT * getSetT() const { return qh_set; }
70  bool isEmpty() const { return SETempty_(qh_set); }
71  QhullQh * qh() const { return qh_qh; }
72  setT ** referenceSetT() { return &qh_set; }
73  size_t size() const { return QhullSetBase::count(qh_set); }
74 
75 #
76 protected:
77  void ** beginPointer() const { return &qh_set->e[0].p; }
78  void ** elementPointer(countT idx) const { QHULL_ASSERT(idx>=0 && idx<qh_set->maxsize); return &SETelem_(qh_set, idx); }
80  void ** endPointer() const { return qh_setendpointer(qh_set); }
81 
82 #
83 public:
84  static countT count(const setT *set);
85  //s may be null
86  static bool isEmpty(const setT *s) { return SETempty_(s); }
87 };//QhullSetBase
88 
89 
98 template <typename T>
99 class QhullSet : public QhullSetBase {
100 
101 private:
102 #
103 
104 #
105  static setT s_empty_set;
106 
107 public:
108 #
109  class iterator;
110  class const_iterator;
111  typedef typename QhullSet<T>::iterator Iterator;
113 
114 #
115  QhullSet<T>(const Qhull &q, setT *s) : QhullSetBase(q, s) { }
116  QhullSet<T>(QhullQh *qqh, setT *s) : QhullSetBase(qqh, s) { }
117  //Conversion from setT* is not type-safe. Implicit conversion for void* to T
118  //Copy constructor copies pointer but not contents. Needed for return by value.
119  QhullSet<T>(const QhullSet<T> &other) : QhullSetBase(other) {}
120  QhullSet<T> & operator=(const QhullSet<T> &other) { QhullSetBase::operator=(other); return *this; }
122 
123 private:
125  QhullSet<T>();
126 public:
127 
128 #
129 
130 #ifndef QHULL_NO_STL
131  std::vector<T> toStdVector() const;
132 #endif
133 #ifdef QHULL_USES_QT
134  QList<typename T> toQList() const;
135 #endif
136 
137 #
138  using QhullSetBase::count;
139  using QhullSetBase::isEmpty;
140  // operator== defined for QhullSets of the same type
141  bool operator==(const QhullSet<T> &other) const { return qh_setequal(getSetT(), other.getSetT()); }
142  bool operator!=(const QhullSet<T> &other) const { return !operator==(other); }
143 
144 #
145  // Constructs T. Cannot return reference.
146  const T at(countT idx) const { return operator[](idx); }
147  // Constructs T. Cannot return reference.
148  const T back() const { return last(); }
149  T back() { return last(); }
151  const typename T::base_type * constData() const { return reinterpret_cast<const typename T::base_type *>(beginPointer()); }
152  typename T::base_type * data() { return reinterpret_cast<typename T::base_type *>(beginPointer()); }
153  const typename T::base_type *data() const { return reinterpret_cast<const typename T::base_type *>(beginPointer()); }
154  typename T::base_type * endData() { return reinterpret_cast<typename T::base_type *>(endPointer()); }
155  const typename T::base_type * endData() const { return reinterpret_cast<const typename T::base_type *>(endPointer()); }
156  // Constructs T. Cannot return reference.
157  const T first() const { QHULL_ASSERT(!isEmpty()); return T(qh(), *data()); }
158  T first() { QHULL_ASSERT(!isEmpty()); return T(qh(), *data()); }
159  // Constructs T. Cannot return reference.
160  const T front() const { return first(); }
161  T front() { return first(); }
162  // Constructs T. Cannot return reference.
163  const T last() const { QHULL_ASSERT(!isEmpty()); return T(qh(), *(endData()-1)); }
164  T last() { QHULL_ASSERT(!isEmpty()); return T(qh(), *(endData()-1)); }
165  // mid() not available. No setT constructor
166  // Constructs T. Cannot return reference.
167  const T operator[](countT idx) const { const typename T::base_type *p= reinterpret_cast<typename T::base_type *>(elementPointer(idx)); QHULL_ASSERT(idx>=0 && p < endData()); return T(qh(), *p); }
168  T operator[](countT idx) { typename T::base_type *p= reinterpret_cast<typename T::base_type *>(elementPointer(idx)); QHULL_ASSERT(idx>=0 && p < endData()); return T(qh(), *p); }
169  const T second() const { return operator[](1); }
170  T second() { return operator[](1); }
171  T value(countT idx) const;
172  T value(countT idx, const T &defaultValue) const;
173 
174 #
175 
176 #
177  iterator begin() { return iterator(qh(), reinterpret_cast<typename T::base_type *>(beginPointer())); }
178  const_iterator begin() const { return const_iterator(qh(), data()); }
179  const_iterator constBegin() const { return const_iterator(qh(), data()); }
180  const_iterator constEnd() const { return const_iterator(qh(), endData()); }
181  iterator end() { return iterator(qh(), endData()); }
182  const_iterator end() const { return const_iterator(qh(), endData()); }
183 
184 #
185  bool contains(const T &t) const;
186  countT count(const T &t) const;
187  countT indexOf(const T &t) const { /* no qh_qh */ return qh_setindex(getSetT(), t.getBaseT()); }
188  countT lastIndexOf(const T &t) const;
189 
190  // before const_iterator for conversion with comparison operators
191  class iterator {
192  friend class const_iterator;
193  private:
194  typename T::base_type * i; // e.g., facetT**, first for debugger
196 
197  public:
198  typedef ptrdiff_t difference_type;
199  typedef std::bidirectional_iterator_tag iterator_category;
200  typedef T value_type;
201 
202  iterator(QhullQh *qqh, typename T::base_type *p) : i(p), qh_qh(qqh) {}
203  iterator(const iterator &o) : i(o.i), qh_qh(o.qh_qh) {}
204  iterator & operator=(const iterator &o) { i= o.i; qh_qh= o.qh_qh; return *this; }
205 
206  // Constructs T. Cannot return reference.
207  T operator*() const { return T(qh_qh, *i); }
208  //operator->() n/a, value-type
209  // Constructs T. Cannot return reference.
210  T operator[](countT idx) const { return T(qh_qh, *(i+idx)); }
211  bool operator==(const iterator &o) const { return i == o.i; }
212  bool operator!=(const iterator &o) const { return !operator==(o); }
213  bool operator==(const const_iterator &o) const { return (i==reinterpret_cast<const iterator &>(o).i); }
214  bool operator!=(const const_iterator &o) const { return !operator==(o); }
215 
217  countT operator-(const iterator &o) const { return (countT)(i-o.i); } //WARN64
218  bool operator>(const iterator &o) const { return i>o.i; }
219  bool operator<=(const iterator &o) const { return !operator>(o); }
220  bool operator<(const iterator &o) const { return i<o.i; }
221  bool operator>=(const iterator &o) const { return !operator<(o); }
222  bool operator>(const const_iterator &o) const { return (i > reinterpret_cast<const iterator &>(o).i); }
223  bool operator<=(const const_iterator &o) const { return !operator>(o); }
224  bool operator<(const const_iterator &o) const { return (i < reinterpret_cast<const iterator &>(o).i); }
225  bool operator>=(const const_iterator &o) const { return !operator<(o); }
226 
228  iterator & operator++() { ++i; return *this; }
229  iterator operator++(int) { iterator o= *this; ++i; return o; }
230  iterator & operator--() { --i; return *this; }
231  iterator operator--(int) { iterator o= *this; --i; return o; }
232  iterator operator+(countT j) const { return iterator(qh_qh, i+j); }
233  iterator operator-(countT j) const { return operator+(-j); }
234  iterator & operator+=(countT j) { i += j; return *this; }
235  iterator & operator-=(countT j) { i -= j; return *this; }
236  };//QhullPointSet::iterator
237 
239  private:
240  const typename T::base_type * i; // e.g., const facetT**, first for debugger
242 
243  public:
244  typedef ptrdiff_t difference_type;
245  typedef std::random_access_iterator_tag iterator_category;
246  typedef T value_type;
247 
248  const_iterator(QhullQh *qqh, const typename T::base_type * p) : i(p), qh_qh(qqh) {}
249  const_iterator(const const_iterator &o) : i(o.i), qh_qh(o.qh_qh) {}
250  const_iterator(const iterator &o) : i(o.i), qh_qh(o.qh_qh) {}
251  const_iterator &operator=(const const_iterator &o) { i= o.i; qh_qh= o.qh_qh; return *this; }
252 
253  // Constructs T. Cannot return reference. Retaining 'const T' return type for consistency with QList/QVector
254  const T operator*() const { return T(qh_qh, *i); }
255  const T operator[](countT idx) const { return T(qh_qh, *(i+idx)); }
256  //operator->() n/a, value-type
257  bool operator==(const const_iterator &o) const { return i == o.i; }
258  bool operator!=(const const_iterator &o) const { return !operator==(o); }
259 
261  countT operator-(const const_iterator &o) { return (countT)(i-o.i); } //WARN64
262  bool operator>(const const_iterator &o) const { return i>o.i; }
263  bool operator<=(const const_iterator &o) const { return !operator>(o); }
264  bool operator<(const const_iterator &o) const { return i<o.i; }
265  bool operator>=(const const_iterator &o) const { return !operator<(o); }
266 
268  const_iterator &operator++() { ++i; return *this; }
269  const_iterator operator++(int) { const_iterator o= *this; ++i; return o; }
270  const_iterator &operator--() { --i; return *this; }
271  const_iterator operator--(int) { const_iterator o= *this; --i; return o; }
272  const_iterator operator+(int j) const { return const_iterator(qh_qh, i+j); }
273  const_iterator operator-(int j) const { return operator+(-j); }
274  const_iterator &operator+=(int j) { i += j; return *this; }
275  const_iterator &operator-=(int j) { i -= j; return *this; }
276  };//QhullPointSet::const_iterator
277 
278 };//class QhullSet
279 
280 
282 template <typename T>
284 
285 #
286  typedef typename QhullSet<T>::const_iterator const_iterator;
287 
288 private:
289 #
290  const typename T::base_type * i; // e.g., facetT**, first for debugger
291  const typename T::base_type * begin_i; // must be initialized after i
292  const typename T::base_type * end_i;
294 
295 public:
296 #
297  QhullSetIterator<T>(const QhullSet<T> &s) : i(s.data()), begin_i(i), end_i(s.endData()), qh_qh(s.qh()) {}
298  QhullSetIterator<T>(const QhullSetIterator<T> &o) : i(o.i), begin_i(o.begin_i), end_i(o.end_i), qh_qh(o.qh_qh) {}
299  QhullSetIterator<T> &operator=(const QhullSetIterator<T> &o) { i= o.i; begin_i= o.begin_i; end_i= o.end_i; qh_qh= o.qh_qh; return *this; }
300 
301 #
302  countT countRemaining() { return (countT)(end_i-i); } // WARN64
303 
304 #
305  bool findNext(const T &t);
306  bool findPrevious(const T &t);
307 
308 #
309  bool hasNext() const { return i != end_i; }
310  bool hasPrevious() const { return i != begin_i; }
311  T next() { return T(qh_qh, *i++); }
312  T peekNext() const { return T(qh_qh, *i); }
313  T peekPrevious() const { const typename T::base_type *p = i; return T(qh_qh, *--p); }
314  T previous() { return T(qh_qh, *--i); }
315  void toBack() { i = end_i; }
316  void toFront() { i = begin_i; }
317 };//class QhullSetIterator
318 
319 #
320 
321 #
322 
323 // See qt-qhull.cpp for QList conversion
324 
325 #ifndef QHULL_NO_STL
326 template <typename T>
327 std::vector<T> QhullSet<T>::
328 toStdVector() const
329 {
330  QhullSet<T>::const_iterator i= begin();
332  std::vector<T> vs;
333  while(i!=e){
334  vs.push_back(*i++);
335  }
336  return vs;
337 }//toStdVector
338 #endif //QHULL_NO_STL
339 
340 #ifdef QHULL_USES_QT
341 template <typename T>
342 QList<T> QhullSet<T>::
343 toQList() const
344 {
345  QhullSet<T>::const_iterator i= begin();
347  QList<T> vs;
348  while(i!=e){
349  vs.append(*i++);
350  }
351  return vs;
352 }//toQList
353 #endif
354 
355 #
356 
357 template <typename T>
359 value(countT idx) const
360 {
361  // Avoid call to qh_setsize() and assert in elementPointer()
362  const typename T::base_type *p= reinterpret_cast<const typename T::base_type *>(&SETelem_(getSetT(), idx));
363  return (idx>=0 && p<endData()) ? T(qh(), *p) : T(qh());
364 }//value
365 
366 template <typename T>
368 value(countT idx, const T &defaultValue) const
369 {
370  // Avoid call to qh_setsize() and assert in elementPointer()
371  const typename T::base_type *p= reinterpret_cast<const typename T::base_type *>(&SETelem_(getSetT(), idx));
372  return (idx>=0 && p<endData() ? T(qh(), *p) : defaultValue);
373 }//value
374 
375 #
376 
377 template <typename T>
378 bool QhullSet<T>::
379 contains(const T &t) const
380 {
381  setT *s= getSetT();
382  void *p= t.getBaseT(); // contains() is not inline for better error reporting
383  int result= qh_setin(s, p);
384  return result!=0;
385 }//contains
386 
387 template <typename T>
389 count(const T &t) const
390 {
391  countT n= 0;
392  const typename T::base_type *i= data();
393  const typename T::base_type *e= endData();
394  typename T::base_type p= t.getBaseT();
395  while(i<e){
396  if(*i==p){
397  n++;
398  }
399  i++;
400  }
401  return n;
402 }//count
403 
404 template <typename T>
406 lastIndexOf(const T &t) const
407 {
408  const typename T::base_type *b= data();
409  const typename T::base_type *i= endData();
410  typename T::base_type p= t.getBaseT();
411  while(--i>=b){
412  if(*i==p){
413  break;
414  }
415  }
416  return (countT)(i-b); // WARN64
417 }//lastIndexOf
418 
419 #
420 
421 template <typename T>
423 findNext(const T &t)
424 {
425  typename T::base_type p= t.getBaseT();
426  while(i!=end_i){
427  if(*(++i)==p){
428  return true;
429  }
430  }
431  return false;
432 }//findNext
433 
434 template <typename T>
436 findPrevious(const T &t)
437 {
438  typename T::base_type p= t.getBaseT();
439  while(i!=begin_i){
440  if(*(--i)==p){
441  return true;
442  }
443  }
444  return false;
445 }//findPrevious
446 
447 }//namespace orgQhull
448 
449 
450 #
451 
452 template <typename T>
453 std::ostream &
454 operator<<(std::ostream &os, const orgQhull::QhullSet<T> &qs)
455 {
456  const typename T::base_type *i= qs.data();
457  const typename T::base_type *e= qs.endData();
458  while(i!=e){
459  os << T(qs.qh(), *i++);
460  }
461  return os;
462 }//operator<<
463 
464 #endif // QhullSet_H
const T::base_type * endData() const
Definition: QhullSet.h:155
countT indexOf(const T &t) const
Definition: QhullSet.h:187
const T second() const
Definition: QhullSet.h:169
bool operator==(const const_iterator &o) const
Definition: QhullSet.h:257
iterator(const iterator &o)
Definition: QhullSet.h:203
iterator operator--(int)
Definition: QhullSet.h:231
bool operator>=(const iterator &o) const
Definition: QhullSet.h:221
const_iterator(const iterator &o)
Definition: QhullSet.h:250
std::vector< T > toStdVector() const
Definition: QhullSet.h:328
iterator operator++(int)
Definition: QhullSet.h:229
countT lastIndexOf(const T &t) const
Definition: QhullSet.h:406
Definition: qset.h:83
const_iterator(const const_iterator &o)
Definition: QhullSet.h:249
const T operator[](countT idx) const
No error checking.
Definition: QhullSet.h:255
const T::base_type * constData() const
end element is NULL
Definition: QhullSet.h:151
QhullRidge – Qhull&#39;s ridge structure, ridgeT, as a C++ class.
Definition: Coordinates.cpp:21
bool operator>(const iterator &o) const
Definition: QhullSet.h:218
#define SETempty_(set)
Definition: qset.h:419
const_iterator & operator+=(int j)
Definition: QhullSet.h:274
iterator operator-(countT j) const
Definition: QhullSet.h:233
QhullSet< T > & operator=(const QhullSet< T > &other)
Definition: QhullSet.h:120
POD type equivalent to qhT. No virtual members.
Definition: QhullQh.h:58
const T::base_type * i
Definition: QhullSet.h:240
QhullQh * qh() const
Definition: QhullSet.h:71
t
QhullSetBase(QhullQh *qqh, setT *s)
Definition: QhullSet.h:54
bool isEmpty() const
Definition: QhullSet.h:70
T operator[](countT idx) const
No error checking.
Definition: QhullSet.h:210
T value(countT idx) const
Definition: QhullSet.h:359
QhullSet< T >::const_iterator ConstIterator
Definition: QhullSet.h:112
const T::base_type * i
Definition: QhullSet.h:290
Faster then interator/const_iterator due to T::base_type.
Definition: QhullSet.h:283
QhullSetBase()
disabled since memory allocation for QhullSet not defined
Definition: QhullSet.h:62
bool findPrevious(const T &t)
Definition: QhullSet.h:436
const T operator[](countT idx) const
Definition: QhullSet.h:167
bool operator==(const const_iterator &o) const
Definition: QhullSet.h:213
size_t size() const
Definition: QhullSet.h:73
void ** beginPointer() const
Definition: QhullSet.h:77
const T::base_type * begin_i
Definition: QhullSet.h:291
int qh_setindex(setT *set, void *atelem)
Definition: qset.c:821
setelemT e[1]
Definition: qset.h:85
bool operator<(const const_iterator &o) const
Definition: QhullSet.h:264
const T::base_type * end_i
Definition: QhullSet.h:292
bool operator>=(const const_iterator &o) const
No error checking.
Definition: QhullSet.h:265
data
bool operator>(const const_iterator &o) const
Definition: QhullSet.h:262
std::random_access_iterator_tag iterator_category
Definition: QhullSet.h:245
bool hasPrevious() const
Definition: QhullSet.h:310
int qh_setin(setT *set, void *setelem)
Definition: qset.c:795
std::bidirectional_iterator_tag iterator_category
Definition: QhullSet.h:199
countT operator-(const const_iterator &o)
Assumes same point set.
Definition: QhullSet.h:261
iterator & operator-=(countT j)
Definition: QhullSet.h:235
iterator(QhullQh *qqh, typename T::base_type *p)
Definition: QhullSet.h:202
bool operator<(const const_iterator &o) const
Definition: QhullSet.h:224
const T last() const
Definition: QhullSet.h:163
bool contains(const T &t) const
Definition: QhullSet.h:379
const_iterator(QhullQh *qqh, const typename T::base_type *p)
Definition: QhullSet.h:248
countT operator-(const iterator &o) const
Assumes same point set.
Definition: QhullSet.h:217
bool operator<=(const const_iterator &o) const
Definition: QhullSet.h:263
iterator & operator+=(countT j)
Definition: QhullSet.h:234
float value
#define QHULL_ASSERT
Definition: QhullError.h:16
const_iterator constEnd() const
Definition: QhullSet.h:180
const_iterator begin() const
Definition: QhullSet.h:178
void * p
Definition: qset.h:79
const T::base_type * data() const
Definition: QhullSet.h:153
const_iterator & operator-=(int j)
Definition: QhullSet.h:275
bool operator!=(const const_iterator &o) const
Definition: QhullSet.h:214
T operator[](countT idx)
Definition: QhullSet.h:168
const_iterator operator--(int)
Definition: QhullSet.h:271
bool operator==(const QhullSet< T > &other) const
Definition: QhullSet.h:141
T::base_type * endData()
Definition: QhullSet.h:154
const_iterator & operator--()
Definition: QhullSet.h:270
const T front() const
Definition: QhullSet.h:160
iterator end()
Definition: QhullSet.h:181
bool operator<=(const iterator &o) const
Definition: QhullSet.h:219
const_iterator operator+(int j) const
Definition: QhullSet.h:272
void ** elementPointer(countT idx) const
Definition: QhullSet.h:78
void ** qh_setendpointer(setT *set)
Definition: qset.c:566
bool operator<(const iterator &o) const
Definition: QhullSet.h:220
const T back() const
Definition: QhullSet.h:148
T::base_type * data()
Definition: QhullSet.h:152
void defineAs(setT *s)
Not type-safe since setT may contain any type.
Definition: QhullSet.h:67
static bool isEmpty(const setT *s)
Definition: QhullSet.h:86
bool operator!=(const QhullSet< T > &other) const
Definition: QhullSet.h:142
QhullSet< T >::iterator Iterator
Definition: QhullSet.h:110
setT ** referenceSetT()
Definition: QhullSet.h:72
setT * getSetT() const
Definition: QhullSet.h:69
void ** endPointer() const
Always points to 0.
Definition: QhullSet.h:80
bool operator==(const iterator &o) const
Definition: QhullSet.h:211
#define SETelem_(set, n)
Definition: qset.h:331
QhullSetBase & operator=(const QhullSetBase &other)
Definition: QhullSet.h:57
bool findNext(const T &t)
Definition: QhullSet.h:423
iterator & operator=(const iterator &o)
Definition: QhullSet.h:204
QhullSetBase(const QhullSetBase &other)
Copy constructor copies the pointer but not the set. Needed for return by value and parameter passing...
Definition: QhullSet.h:56
const_iterator constBegin() const
Definition: QhullSet.h:179
bool operator!=(const const_iterator &o) const
Definition: QhullSet.h:258
bool operator!=(const iterator &o) const
Definition: QhullSet.h:212
const_iterator operator++(int)
Definition: QhullSet.h:269
const_iterator & operator=(const const_iterator &o)
Definition: QhullSet.h:251
const_iterator operator-(int j) const
Definition: QhullSet.h:273
const T at(countT idx) const
Definition: QhullSet.h:146
bool operator<=(const const_iterator &o) const
Definition: QhullSet.h:223
const_iterator & operator++()
Definition: QhullSet.h:268
iterator & operator++()
No error checking.
Definition: QhullSet.h:228
const T first() const
Definition: QhullSet.h:157
countT count() const
Definition: QhullSet.h:66
static setT s_empty_set
Definition: QhullSet.h:49
int countT
Definition: user_r.h:182
bool operator>(const const_iterator &o) const
Definition: QhullSet.h:222
int qh_setequal(setT *setA, setT *setB)
Definition: qset.c:587
const_iterator end() const
Definition: QhullSet.h:182
QhullSetIterator< T > & operator=(const QhullSetIterator< T > &o)
Definition: QhullSet.h:299
iterator operator+(countT j) const
Definition: QhullSet.h:232
bool operator>=(const const_iterator &o) const
Definition: QhullSet.h:225


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:02