13#if !defined(ULTRA_LINEAR_POPULATION_H)
14# error "Don't include this file directly, include the specific .h instead"
17#if !defined(ULTRA_LINEAR_POPULATION_TCC)
18#define ULTRA_LINEAR_POPULATION_TCC
30 : allowed_(
std::max(p.params.population.individuals,
31 p.params.population.min_individuals)),
32 min_allowed_(p.params.population.min_individuals)
52 Expects(allowed() >= min_allowed_);
55 std::generate_n(std::back_inserter(members_), allowed(),
56 [&p] {
return I(p); });
66I &linear_population<I>::operator[](std::size_t i)
79const I &linear_population<I>::operator[](std::size_t i)
const
89std::size_t linear_population<I>::size() const noexcept
91 return members_.size();
100template<Indiv
idual I>
101std::size_t linear_population<I>::safe_size()
const
103 std::lock_guard lock(mutex_);
104 return members_.size();
110template<Indiv
idual I>
111unsigned linear_population<I>::max_age() const noexcept
121template<Indiv
idual I>
122void linear_population<I>::max_age(
unsigned m)
noexcept
133template<Indiv
idual I>
134std::shared_mutex &linear_population<I>::mutex()
const
145template<Indiv
idual I>
146std::size_t linear_population<I>::allowed() const noexcept
162template<Indiv
idual I>
163void linear_population<I>::allowed(std::size_t n)
166 n = std::max(n, min_allowed_);
170 const auto delta(size() - n);
171 members_.erase(members_.end() - delta, members_.end());
184template<Indiv
idual I>
185bool linear_population<I>::empty() const noexcept
187 return members_.empty();
195template<Indiv
idual I>
196void linear_population<I>::clear() noexcept
209template<Indiv
idual I>
210void linear_population<I>::push_back(
const I &i)
212 if (size() < allowed())
213 members_.push_back(i);
221template<Indiv
idual I>
222void linear_population<I>::pop_back()
231template<Indiv
idual I>
232typename linear_population<I>::const_iterator
233linear_population<I>::begin() const noexcept
235 return members_.begin();
241template<Indiv
idual I>
242typename linear_population<I>::iterator linear_population<I>::begin() noexcept
244 return members_.begin();
250template<Indiv
idual I>
251typename linear_population<I>::const_iterator
252linear_population<I>::end() const noexcept
254 return members_.end();
260template<Indiv
idual I>
261typename linear_population<I>::iterator linear_population<I>::end() noexcept
263 return members_.end();
269template<Indiv
idual I>
270void linear_population<I>::inc_age()
272 std::ranges::for_each(*
this, [](
auto &i) { i.inc_age(); });
284template<Indiv
idual I>
285bool linear_population<I>::load(std::istream &in,
const symbol_set &ss)
287 unsigned tmp_max_age;
288 if (!(in >> tmp_max_age))
291 std::size_t tmp_min_allowed;
292 if (!(in >> tmp_min_allowed))
295 std::size_t tmp_allowed;
296 if (!(in >> tmp_allowed))
299 if (tmp_allowed < tmp_min_allowed)
306 if (tmp_allowed < n_elem)
309 std::vector<I> tmp_members;
310 tmp_members.reserve(n_elem);
311 for (std::size_t i(0); i < n_elem; ++i)
312 if (I ind; !ind.load(in, ss))
315 tmp_members.push_back(ind);
317 max_age(tmp_max_age);
318 members_ = std::move(tmp_members);
319 min_allowed_ = tmp_min_allowed;
320 allowed(tmp_allowed);
332template<Indiv
idual I>
333bool linear_population<I>::save(std::ostream &
out)
const
335 out << max_age() <<
' ' << min_allowed_ <<
' ' << allowed() <<
' ' << size()
338 for (
const auto &prg : *
this)
351template<Indiv
idual I>
362template<Indiv
idual I>
363bool linear_population<I>::is_valid()
const
365 if (!std::ranges::all_of(*
this, [](
const auto &i) {
return i.is_valid(); }))
368 if (allowed() < min_allowed_)
371 return size() <= allowed();
unsigned population_uid
Definition population.h:29