Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef PLAYA_BLOCKITERATORIMPL_HPP
00043 #define PLAYA_BLOCKITERATORIMPL_HPP
00044
00045
00046 #include "PlayaBlockIteratorDecl.hpp"
00047 #include "PlayaVectorSpaceDecl.hpp"
00048 #include "PlayaExceptions.hpp"
00049
00050 namespace Playa
00051 {
00052
00053 template <class Scalar> inline
00054 bool BlockIterator<Scalar>
00055 ::operator==(const BlockIterator<Scalar>& other) const
00056 {
00057 if (debug())
00058 {
00059 Out::os() << "comparing: LHS=" << *this
00060 << ", RHS=" << other << std::endl;
00061 }
00062 if (this->atEnd_ && other.atEnd_) return true;
00063 if (this->atEnd_ != other.atEnd_) return false;
00064 if (this->index_.size() != other.index_.size()) return false;
00065
00066 for (unsigned int i=0; i<this->index_.size(); i++)
00067 {
00068 if (this->index_[i] != other.index_[i]) return false;
00069 }
00070
00071 return true;
00072 }
00073
00074 template <class Scalar> inline
00075 bool BlockIterator<Scalar>
00076 ::operator<(const BlockIterator<Scalar>& other) const
00077 {
00078 if (debug())
00079 {
00080 Out::os() << "comparing (<): LHS=" << *this
00081 << ", RHS=" << other << std::endl;
00082 }
00083 TEUCHOS_TEST_FOR_EXCEPTION(this->space() != other.space(),
00084 RuntimeError, "Attempt to compare block iterators attached to "
00085 "two different spaces");
00086
00087 if (!this->atEnd_ && other.atEnd_) return true;
00088 if (this->atEnd_ && !other.atEnd_) return false;
00089 if (this->atEnd_ && other.atEnd_) return false;
00090
00091 int d = std::min(this->index_.size(), other.index_.size());
00092
00093
00094 for (int i=0; i<d; i++)
00095 {
00096 if (this->index_[i] < other.index_[i]) return true;
00097 if (this->index_[i] > other.index_[i]) return false;
00098 }
00099
00100 return false;
00101 }
00102
00103
00104
00105 template <class Scalar> inline
00106 BlockIterator<Scalar> BlockIterator<Scalar>::operator++(int)
00107 {
00108 if (debug())
00109 {
00110 Out::os() << "iter++" << std::endl;
00111 }
00112 BlockIterator<Scalar> old = *this;
00113
00114 TEUCHOS_TEST_FOR_EXCEPTION(this->atEnd_, RuntimeError,
00115 "attempt to advance a BlockIterator beyond end");
00116
00117 int depth = this->index_.size();
00118 TEUCHOS_TEST_FOR_EXCEPTION(depth <= 0, RuntimeError,
00119 "empty index stack in BlockIterator");
00120
00121 atEnd_ = !advance(depth-1);
00122
00123 if (debug())
00124 {
00125 Out::os() << "old=" << old << ", new=" << *this << std::endl;
00126 }
00127 return old;
00128 }
00129
00130 template <class Scalar> inline
00131 bool BlockIterator<Scalar>::advance(int level)
00132 {
00133 if (level < 0) return false;
00134
00135 this->index_[level]++;
00136 std::deque<int> base = this->index_;
00137 base.pop_back();
00138
00139 if (this->index_[level] >= this->space().getBlock(base).numBlocks())
00140 {
00141 this->index_.pop_back();
00142 return this->advance(level-1);
00143 }
00144 else
00145 {
00146 goToStart(this->space().getBlock(*this), this->index_);
00147 }
00148 return true;
00149 }
00150
00151 template <class Scalar> inline
00152 void BlockIterator<Scalar>
00153 ::goToStart(const VectorSpace<Scalar>& space,
00154 std::deque<int>& pos) const
00155 {
00156 pos.push_back(0);
00157 if (space.isBlockSpace())
00158 {
00159 goToStart(space.getBlock(0), pos);
00160 }
00161 }
00162
00163
00164 template <class Scalar> inline
00165 const VectorSpace<Scalar>& BlockIterator<Scalar>::space() const
00166 {
00167 return *(this->space_);
00168 }
00169
00170
00171 template <class Scalar> inline
00172 BlockIterator<Scalar>::BlockIterator(
00173 const VectorSpace<Scalar>& space,
00174 bool atEnd
00175 )
00176 : space_(rcp(new VectorSpace<Scalar>(space))),
00177 index_(),
00178 atEnd_(atEnd)
00179 {
00180 if (!atEnd)
00181 {
00182 goToStart(space, index_);
00183 }
00184 }
00185
00186
00187 template <class Scalar> inline
00188 std::ostream& BlockIterator<Scalar>::print(std::ostream& os) const
00189 {
00190 os << "BlockIterator";
00191 if (this->atEnd_)
00192 {
00193 os << "[end]";
00194 }
00195 else
00196 {
00197 os << "[index={";
00198 for (unsigned int i=0; i<index_.size(); i++)
00199 {
00200 if (i>0U) os << ", ";
00201 os << index_[i];
00202 }
00203 os << "}]";
00204 }
00205 return os;
00206 }
00207
00208
00209
00210 }
00211
00212
00213
00214
00215
00216 #endif