TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
mesh_iterator.hpp
Go to the documentation of this file.
1// Copyright (c) 2016-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2016-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: Thomas Ayral, Philipp Dumitrescu, Michel Ferrero, Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "../utility/macros.hpp"
28
29#include <compare>
30#include <cstddef>
31#include <iterator>
32
33namespace triqs::mesh {
34
44 template <typename M> struct C2PY_IGNORE mesh_iterator {
46 using value_type = typename M::mesh_point_t;
47
49 using iterator_category = std::random_access_iterator_tag;
50
53
55 using difference_type = std::ptrdiff_t;
56
58 using reference = value_type const &;
59
61 M const *mesh_ptr{nullptr};
62
64 long data_index{-1};
65
71 ++data_index;
72 return *this;
73 }
74
79 mesh_iterator operator++(int) noexcept {
80 mesh_iterator tmp = *this;
81 ++data_index;
82 return tmp;
83 }
84
90 --data_index;
91 return *this;
92 }
93
98 mesh_iterator operator--(int) noexcept {
99 mesh_iterator tmp = *this;
100 --data_index;
101 return tmp;
102 }
103
110 [[nodiscard]] std::strong_ordering operator<=>(mesh_iterator const &rhs) const noexcept { return data_index <=> rhs.data_index; }
111
118 [[nodiscard]] bool operator==(mesh_iterator const &other) const noexcept { return mesh_ptr == other.mesh_ptr and data_index == other.data_index; }
119
124 [[nodiscard]] value_type operator*() const noexcept { return mesh_ptr->operator[](data_index); }
125
130 [[nodiscard]] value_type operator->() const noexcept { return operator*(); }
131
138 data_index += n;
139 return *this;
140 }
141
147 [[nodiscard]] mesh_iterator operator+(difference_type n) const noexcept { return {.mesh_ptr = mesh_ptr, .data_index = data_index + n}; }
148
155 [[nodiscard]] C2PY_IGNORE friend mesh_iterator operator+(difference_type n, mesh_iterator it) noexcept { return it + n; }
156
163 data_index -= n;
164 return *this;
165 }
166
172 [[nodiscard]] mesh_iterator operator-(difference_type n) const noexcept { return {.mesh_ptr = mesh_ptr, .data_index = data_index - n}; }
173
179 [[nodiscard]] difference_type operator-(mesh_iterator const &rhs) const noexcept { return data_index - rhs.data_index; }
180
186 [[nodiscard]] value_type operator[](difference_type n) const noexcept { return mesh_ptr->operator[](data_index + n); }
187 };
188
189} // namespace triqs::mesh
Common macros used in TRIQS.
A generic random access iterator for 1D meshes.
mesh_iterator & operator++() noexcept
Pre-increment operator increments the current data index by .
bool operator==(mesh_iterator const &other) const noexcept
Equal-to operator for two iterators.
value_type operator->() const noexcept
Member access operator.
difference_type operator-(mesh_iterator const &rhs) const noexcept
Get the distance between two iterators.
std::random_access_iterator_tag iterator_category
Iterator category.
mesh_iterator operator-(difference_type n) const noexcept
Subtraction operator for an iterator and an integer.
mesh_iterator & operator--() noexcept
Pre-decrement operator decrements the current data index by .
mesh_iterator operator--(int) noexcept
Post-decrement operator decrements the current data index by .
mesh_iterator operator++(int) noexcept
Post-increment operator increments the current data index by .
M const * mesh_ptr
Pointer to the underlying mesh.
long data_index
Current data index.
mesh_iterator operator+(difference_type n) const noexcept
Addition operator for an iterator and an integer.
friend mesh_iterator operator+(difference_type n, mesh_iterator it) noexcept
Addition operator for an integer and an iterator.
value_type * pointer
Pointer type.
mesh_iterator & operator+=(difference_type n) noexcept
Addition assignment operator.
value_type const & reference
Reference type.
value_type operator[](difference_type n) const noexcept
Subscript operator.
typename M::mesh_point_t value_type
Value type.
mesh_iterator & operator-=(difference_type n) noexcept
Subtraction assignment operator.
value_type operator*() const noexcept
Dereference operator.
std::ptrdiff_t difference_type
Difference type.
std::strong_ordering operator<=>(mesh_iterator const &rhs) const noexcept
Three-way comparison operator for two iterators.