#pragma once #include "evo/ecology/collapse.hpp" namespace evo::ecology { // How many whole members to draw from one class. A residual is a fraction that is // owed and not present, so it is never drawable: an expected density is not an // available individual. struct ClassDraw { std::size_t age_class{}; std::uint64_t count{}; auto operator<=>(const ClassDraw&) const = default; }; struct MaterializeRequest { Owner owner{}; std::uint64_t revision{}; std::vector draws; // Identities are assigned in draw order from here. Materialization invents // identity and nothing else: no parentage, no history, no condition. std::uint64_t first_identity{}; // One per member to be drawn, in draw order. Each must be empty when the plan // is committed, so a member's reserve is the whole of what backs it rather // than a part sharing a store with inventory nobody counted. std::vector reservoirs; ApproximationBound bound{}; }; enum class MaterializeError : std::uint8_t { invalid_request, nothing_drawn, unknown_age_class, duplicate_age_class, insufficient_members, reservoir_mismatch, duplicate_reservoir, wrong_owner, stale_revision, overflow, invalid_cohort, not_installed, stale_plan, reservoir_not_empty, // An empty reservoir is not an available one: a vacant cohort, or one holding // nothing, still owns its store. reservoir_owned, ledger_failure }; // The draw, decided once. Copy-only for the reason a cohort is: a moved-from plan // would keep its identities while losing its legs and its members. class MaterializePlan { public: MaterializePlan(const MaterializePlan&) = default; MaterializePlan& operator=(const MaterializePlan&) = default; [[nodiscard]] const Cohort& before() const noexcept { return before_; } // The cohort after the draw is debited. Vacant if every member was taken and // nothing is owed: vacancy is not extinction, and the record stays. [[nodiscard]] const Cohort& after() const noexcept { return after_; } // Exactly what a collapse consumes, so the two compose and can be checked // against each other rather than only described as inverses. [[nodiscard]] std::span members() const noexcept { return members_; } [[nodiscard]] std::span legs() const noexcept { return legs_; } [[nodiscard]] accounting::Inventory withdrawn() const noexcept { return withdrawn_; } auto operator<=>(const MaterializePlan&) const = default; private: friend std::expected plan_materialize(const Cohort&, const MaterializeRequest&); MaterializePlan(const Cohort& before, const Cohort& after) : before_{before}, after_{after} {} Cohort before_; Cohort after_; std::vector members_; std::vector legs_; accounting::Inventory withdrawn_{}; }; // Decides the whole draw without touching any ledger or registry. Instantaneous: // it happens at the cohort's current tick and never advances it, because // advancing time is ADR-0038's interval and this is not one. [[nodiscard]] std::expected plan_materialize(const Cohort& before, const MaterializeRequest& request); // Applies exactly that plan. The installed record must still be the one the plan // started from, and every destination reservoir must be both empty and unowned, so // no rejection can leave the cohort debited with nobody holding what it gave up and // no credit can land in a store some other record already accounts for. [[nodiscard]] std::expected, MaterializeError> commit_materialize(accounting::Ledger& ledger, CohortRegistry& registry, const MaterializePlan& plan); } // namespace evo::ecology