00001
00002
00003
00004
00005
00006
00007
00008 #ifndef IMPALGEBRA_GRID_RANGE_D_H
00009 #define IMPALGEBRA_GRID_RANGE_D_H
00010
00011 #include "VectorD.h"
00012 #include <IMP/RefCounted.h>
00013 #include <IMP/Pointer.h>
00014 #include <boost/range.hpp>
00015
00016 IMPALGEBRA_BEGIN_NAMESPACE
00017
00018 #ifndef IMP_DOXYGEN
00019 namespace {
00020 template <unsigned int D>
00021 struct GridRangeData: public RefCounted {
00022 VectorD<D> min,max;
00023 double step;
00024 GridRangeData(const VectorD<D> &mn,
00025 const VectorD<D> &mx,
00026 double stp): min(mn), max(mx), step(stp){}
00027 };
00028
00029 template <unsigned int D>
00030 std::ostream &operator<<(std::ostream &out, const GridRangeData<D> &d) {
00031 out << d.min << " " << d.max << " " << d.step << std::endl;
00032 return out;
00033 }
00034 }
00035 #endif
00036
00037 template <unsigned int D>
00038 class GridIteratorD
00039 {
00040 Pointer<GridRangeData<D> > data_;
00041 VectorD<D> cur_;
00042 public:
00043 typedef GridIteratorD<D> This;
00044 typedef const VectorD<D> value_type;
00045 typedef unsigned int difference_type;
00046 typedef const VectorD<D>& reference;
00047 typedef const VectorD<D>* pointer;
00048 typedef std::forward_iterator_tag iterator_category;
00049
00050 GridIteratorD(Pointer<GridRangeData<D> > d, reference cur):
00051 data_(d), cur_(cur) {
00052 }
00053 reference operator*() const {
00054 return cur_;
00055 }
00056 pointer operator->() const {
00057 return &cur_;
00058 }
00059 const This& operator++() {
00060 for (unsigned int i=0; i< D; ++i) {
00061 cur_[i]+= data_->step;
00062 if (cur_[i] > data_->max[i]) {
00063 cur_[i]= data_->min[i];
00064 } else {
00065 return *this;
00066 }
00067 }
00068 cur_= data_->max;
00069 return *this;
00070 }
00071
00072 This operator++(int) {
00073 This ret= *this;
00074 this->operator++();
00075 return ret;
00076 }
00077
00078 bool operator==(const This &o) const {
00079 return compare(cur_, o.cur_) ==0;
00080 }
00081 bool operator!=(const This &o) const {
00082 return compare(cur_, o.cur_) !=0;
00083 }
00084 bool operator<(const This &o) const {
00085 return compare(cur_, o.cur_) <0;
00086 }
00087 bool operator>(const This &o) const {
00088 return compare(cur_, o.cur_) >0;
00089 }
00090 };
00091
00092 #ifndef IMP_DOXYGEN
00093 template <unsigned int D>
00094 std::ostream &operator<<(std::ostream &out, const GridIteratorD<D> &v) {
00095 v.show(out);
00096 return out;
00097 }
00098 #endif
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 template <int D>
00110 class GridRangeD {
00111 private:
00112 IMP::internal::OwnerPointer<GridRangeData<D> > data_;
00113 public:
00114 typedef GridIteratorD<D> iterator;
00115 typedef iterator const_iterator;
00116
00117 GridRangeD(const VectorD<D>& min, const VectorD<D>& max, double step):
00118 data_(new GridRangeData<D>(min, max, step)){}
00119 const_iterator begin() const {
00120 return iterator(data_, data_->min);
00121 }
00122 const_iterator end() const {
00123 return iterator(data_, data_->max);
00124 }
00125 };
00126
00127
00128
00129 IMPALGEBRA_END_NAMESPACE
00130
00131 #endif