CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
species_collection.h
1#pragma once
2
3#include <algorithm>
4#include <cassert>
5#include <memory>
6#include <string>
7#include <vector>
8
9#include "base_species.h"
10
34 public:
35 using Ptr = std::unique_ptr<BaseSpecies>;
36
37 struct Entry {
38 std::string key;
39 Ptr species;
40
41 // Forwarders mimic std::unique_ptr: const-on-Entry does NOT propagate to
42 // the pointee, matching how `const std::unique_ptr<T>&` iteration behaved
43 // under the previous std::map-backed storage.
44 BaseSpecies* operator->() const {
45 return species.get();
46 }
47 BaseSpecies& operator*() const {
48 return *species;
49 }
50 BaseSpecies* get() const {
51 return species.get();
52 }
53 explicit operator bool() const {
54 return static_cast<bool>(species);
55 }
56 };
57
58 using Container = std::vector<Entry>;
59
60 SpeciesCollection() = default;
61 SpeciesCollection(const SpeciesCollection&) = delete;
62 SpeciesCollection& operator=(const SpeciesCollection&) = delete;
64 SpeciesCollection& operator=(SpeciesCollection&&) = default;
65
66 // ── Construction-phase mutation ─────────────────────────────────────────
69 void insert(std::string key, Ptr species);
70
73 void freeze();
74
75 // ── Keyed lookup (replaces std::map API) ────────────────────────────────
77 bool contains(const std::string& key) const;
78
83 std::unique_ptr<BaseSpecies>& at(const std::string& key);
84 const std::unique_ptr<BaseSpecies>& at(const std::string& key) const;
85
89 std::unique_ptr<BaseSpecies>* find(const std::string& key);
90 const std::unique_ptr<BaseSpecies>* find(const std::string& key) const;
91
95 std::size_t index_of(const std::string& key) const {
96 assert(frozen_);
97 auto it = std::lower_bound(species_.begin(),
98 species_.end(),
99 key,
100 [](const Entry& e, const std::string& k) { return e.key < k; });
101 if (it == species_.end() || it->key != key) {
102 return species_.size();
103 }
104 return static_cast<std::size_t>(it - species_.begin());
105 }
106
107 // ── Hot-path cached accessors ───────────────────────────────────────────
108 // Valid only after freeze(); asserted in debug builds.
109 BaseSpecies& photons() {
110 assert(frozen_ && photons_);
111 return *photons_;
112 }
113 BaseSpecies& photons() const {
114 assert(frozen_ && photons_);
115 return *photons_;
116 }
117 BaseSpecies& baryons() {
118 assert(frozen_ && baryons_);
119 return *baryons_;
120 }
121 BaseSpecies& baryons() const {
122 assert(frozen_ && baryons_);
123 return *baryons_;
124 }
125 std::size_t photons_index() const {
126 assert(frozen_);
127 return photons_index_;
128 }
129 std::size_t baryons_index() const {
130 assert(frozen_);
131 return baryons_index_;
132 }
135 bool has_ncdm() const {
136 assert(frozen_);
137 return has_ncdm_;
138 }
143 bool has_warm_matter() const {
144 assert(frozen_);
145 return has_warm_matter_;
146 }
147
148 // ── Iteration / size ────────────────────────────────────────────────────
149 Container::iterator begin() {
150 return species_.begin();
151 }
152 Container::iterator end() {
153 return species_.end();
154 }
155 Container::const_iterator begin() const {
156 return species_.begin();
157 }
158 Container::const_iterator end() const {
159 return species_.end();
160 }
161
162 bool empty() const {
163 return species_.empty();
164 }
165 std::size_t size() const {
166 return species_.size();
167 }
168
171 BaseSpecies* operator[](std::size_t i) {
172 assert(i < species_.size());
173 return species_[i].species.get();
174 }
175 const BaseSpecies* operator[](std::size_t i) const {
176 assert(i < species_.size());
177 return species_[i].species.get();
178 }
179
185 BaseSpecies* get(std::size_t i) const {
186 assert(i < species_.size());
187 return species_[i].species.get();
188 }
189
190 private:
191 Container species_;
192 BaseSpecies* photons_ = nullptr;
193 BaseSpecies* baryons_ = nullptr;
194 std::size_t photons_index_ = 0;
195 std::size_t baryons_index_ = 0;
196 bool has_ncdm_ = false;
197 bool has_warm_matter_ = false;
198 bool frozen_ = false;
199};
Definition base_species.h:90
Definition species_collection.h:33
BaseSpecies * get(std::size_t i) const
Definition species_collection.h:185
std::unique_ptr< BaseSpecies > * find(const std::string &key)
Definition species_collection.cpp:81
void insert(std::string key, Ptr species)
Definition species_collection.cpp:6
bool has_warm_matter() const
Definition species_collection.h:143
BaseSpecies * operator[](std::size_t i)
Definition species_collection.h:171
std::unique_ptr< BaseSpecies > & at(const std::string &key)
Definition species_collection.cpp:88
void freeze()
Definition species_collection.cpp:21
std::size_t index_of(const std::string &key) const
Definition species_collection.h:95
bool contains(const std::string &key) const
Definition species_collection.cpp:77
bool has_ncdm() const
Definition species_collection.h:135