PlayaBlockIteratorImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
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   /* If we're at the end of this level, drop back a level */
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 /* go to the start of the next block */
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

Site Contact