TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
dressed_iterator.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2013-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2023 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./first_include.hpp"
28
29#include <boost/iterator/iterator_facade.hpp>
30#include <itertools/itertools.hpp>
31
32#include <iterator>
33#include <type_traits>
34
35namespace triqs::utility {
36
37 // Forward declaration.
38 template <typename IteratorType, typename Dressing, typename DressingAuxiliaryArgumentPtrType = void> struct dressed_iterator;
39
40 namespace detail {
41
42 template <typename IteratorType>
43 constexpr bool is_bidir = std::is_same_v<typename IteratorType::iterator_category, std::bidirectional_iterator_tag>;
44
45 } // namespace detail
46
51
68 template <typename IteratorType, typename Dressing, typename DressingAuxiliaryArgumentPtrType>
70 : public boost::iterator_facade<
71 dressed_iterator<IteratorType, Dressing, DressingAuxiliaryArgumentPtrType>, Dressing,
72 std::conditional_t<detail::is_bidir<IteratorType>, boost::bidirectional_traversal_tag, boost::forward_traversal_tag>, Dressing> {
73 public:
75 dressed_iterator() = default;
76
83 template <typename T> dressed_iterator(T const &it, DressingAuxiliaryArgumentPtrType *aux) : _it(it), _aux(aux) {}
84
85 // Special member functions (should be removed probably).
86 dressed_iterator(dressed_iterator const &it) = default;
87 dressed_iterator(dressed_iterator &&it) = default;
88 dressed_iterator &operator=(dressed_iterator const &it) = default;
89 dressed_iterator &operator=(dressed_iterator &&it) noexcept {
90 using std::swap;
91 swap(it._it, this->_it);
92 swap(this->_aux, it._aux);
93 return *this;
94 }
95
100 IteratorType const &get() const { return _it; }
101
106 IteratorType &get() { return _it; }
107
109 operator IteratorType() const { return _it; }
110
115 auto *get_aux() { return _aux; }
116
121 const auto *get_aux() const { return _aux; }
122
130 template <typename OtherSentinel> bool operator==(itertools::sentinel_t<OtherSentinel> other) { return _it == other.it; }
131
132 private:
133 // Core operations required by boost::iterator_facade; iterator_core_access lets the facade call them while private.
134 // increment/equal/dereference implement forward traversal, decrement adds bidirectional traversal.
135 friend class boost::iterator_core_access;
136 void increment() { ++_it; }
137 void decrement()
138 requires(detail::is_bidir<IteratorType>)
139 {
140 --_it;
141 }
142 bool equal(dressed_iterator const &other) const { return (other._it == _it); }
143 Dressing dereference() const { return Dressing(_it, _aux); }
144
145 private:
146 IteratorType _it;
147 DressingAuxiliaryArgumentPtrType *_aux;
148 };
149
156 template <typename IteratorType, typename Dressing>
157 struct dressed_iterator<IteratorType, Dressing, void>
158 : public boost::iterator_facade<
159 dressed_iterator<IteratorType, Dressing>, Dressing,
160 std::conditional_t<detail::is_bidir<IteratorType>, boost::bidirectional_traversal_tag, boost::forward_traversal_tag>, Dressing> {
161 public:
163 dressed_iterator() = default;
164
169 template <typename T> dressed_iterator(T const &it) : _it(it) {}
170
171 // Special member functions (should be removed probably).
172 dressed_iterator(dressed_iterator const &it) = default;
173 dressed_iterator(dressed_iterator &&it) = default;
174 dressed_iterator &operator=(dressed_iterator const &it) = default;
175 dressed_iterator &operator=(dressed_iterator &&it) noexcept {
176 using std::swap;
177 swap(it._it, this->_it);
178 return *this;
179 }
180
185 IteratorType const &get() const { return _it; }
186
191 IteratorType &get() { return _it; }
192
194 operator IteratorType() const { return _it; }
195
203 template <typename OtherSentinel> bool operator==(itertools::sentinel_t<OtherSentinel> other) { return _it == other.it; }
204
205 private:
206 // Core operations required by boost::iterator_facade; iterator_core_access lets the facade call them while private.
207 // increment/equal/dereference implement forward traversal, decrement adds bidirectional traversal.
208 friend class boost::iterator_core_access;
209 void increment() { ++_it; }
210 void decrement()
211 requires(detail::is_bidir<IteratorType>)
212 {
213 --_it;
214 }
215 bool equal(dressed_iterator const &other) const { return (other._it == _it); }
216 Dressing dereference() const { return Dressing(_it); }
217
218 private:
219 IteratorType _it;
220 };
221
223
224} // namespace triqs::utility
Compiler / platform glue and the dcomplex alias (must be included before any Boost header).
IteratorType const & get() const
Access the underlying iterator.
dressed_iterator(T const &it)
Construct a dressed iterator from an underlying iterator.
bool operator==(itertools::sentinel_t< OtherSentinel > other)
Equal-to operator to compare a itertools::sentinel_t type with a dressed iterator.
IteratorType & get()
Access the underlying iterator.
dressed_iterator()=default
Default constructor leaves the underlying iterator uninitialized.
STL-compatible iterator that wraps an underlying iterator and dereferences to a user-supplied dressin...
IteratorType & get()
Access the underlying iterator.
dressed_iterator(T const &it, DressingAuxiliaryArgumentPtrType *aux)
Construct a dressed iterator from an underlying iterator and an auxiliary pointer.
const auto * get_aux() const
Access the auxiliary pointer.
IteratorType const & get() const
Access the underlying iterator.
auto * get_aux()
Access the auxiliary pointer.
dressed_iterator()=default
Default constructor leaves the underlying iterator and auxiliary pointer uninitialized.
bool operator==(itertools::sentinel_t< OtherSentinel > other)
Equal-to operator to compare a itertools::sentinel_t type with a dressed iterator.