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