1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* 4 Copyright (C) 2008 Andreas Gaida 5 Copyright (C) 2008 Ralph Schreyer 6 Copyright (C) 2008 Klaus Spanderen 7 8 This file is part of QuantLib, a free-software/open-source library 9 for financial quantitative analysts and developers - http://quantlib.org/ 10 11 QuantLib is free software: you can redistribute it and/or modify it 12 under the terms of the QuantLib license. You should have received a 13 copy of the license along with this program; if not, please email 14 <quantlib-dev@lists.sf.net>. The license is also available online at 15 <http://quantlib.org/license.shtml>. 16 17 This program is distributed in the hope that it will be useful, but WITHOUT 18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 19 FOR A PARTICULAR PURPOSE. See the license for more details. 20 */ 21 22 /*! \file fdmlinearopiterator.hpp 23 \brief iterator for a linear fdm operator 24 */ 25 26 #ifndef quantlib_linear_op_iterator_hpp 27 #define quantlib_linear_op_iterator_hpp 28 29 #include <ql/types.hpp> 30 #include <ql/utilities/disposable.hpp> 31 #include <vector> 32 #include <numeric> 33 34 namespace QuantLib { 35 36 class FdmLinearOpIterator { 37 public: FdmLinearOpIterator(Size index=0)38 explicit FdmLinearOpIterator(Size index = 0) 39 : index_(index) {} 40 FdmLinearOpIterator(const std::vector<Size> & dim)41 explicit FdmLinearOpIterator(const std::vector<Size>& dim) 42 : index_(0), 43 dim_(dim), 44 coordinates_(dim.size(), 0) {} 45 FdmLinearOpIterator(const std::vector<Size> & dim,const std::vector<Size> & coordinates,Size index)46 FdmLinearOpIterator(const std::vector<Size>& dim, 47 const std::vector<Size>& coordinates, Size index) 48 : index_(index), 49 dim_(dim), 50 coordinates_(coordinates) {} 51 FdmLinearOpIterator(const Disposable<FdmLinearOpIterator> & from)52 FdmLinearOpIterator( 53 const Disposable<FdmLinearOpIterator> & from) { 54 swap(const_cast<Disposable<FdmLinearOpIterator> & >(from)); 55 } 56 operator ++()57 void operator++() { 58 ++index_; 59 for (Size i=0; i < dim_.size(); ++i) { 60 if (++coordinates_[i] == dim_[i]) { 61 coordinates_[i] = 0; 62 } 63 else { 64 break; 65 } 66 } 67 } 68 operator !=(const FdmLinearOpIterator & iterator) const69 bool operator!=(const FdmLinearOpIterator& iterator) const { 70 return index_ != iterator.index_; 71 } 72 index() const73 Size index() const { 74 return index_; 75 } 76 coordinates() const77 const std::vector<Size> & coordinates() const { 78 return coordinates_; 79 } 80 swap(FdmLinearOpIterator & iter)81 void swap(FdmLinearOpIterator& iter) { 82 std::swap(iter.index_, index_); 83 dim_.swap(iter.dim_); 84 coordinates_.swap(iter.coordinates_); 85 } 86 87 private: 88 Size index_; 89 std::vector<Size> dim_; 90 std::vector<Size> coordinates_; 91 }; 92 } 93 94 #endif 95