fourdst::libcomposition v2.3.1
Robust atomic species information library
Loading...
Searching...
No Matches
composition_abstract_iterator.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <iterator>
5#include <utility>
6#include <compare>
7
9
11
12 template <bool IsConst>
14 public:
15 using iterator_category = std::random_access_iterator_tag;
16 using difference_type = std::ptrdiff_t;
17 // Returns a pair of references. Natively supports structured binding [sp, y]
18 using value_type = std::pair<const atomic::Species, double>;
19
20
21 // Define reference types based on const-ness
23 using AbundRef = std::conditional_t<IsConst, const double&, double&>;
24 using reference = std::pair<SpeciesRef, AbundRef>;
25
26 struct ArrowProxy {
28 const reference* operator->() const { return &m_payload; }
29 };
30
31 using pointer = ArrowProxy;
32
33 private:
34 using SpecIt = std::vector<atomic::Species>::const_iterator;
35 using AbunIt = std::conditional_t<IsConst,
36 std::vector<double>::const_iterator,
37 std::vector<double>::iterator>;
38
41
42 public:
44 CompositionIterator(SpecIt sIt, AbunIt aIt) : m_sIt(sIt), m_aIt(aIt) {}
45
46 template <bool WasConst, typename = std::enable_if_t<IsConst && !WasConst>>
49
50 [[nodiscard]] SpecIt getSpeciesIt() const { return m_sIt; }
51 [[nodiscard]] AbunIt getAbundanceIt() const { return m_aIt; }
52
54 return { *m_sIt, *m_aIt };
55 }
56
57 ArrowProxy operator->() const {
58 return ArrowProxy{ **this };
59 }
60
62 return { *(m_sIt + n), *(m_aIt + n) };
63 }
64
65 // --- Movement ---
66 CompositionIterator& operator++() { ++m_sIt; ++m_aIt; return *this; }
67 CompositionIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
68
69 CompositionIterator& operator--() { --m_sIt; --m_aIt; return *this; } // FIXED
70 CompositionIterator operator--(int) { auto tmp = *this; --(*this); return tmp; }
71
72 CompositionIterator& operator+=(difference_type n) { m_sIt += n; m_aIt += n; return *this; }
73 CompositionIterator& operator-=(difference_type n) { m_sIt -= n; m_aIt -= n; return *this; }
74
75 // --- Arithmetic ---
77
78 // Commutative addition (n + it)
81
82 // Difference between iterators
84 return lhs.m_sIt - rhs.m_sIt;
85 }
86
87 template <bool R>
88 bool operator==(const CompositionIterator<R>& other) const { return m_sIt == other.getSpeciesIt(); }
89
90 template <bool R>
91 bool operator!=(const CompositionIterator<R>& other) const { return m_sIt != other.getSpeciesIt(); }
92
93 template <bool R>
94 bool operator<(const CompositionIterator<R>& other) const { return m_sIt < other.getSpeciesIt(); }
95
96 template <bool R>
97 bool operator>(const CompositionIterator<R>& other) const { return m_sIt > other.getSpeciesIt(); }
98
99 template <bool R>
100 bool operator<=(const CompositionIterator<R>& other) const { return m_sIt <= other.getSpeciesIt(); }
101
102 template <bool R>
103 bool operator>=(const CompositionIterator<R>& other) const { return m_sIt >= other.getSpeciesIt(); }
104 };
105
106}
bool operator!=(const CompositionIterator< R > &other) const
std::conditional_t< IsConst, std::vector< double >::const_iterator, std::vector< double >::iterator > AbunIt
bool operator>=(const CompositionIterator< R > &other) const
friend CompositionIterator operator+(CompositionIterator it, difference_type n)
bool operator<(const CompositionIterator< R > &other) const
bool operator>(const CompositionIterator< R > &other) const
friend difference_type operator-(const CompositionIterator &lhs, const CompositionIterator &rhs)
bool operator<=(const CompositionIterator< R > &other) const
bool operator==(const CompositionIterator< R > &other) const
CompositionIterator(const CompositionIterator< WasConst > &other)
friend CompositionIterator operator+(difference_type n, CompositionIterator it)
friend CompositionIterator operator-(CompositionIterator it, difference_type n)
std::conditional_t< IsConst, const double &, double & > AbundRef
Represents an atomic species (isotope) with its fundamental physical properties.