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 246 247 248 249 250 251 252 253 254
|
/*
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 steppingiterator.hpp
\brief Iterator advancing in constant steps
\fullpath
ql/Utilities/%steppingiterator.hpp
*/
// $Id: steppingiterator.hpp,v 1.4 2002/01/16 14:40:56 nando Exp $
#ifndef quantlib_stepping_iterator_h
#define quantlib_stepping_iterator_h
#include <iterator>
namespace QuantLib {
namespace Utilities {
//! Iterator advancing in constant steps
/*! This iterator advances an underlying random access
iterator in steps of \f$ n \f$ positions, where
\f$ n \f$ is an integer given upon construction.
*/
template <class RandomAccessIterator>
class stepping_iterator : public QL_ITERATOR<
std::random_access_iterator_tag,
typename QL_ITERATOR_TRAITS<RandomAccessIterator>::value_type,
typename QL_ITERATOR_TRAITS<
RandomAccessIterator>::difference_type,
typename QL_ITERATOR_TRAITS<RandomAccessIterator>::pointer,
typename QL_ITERATOR_TRAITS<RandomAccessIterator>::reference>
{
public:
/* These typedefs are needed even though inherited from
QL_ITERATOR (see 14.6.2.3 of the standard). */
typedef typename QL_ITERATOR_TRAITS<
RandomAccessIterator>::difference_type difference_type;
typedef typename QL_ITERATOR_TRAITS<
RandomAccessIterator>::pointer pointer;
typedef typename QL_ITERATOR_TRAITS<
RandomAccessIterator>::reference reference;
stepping_iterator(const RandomAccessIterator&,
difference_type step);
//! \name Dereferencing
//@{
reference operator*() const;
pointer operator->() const;
//@}
//! \name Random access
//@{
reference operator[](int ) const;
//@}
//! \name Increment and decrement
//@{
stepping_iterator& operator++();
stepping_iterator operator++(int );
stepping_iterator& operator--();
stepping_iterator operator--(int );
stepping_iterator& operator+=(difference_type);
stepping_iterator& operator-=(difference_type);
stepping_iterator<RandomAccessIterator> operator+(difference_type);
stepping_iterator<RandomAccessIterator> operator-(difference_type);
//@}
//! \name Difference
//@{
difference_type operator-(
const stepping_iterator<RandomAccessIterator>&);
//@}
//! \name Comparisons
//@{
bool operator==(const stepping_iterator<RandomAccessIterator>&);
bool operator!=(const stepping_iterator<RandomAccessIterator>&);
bool operator< (const stepping_iterator<RandomAccessIterator>&);
bool operator> (const stepping_iterator<RandomAccessIterator>&);
bool operator<=(const stepping_iterator<RandomAccessIterator>&);
bool operator>=(const stepping_iterator<RandomAccessIterator>&);
//@}
private:
difference_type dn_;
RandomAccessIterator it_;
};
//! helper function to create stepping iterators
/*! \relates stepping_iterator */
template <class Iterator>
stepping_iterator<Iterator>
make_stepping_iterator(Iterator it,
typename stepping_iterator<Iterator>::difference_type step);
// inline definitions
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>::stepping_iterator(
const RandomAccessIterator& it,
stepping_iterator<RandomAccessIterator>::difference_type step)
: dn_(step), it_(it) {}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>&
stepping_iterator<RandomAccessIterator>::operator++() {
it_ += dn_;
return *this;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>
stepping_iterator<RandomAccessIterator>::operator++(int ) {
stepping_iterator<RandomAccessIterator> temp = *this;
it_ += dn_;
return temp;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>&
stepping_iterator<RandomAccessIterator>::operator--() {
it_ -= dn_;
return *this;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>
stepping_iterator<RandomAccessIterator>::operator--(int ) {
stepping_iterator<RandomAccessIterator> temp = *this;
it_ -= dn_;
return temp;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>&
stepping_iterator<RandomAccessIterator>::operator+=(
stepping_iterator<RandomAccessIterator>::difference_type i) {
it_ += i*dn_;
return *this;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>&
stepping_iterator<RandomAccessIterator>::operator-=(
stepping_iterator<RandomAccessIterator>::difference_type i) {
it_ -= i*dn_;
return *this;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>::reference
stepping_iterator<RandomAccessIterator>::operator*() const {
return *it_;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>::pointer
stepping_iterator<RandomAccessIterator>::operator->() const {
return it_;
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>::reference
stepping_iterator<RandomAccessIterator>::operator[](int i) const {
return it_[i*dn_];
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>
stepping_iterator<RandomAccessIterator>::operator+(
stepping_iterator<RandomAccessIterator>::difference_type i) {
return stepping_iterator<RandomAccessIterator>(it_+dn_*i,dn_);
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>
stepping_iterator<RandomAccessIterator>::operator-(
stepping_iterator<RandomAccessIterator>::difference_type i) {
return stepping_iterator<RandomAccessIterator>(it_-dn_*i,dn_);
}
template<class RandomAccessIterator>
inline stepping_iterator<RandomAccessIterator>::difference_type
stepping_iterator<RandomAccessIterator>::operator-(
const stepping_iterator<RandomAccessIterator>& i) {
#ifdef QL_DEBUG
QL_REQUIRE((it_-i.it_)%dn_ == 0,
"Cannot subtract stepping iterators not reachable "
"from each other");
#endif
return (it_-i.it_)/dn_;
}
template<class RandomAccessIterator>
inline bool stepping_iterator<RandomAccessIterator>::operator==(
const stepping_iterator<RandomAccessIterator>& i) {
return (it_ == i.it_);
}
template<class RandomAccessIterator>
inline bool stepping_iterator<RandomAccessIterator>::operator!=(
const stepping_iterator<RandomAccessIterator>& i) {
return (it_ != i.it_);
}
template<class RandomAccessIterator>
inline bool stepping_iterator<RandomAccessIterator>::operator<(
const stepping_iterator<RandomAccessIterator>& i) {
return (it_ < i.it_);
}
template<class RandomAccessIterator>
inline bool stepping_iterator<RandomAccessIterator>::operator>(
const stepping_iterator<RandomAccessIterator>& i) {
return (it_ > i.it_);
}
template<class RandomAccessIterator>
inline bool stepping_iterator<RandomAccessIterator>::operator<=(
const stepping_iterator<RandomAccessIterator>& i) {
return (it_ <= i.it_);
}
template<class RandomAccessIterator>
inline bool stepping_iterator<RandomAccessIterator>::operator>=(
const stepping_iterator<RandomAccessIterator>& i) {
return (it_ >= i.it_);
}
template <class Iterator>
inline stepping_iterator<Iterator>
make_stepping_iterator(Iterator it,
typename stepping_iterator<Iterator>::difference_type step) {
return stepping_iterator<Iterator>(it,step);
}
}
}
#endif
|