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) ────────────────────────────────
76 std::size_t count(const std::string& key) const;
77
82 std::unique_ptr<BaseSpecies>& at(const std::string& key);
83 const std::unique_ptr<BaseSpecies>& at(const std::string& key) const;
84
88 std::unique_ptr<BaseSpecies>* find(const std::string& key);
89 const std::unique_ptr<BaseSpecies>* find(const std::string& key) const;
90
94 std::size_t index_of(const std::string& key) const {
95 assert(frozen_);
96 auto it = std::lower_bound(species_.begin(),
97 species_.end(),
98 key,
99 [](const Entry& e, const std::string& k) { return e.key < k; });
100 if (it == species_.end() || it->key != key)
101 return species_.size();
102 return static_cast<std::size_t>(it - species_.begin());
103 }
104
105 // ── Hot-path cached accessors ───────────────────────────────────────────
106 // Valid only after freeze(); asserted in debug builds.
107 BaseSpecies& photons() {
108 assert(frozen_ && photons_);
109 return *photons_;
110 }
111 BaseSpecies& photons() const {
112 assert(frozen_ && photons_);
113 return *photons_;
114 }
115 BaseSpecies& baryons() {
116 assert(frozen_ && baryons_);
117 return *baryons_;
118 }
119 BaseSpecies& baryons() const {
120 assert(frozen_ && baryons_);
121 return *baryons_;
122 }
123 std::size_t photons_index() const {
124 assert(frozen_);
125 return photons_index_;
126 }
127 std::size_t baryons_index() const {
128 assert(frozen_);
129 return baryons_index_;
130 }
133 bool has_ncdm() const {
134 assert(frozen_);
135 return has_ncdm_;
136 }
141 bool has_warm_matter() const {
142 assert(frozen_);
143 return has_warm_matter_;
144 }
145
146 // ── Iteration / size ────────────────────────────────────────────────────
147 Container::iterator begin() {
148 return species_.begin();
149 }
150 Container::iterator end() {
151 return species_.end();
152 }
153 Container::const_iterator begin() const {
154 return species_.begin();
155 }
156 Container::const_iterator end() const {
157 return species_.end();
158 }
159
160 bool empty() const {
161 return species_.empty();
162 }
163 std::size_t size() const {
164 return species_.size();
165 }
166
169 BaseSpecies* operator[](std::size_t i) {
170 assert(i < species_.size());
171 return species_[i].species.get();
172 }
173 const BaseSpecies* operator[](std::size_t i) const {
174 assert(i < species_.size());
175 return species_[i].species.get();
176 }
177
178 private:
179 Container species_;
180 BaseSpecies* photons_ = nullptr;
181 BaseSpecies* baryons_ = nullptr;
182 std::size_t photons_index_ = 0;
183 std::size_t baryons_index_ = 0;
184 bool has_ncdm_ = false;
185 bool has_warm_matter_ = false;
186 bool frozen_ = false;
187};
Definition base_species.h:76
Definition species_collection.h:33
std::unique_ptr< BaseSpecies > * find(const std::string &key)
Definition species_collection.cpp:77
void insert(std::string key, Ptr species)
Definition species_collection.cpp:9
bool has_warm_matter() const
Definition species_collection.h:141
BaseSpecies * operator[](std::size_t i)
Definition species_collection.h:169
std::unique_ptr< BaseSpecies > & at(const std::string &key)
Definition species_collection.cpp:84
void freeze()
Definition species_collection.cpp:24
std::size_t index_of(const std::string &key) const
Definition species_collection.h:94
bool has_ncdm() const
Definition species_collection.h:133