1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/*
Copyright (C) 2000, 2001, 2002 RiskMap srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email ferdinando@ametrano.net
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file combiningiterator.hpp
\brief Iterator mapping a function to a set of underlying sequences
\fullpath
ql/Utilities/%combiningiterator.hpp
*/
// $Id: combiningiterator.hpp,v 1.5 2002/02/27 08:16:28 lballabio Exp $
#ifndef quantlib_combining_iterator_h
#define quantlib_combining_iterator_h
#include <iterator>
namespace QuantLib {
namespace Utilities {
//! Iterator mapping a function to a set of underlying sequences
/*! This iterator advances a set of underlying iterators and
returns the values obtained by applying a function to the
sets of values such iterators point to.
This class was implemented based on Christopher Baus and Thomas
Becker, <i>Custom Iterators for the STL</i>, included in the
proceedings of the First Workshop on C++ Template Programming,
Erfurt, Germany, 2000 (http://www.oonumerics.org/tmpw00/)
*/
template <class Iterator, class Function>
class combining_iterator : public QL_ITERATOR<
typename QL_ITERATOR_TRAITS<Iterator>::iterator_category,
typename Function::result_type,
typename QL_ITERATOR_TRAITS<Iterator>::difference_type,
const typename Function::result_type*,
const typename Function::result_type&>
{
public:
/* These typedefs are needed even though inherited from
QL_ITERATOR (see 14.6.2.3 of the standard). */
typedef typename Function::result_type value_type;
typedef typename QL_ITERATOR_TRAITS<Iterator>::difference_type
difference_type;
typedef const typename Function::result_type* pointer;
typedef const typename Function::result_type& reference;
// construct a combining iterator from a collection of iterators
template <class IteratorCollectionIterator>
combining_iterator(IteratorCollectionIterator it1,
IteratorCollectionIterator it2, Function f)
: iteratorVector_(it1,it2), f_(f) {}
//! \name Dereferencing
//@{
reference operator*() const;
pointer operator->() const;
//@}
//! \name Random access
//@{
value_type operator[](difference_type n) const;
//@}
//! \name Increment and decrement
//@{
combining_iterator& operator++();
combining_iterator operator++(int );
combining_iterator& operator--();
combining_iterator operator--(int );
combining_iterator& operator+=(difference_type n);
combining_iterator& operator-=(difference_type n);
combining_iterator operator+ (difference_type n) const;
combining_iterator operator- (difference_type n) const;
//@}
//! \name Difference
//@{
difference_type operator-(
const combining_iterator<Iterator,Function>& rhs ) const;
//@}
//! \name Comparisons
//@{
bool operator==(
const combining_iterator<Iterator,Function>& rhs) const;
bool operator !=(
const combining_iterator<Iterator,Function>& rhs) const;
//@}
private:
std::vector<Iterator> iteratorVector_;
Function f_;
mutable value_type x_;
};
//! helper function to create combining iterators
/*! \relates combining_iterator */
template <class It, class Function>
combining_iterator<typename QL_ITERATOR_TRAITS<It>::value_type,
Function>
make_combining_iterator(It it1, It it2, Function f);
// inline definitions
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>&
combining_iterator<Iterator,Function>::operator++() {
typename std::vector<Iterator>::iterator it =
iteratorVector_.begin();
while (it != iteratorVector_.end()) { ++*it; ++it; }
return *this;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>
combining_iterator<Iterator,Function>::operator++(int ) {
combining_iterator<Iterator,Function> tmp = *this;
++*this;
return tmp;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>&
combining_iterator<Iterator,Function>::operator--() {
typename std::vector<Iterator>::iterator it =
iteratorVector_.begin();
while (it != iteratorVector_.end()){ --*it; ++it; }
return *this;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>
combining_iterator<Iterator,Function>::operator--(int ) {
combining_iterator<Iterator,Function> temp = *this;
--*this;
return temp;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>&
combining_iterator<Iterator,Function>::operator+=(
combining_iterator<Iterator,Function>::difference_type n) {
typename std::vector<Iterator>::iterator it =
iteratorVector_.begin();
while ( it != iteratorVector_.end()){ *it +=n ; ++it ; }
return *this;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>&
combining_iterator<Iterator,Function>::operator-=(
combining_iterator<Iterator,Function>::difference_type n) {
typename std::vector<Iterator>::iterator it =
iteratorVector_.begin();
while ( it != iteratorVector_.end()){ *it -=n ; ++it ; }
return *this;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>::reference
combining_iterator<Iterator,Function>::operator*() const {
x_ = f_(iteratorVector_.begin(), iteratorVector_.end());
return x_;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>::pointer
combining_iterator<Iterator,Function>::operator->() const {
x_ = f_(iteratorVector_.begin(), iteratorVector_.end());
return &x_;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>::value_type
combining_iterator<Iterator,Function>::operator[](
difference_type n) const {
return *(*this+n) ;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>
combining_iterator<Iterator,Function>::operator+(
combining_iterator<Iterator,Function>::difference_type n) const {
combining_iterator<Iterator,Function> tmp(*this);
tmp += n;
return tmp;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>
combining_iterator<Iterator,Function>::operator-(
combining_iterator<Iterator,Function>::difference_type n) const {
combining_iterator<Iterator,Function> tmp(*this);
tmp -= n;
return tmp;
}
template <class Iterator, class Function>
inline combining_iterator<Iterator,Function>::difference_type
combining_iterator<Iterator,Function>::operator-(
const combining_iterator<Iterator,Function>& rhs) const {
if (iteratorVector_.size()>0 && rhs.iteratorVector_.size()>0)
return iteratorVector_[0] - rhs.iteratorVector_[0];
else
return 0;
}
template <class Iterator, class Function>
inline bool combining_iterator<Iterator,Function>::operator==(
const combining_iterator<Iterator,Function>& rhs) const {
return iteratorVector_ == rhs.iteratorVector_;
}
template <class Iterator, class Function>
inline bool combining_iterator<Iterator,Function>::operator!=(
const combining_iterator<Iterator,Function>& rhs) const {
return iteratorVector_ != rhs.iteratorVector_;
}
template <class It, class Function>
inline combining_iterator<
typename QL_ITERATOR_TRAITS<It>::value_type, Function>
make_combining_iterator(It it1, It it2, Function f) {
typedef QL_ITERATOR_TRAITS<It>::value_type Iterator;
return combining_iterator<Iterator,Function>(it1,it2,f);
}
}
}
#endif
|