Loading...
Searching...
No Matches
composition_abstract_iterator.h
Go to the documentation of this file.
#pragma once
#include <vector>
#include <iterator>
#include <utility>
#include <compare>
#include "fourdst/atomic/atomicSpecies.h"
namespace fourdst::composition::detail {
template <bool IsConst>
class CompositionIterator {
public:
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
// Returns a pair of references. Natively supports structured binding [sp, y]
using value_type = std::pair<const atomic::Species, double>;
// Define reference types based on const-ness
using SpeciesRef = const atomic::Species&;
using AbundRef = std::conditional_t<IsConst, const double&, double&>;
using reference = std::pair<SpeciesRef, AbundRef>;
struct ArrowProxy {
reference m_payload;
const reference* operator->() const { return &m_payload; }
};
using pointer = ArrowProxy;
private:
using SpecIt = std::vector<atomic::Species>::const_iterator;
using AbunIt = std::conditional_t<IsConst,
std::vector<double>::const_iterator,
std::vector<double>::iterator>;
SpecIt m_sIt;
AbunIt m_aIt;
public:
CompositionIterator() = default;
CompositionIterator(SpecIt sIt, AbunIt aIt) : m_sIt(sIt), m_aIt(aIt) {}
template <bool WasConst, typename = std::enable_if_t<IsConst && !WasConst>>
CompositionIterator(const CompositionIterator<WasConst>& other)
: m_sIt(other.getSpeciesIt()), m_aIt(other.getAbundanceIt()) {}
[[nodiscard]] SpecIt getSpeciesIt() const { return m_sIt; }
[[nodiscard]] AbunIt getAbundanceIt() const { return m_aIt; }
reference operator*() const {
return { *m_sIt, *m_aIt };
}
ArrowProxy operator->() const {
return ArrowProxy{ **this };
}
reference operator[](difference_type n) const {
return { *(m_sIt + n), *(m_aIt + n) };
}
// --- Movement ---
CompositionIterator& operator++() { ++m_sIt; ++m_aIt; return *this; }
CompositionIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
CompositionIterator& operator--() { --m_sIt; --m_aIt; return *this; } // FIXED
CompositionIterator operator--(int) { auto tmp = *this; --(*this); return tmp; }
CompositionIterator& operator+=(difference_type n) { m_sIt += n; m_aIt += n; return *this; }
CompositionIterator& operator-=(difference_type n) { m_sIt -= n; m_aIt -= n; return *this; }
// --- Arithmetic ---
friend CompositionIterator operator+(CompositionIterator it, difference_type n) { return it += n; }
// Commutative addition (n + it)
friend CompositionIterator operator+(difference_type n, CompositionIterator it) { return it += n; }
friend CompositionIterator operator-(CompositionIterator it, difference_type n) { return it -= n; }
// Difference between iterators
friend difference_type operator-(const CompositionIterator& lhs, const CompositionIterator& rhs) {
return lhs.m_sIt - rhs.m_sIt;
}
template <bool R>
bool operator==(const CompositionIterator<R>& other) const { return m_sIt == other.getSpeciesIt(); }
template <bool R>
bool operator!=(const CompositionIterator<R>& other) const { return m_sIt != other.getSpeciesIt(); }
template <bool R>
bool operator<(const CompositionIterator<R>& other) const { return m_sIt < other.getSpeciesIt(); }
template <bool R>
bool operator>(const CompositionIterator<R>& other) const { return m_sIt > other.getSpeciesIt(); }
template <bool R>
bool operator<=(const CompositionIterator<R>& other) const { return m_sIt <= other.getSpeciesIt(); }
template <bool R>
bool operator>=(const CompositionIterator<R>& other) const { return m_sIt >= other.getSpeciesIt(); }
};
}
atomicSpecies.h
fourdst::composition::detail::CompositionIterator::operator++
CompositionIterator & operator++()
Definition composition_abstract_iterator.h:66
fourdst::composition::detail::CompositionIterator< false >::getAbundanceIt
AbunIt getAbundanceIt() const
Definition composition_abstract_iterator.h:51
fourdst::composition::detail::CompositionIterator::operator-=
CompositionIterator & operator-=(difference_type n)
Definition composition_abstract_iterator.h:73
fourdst::composition::detail::CompositionIterator::operator+=
CompositionIterator & operator+=(difference_type n)
Definition composition_abstract_iterator.h:72
fourdst::composition::detail::CompositionIterator< false >::getSpeciesIt
SpecIt getSpeciesIt() const
Definition composition_abstract_iterator.h:50
fourdst::composition::detail::CompositionIterator::operator!=
bool operator!=(const CompositionIterator< R > &other) const
Definition composition_abstract_iterator.h:91
fourdst::composition::detail::CompositionIterator< false >::AbunIt
std::conditional_t< IsConst, std::vector< double >::const_iterator, std::vector< double >::iterator > AbunIt
Definition composition_abstract_iterator.h:35
fourdst::composition::detail::CompositionIterator::operator>=
bool operator>=(const CompositionIterator< R > &other) const
Definition composition_abstract_iterator.h:103
fourdst::composition::detail::CompositionIterator::operator+
friend CompositionIterator operator+(CompositionIterator it, difference_type n)
Definition composition_abstract_iterator.h:76
fourdst::composition::detail::CompositionIterator< false >::m_aIt
AbunIt m_aIt
Definition composition_abstract_iterator.h:40
fourdst::composition::detail::CompositionIterator::operator<
bool operator<(const CompositionIterator< R > &other) const
Definition composition_abstract_iterator.h:94
fourdst::composition::detail::CompositionIterator::CompositionIterator
CompositionIterator()=default
fourdst::composition::detail::CompositionIterator< false >::difference_type
std::ptrdiff_t difference_type
Definition composition_abstract_iterator.h:16
fourdst::composition::detail::CompositionIterator< false >::m_sIt
SpecIt m_sIt
Definition composition_abstract_iterator.h:39
fourdst::composition::detail::CompositionIterator< false >::value_type
std::pair< const atomic::Species, double > value_type
Definition composition_abstract_iterator.h:18
fourdst::composition::detail::CompositionIterator::operator++
CompositionIterator operator++(int)
Definition composition_abstract_iterator.h:67
fourdst::composition::detail::CompositionIterator::operator[]
reference operator[](difference_type n) const
Definition composition_abstract_iterator.h:61
fourdst::composition::detail::CompositionIterator::operator*
reference operator*() const
Definition composition_abstract_iterator.h:53
fourdst::composition::detail::CompositionIterator::operator>
bool operator>(const CompositionIterator< R > &other) const
Definition composition_abstract_iterator.h:97
fourdst::composition::detail::CompositionIterator< false >::pointer
ArrowProxy pointer
Definition composition_abstract_iterator.h:31
fourdst::composition::detail::CompositionIterator::operator-
friend difference_type operator-(const CompositionIterator &lhs, const CompositionIterator &rhs)
Definition composition_abstract_iterator.h:83
fourdst::composition::detail::CompositionIterator::operator<=
bool operator<=(const CompositionIterator< R > &other) const
Definition composition_abstract_iterator.h:100
fourdst::composition::detail::CompositionIterator::operator--
CompositionIterator operator--(int)
Definition composition_abstract_iterator.h:70
fourdst::composition::detail::CompositionIterator< false >::SpeciesRef
const atomic::Species & SpeciesRef
Definition composition_abstract_iterator.h:22
fourdst::composition::detail::CompositionIterator< false >::reference
std::pair< SpeciesRef, AbundRef > reference
Definition composition_abstract_iterator.h:24
fourdst::composition::detail::CompositionIterator::operator==
bool operator==(const CompositionIterator< R > &other) const
Definition composition_abstract_iterator.h:88
fourdst::composition::detail::CompositionIterator::operator--
CompositionIterator & operator--()
Definition composition_abstract_iterator.h:69
fourdst::composition::detail::CompositionIterator::CompositionIterator
CompositionIterator(SpecIt sIt, AbunIt aIt)
Definition composition_abstract_iterator.h:44
fourdst::composition::detail::CompositionIterator::CompositionIterator
CompositionIterator(const CompositionIterator< WasConst > &other)
Definition composition_abstract_iterator.h:47
fourdst::composition::detail::CompositionIterator< false >::iterator_category
std::random_access_iterator_tag iterator_category
Definition composition_abstract_iterator.h:15
fourdst::composition::detail::CompositionIterator::operator+
friend CompositionIterator operator+(difference_type n, CompositionIterator it)
Definition composition_abstract_iterator.h:79
fourdst::composition::detail::CompositionIterator::operator->
ArrowProxy operator->() const
Definition composition_abstract_iterator.h:57
fourdst::composition::detail::CompositionIterator::operator-
friend CompositionIterator operator-(CompositionIterator it, difference_type n)
Definition composition_abstract_iterator.h:80
fourdst::composition::detail::CompositionIterator< false >::SpecIt
std::vector< atomic::Species >::const_iterator SpecIt
Definition composition_abstract_iterator.h:34
fourdst::composition::detail::CompositionIterator< false >::AbundRef
std::conditional_t< IsConst, const double &, double & > AbundRef
Definition composition_abstract_iterator.h:23
fourdst::composition::detail
Definition composition_abstract_iterator.h:10
fourdst::atomic::Species
Represents an atomic species (isotope) with its fundamental physical properties.
Definition atomicSpecies.h:52
fourdst::composition::detail::CompositionIterator::ArrowProxy
Definition composition_abstract_iterator.h:26
fourdst::composition::detail::CompositionIterator::ArrowProxy::m_payload
reference m_payload
Definition composition_abstract_iterator.h:27
fourdst::composition::detail::CompositionIterator::ArrowProxy::operator->
const reference * operator->() const
Definition composition_abstract_iterator.h:28