File: steppingiterator.hpp

package info (click to toggle)
quantlib 1.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,768 kB
  • sloc: cpp: 398,987; makefile: 6,574; python: 214; sh: 150; lisp: 86
file content (178 lines) | stat: -rw-r--r-- 6,039 bytes parent folder | download
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2000, 2001, 2002, 2003 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
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <https://www.quantlib.org/license.shtml>.

 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
*/

#ifndef quantlib_stepping_iterator_hpp
#define quantlib_stepping_iterator_hpp

#include <ql/errors.hpp>
#include <ql/types.hpp>
#include <iterator>
#include <type_traits>

namespace QuantLib {

    //! 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 a positive
        integer given upon construction.
    */
#ifdef __cpp_concepts
    template <std::random_access_iterator Iterator>
#else
    template <class Iterator>
#endif
    class step_iterator {  // NOLINT(cppcoreguidelines-special-member-functions)
      private:
        Iterator base_{};
        // a Size would mess up integer division in distance_to
        BigInteger step_{};

      public:
        using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;
        using difference_type = typename std::iterator_traits<Iterator>::difference_type;
        using value_type = typename std::iterator_traits<Iterator>::value_type;
        using pointer = typename std::iterator_traits<Iterator>::pointer;
        using reference = typename std::iterator_traits<Iterator>::reference;

        step_iterator() = default;

        explicit step_iterator(const Iterator& base, Size step)
        : base_(base), step_(static_cast<BigInteger>(step)) {}

        template <class OtherIterator>
        step_iterator(const step_iterator<OtherIterator>& i,
                      std::enable_if_t<std::is_convertible_v
                      <OtherIterator, Iterator>>* = nullptr)
        : base_(i.base_), step_(static_cast<BigInteger>(i.step())) {}

        Size step() const { return static_cast<Size>(this->step_); }

        step_iterator& operator=(const step_iterator& other) = default;

        step_iterator& operator++() {
            base_ += step_;
            return *this;
        }

        step_iterator operator++(int) {
            auto tmp = *this;
            base_ += step_;
            return tmp;
        }

        reference operator*() const {
            return *base_;
        }

        step_iterator& operator--() {
            base_ -= step_;
            return *this;
        }

        step_iterator operator--(int) {
            auto tmp = *this;
            base_ -= step_;
            return tmp;
        }

        step_iterator& operator+=(Size n) {
            base_ += n * step_;
            return *this;
        }

        step_iterator& operator-=(Size n) {
            base_ -= n * step_;
            return *this;
        }

        reference operator[](Size n) const {
            return *(base_ + n * step_);
        }

        friend step_iterator operator+(const step_iterator& i, Size n) {
            return step_iterator(i.base_ + n * i.step_, i.step_);
        }

        friend step_iterator operator+(Size n, const step_iterator& i) {
            return step_iterator(i.base_ + n * i.step_, i.step_);
        }

        friend step_iterator operator-(const step_iterator& i, Size n) {
            return step_iterator(i.base_ - n * i.step_, i.step_);
        }

        friend difference_type operator-(const step_iterator& lhs, const step_iterator& rhs) {
#ifdef QL_EXTRA_SAFETY_CHECKS
            QL_REQUIRE(lhs.step_ == rhs.step_, "step_iterators with different step cannot be added or subtracted");
#endif
            return (lhs.base_ - rhs.base_) / lhs.step_;
        }

        friend bool operator==(const step_iterator& lhs, const step_iterator& rhs) {
            return lhs.base_ == rhs.base_ && lhs.step_ == rhs.step_;
        }

        friend bool operator!=(const step_iterator& lhs, const step_iterator& rhs) {
            return lhs.base_ != rhs.base_ || lhs.step_ != rhs.step_;
        }

        friend bool operator<(const step_iterator& lhs, const step_iterator& rhs) {
#ifdef QL_EXTRA_SAFETY_CHECKS
            QL_REQUIRE(lhs.step_ == rhs.step_, "step_iterators with different step cannot be compared");
#endif
            return lhs.base_ < rhs.base_;
        }

        friend bool operator>(const step_iterator& lhs, const step_iterator& rhs) {
#ifdef QL_EXTRA_SAFETY_CHECKS
            QL_REQUIRE(lhs.step_ == rhs.step_, "step_iterators with different step cannot be compared");
#endif
            return lhs.base_ > rhs.base_;
        }

        friend bool operator<=(const step_iterator& lhs, const step_iterator& rhs) {
#ifdef QL_EXTRA_SAFETY_CHECKS
            QL_REQUIRE(lhs.step_ == rhs.step_, "step_iterators with different step cannot be compared");
#endif
            return lhs.base_ <= rhs.base_;
        }

        friend bool operator>=(const step_iterator& lhs, const step_iterator& rhs) {
#ifdef QL_EXTRA_SAFETY_CHECKS
            QL_REQUIRE(lhs.step_ == rhs.step_, "step_iterators with different step cannot be compared");
#endif
            return lhs.base_ >= rhs.base_;
        }
    };

    //! helper function to create step iterators
    /*! \relates step_iterator */
    template <class Iterator>
    step_iterator<Iterator> make_step_iterator(Iterator it, Size step) {
        return step_iterator<Iterator>(it,step);
    }

}


#endif