Line data Source code
1 : #ifndef EBF12521_1715_4669_88B2_124B8D0C2AEA_HPP 2 : #define EBF12521_1715_4669_88B2_124B8D0C2AEA_HPP 3 : 4 : #include "AbstractReactionTable.hpp" 5 : #include "DiscType.hpp" 6 : #include "Reaction.hpp" 7 : 8 : #include <vector> 9 : 10 : namespace cell 11 : { 12 : 13 : /** 14 : * @brief Maps a `vector<Reaction>` to `DiscType` or pairs of such: 15 : * 16 : * - `DiscType` -> transformation/decomposition reactions 17 : * 18 : * - `pair<DiscType, DiscType>` -> combination/exchange reactions 19 : */ 20 : class ReactionTable : public AbstractReactionTable 21 : { 22 : public: 23 : ReactionTable(const DiscTypeRegistry& discTypeRegistry); 24 : 25 : const DiscTypeMap<std::vector<Reaction>>& getTransformations() const override; 26 : const DiscTypeMap<std::vector<Reaction>>& getDecompositions() const override; 27 : const DiscTypePairMap<std::vector<Reaction>>& getCombinations() const override; 28 : const DiscTypePairMap<std::vector<Reaction>>& getExchanges() const override; 29 : 30 : /** 31 : * @brief Adds a new reaction to the table and updates all lookup maps 32 : */ 33 : void addReaction(const Reaction& reaction); 34 : 35 : /** 36 : * @brief Equivalent to calling `clear()` and `add(...)` for each reaction 37 : */ 38 : void setReactions(const std::vector<Reaction>& reactions); 39 : 40 : /** 41 : * @brief Removes all reactions where the given disc types are part of the educts or products 42 : * @param discTypesToRemove A vector containing disc types that are to be removed. Their IDs must match with the 43 : * disc types already in the table. 44 : */ 45 : void removeDiscType(DiscTypeID discTypeToRemove); 46 : 47 : /** 48 : * @brief Clears the reaction vector and all reaction lookup tables 49 : */ 50 : void clear(); 51 : 52 : /** 53 : * @returns a vector containing all reactions currently in the table 54 : */ 55 : const std::vector<Reaction>& getReactions() const; 56 : 57 : private: 58 : /** 59 : * @brief Clears the old lookup maps and creates new ones based on the current reaction vector 60 : */ 61 : void createLookupMaps(); 62 : 63 : void checkIfIsDuplicateReaction(const Reaction& reaction) const; 64 : 65 : bool isUnary(const Reaction& r) const; 66 : 67 : // We make these helpers static and let them have *this as a parameter to avoid const overloads 68 : 69 11 : template <typename Self> static auto& unaryMap(Self& self, const Reaction& r) 70 : { 71 11 : return (r.getType() == Reaction::Type::Transformation) ? self.transformations_ : self.decompositions_; 72 : } 73 : 74 15 : template <typename Self> static auto& binaryMap(Self& self, const Reaction& r) 75 : { 76 15 : return (r.getType() == Reaction::Type::Combination) ? self.combinations_ : self.exchanges_; 77 : } 78 : 79 : private: 80 : std::vector<Reaction> reactions_; 81 : 82 : DiscTypeMap<std::vector<Reaction>> transformations_; 83 : DiscTypeMap<std::vector<Reaction>> decompositions_; 84 : DiscTypePairMap<std::vector<Reaction>> combinations_; 85 : DiscTypePairMap<std::vector<Reaction>> exchanges_; 86 : 87 : const DiscTypeRegistry& discTypeRegistry_; 88 : }; 89 : 90 : } // namespace cell 91 : 92 : #endif /* EBF12521_1715_4669_88B2_124B8D0C2AEA_HPP */