|
7 | 7 | * Licensed under the MIT License
|
8 | 8 | */
|
9 | 9 |
|
| 10 | +/** @file |
| 11 | + * @brief Defines a class for representing neutral atom computations. |
| 12 | + */ |
| 13 | + |
10 | 14 | #pragma once
|
11 | 15 |
|
12 |
| -#include "NADefinitions.hpp" |
13 |
| -#include "operations/NAOperation.hpp" |
| 16 | +#include "na/entities/Atom.hpp" |
| 17 | +#include "na/entities/Location.hpp" |
| 18 | +#include "na/entities/Zone.hpp" |
| 19 | +#include "na/operations/Op.hpp" |
14 | 20 |
|
15 |
| -#include <algorithm> |
16 | 21 | #include <cstddef>
|
17 |
| -#include <iterator> |
18 | 22 | #include <memory>
|
19 | 23 | #include <ostream>
|
20 | 24 | #include <string>
|
| 25 | +#include <unordered_map> |
| 26 | +#include <utility> |
21 | 27 | #include <vector>
|
22 | 28 |
|
23 | 29 | namespace na {
|
24 |
| -class NAComputation { |
| 30 | +/// Represents a neutral atom computation. |
| 31 | +class NAComputation final { |
25 | 32 | protected:
|
26 |
| - std::vector<std::shared_ptr<Point>> initialPositions; |
27 |
| - std::vector<std::unique_ptr<NAOperation>> operations; |
| 33 | + /// The operations in the NA computation. |
| 34 | + std::vector<std::unique_ptr<Op>> operations_; |
| 35 | + /// The atoms used in the NA computation. |
| 36 | + std::vector<std::unique_ptr<Atom>> atoms_; |
| 37 | + /// The zones used in the NA computation. |
| 38 | + std::vector<std::unique_ptr<Zone>> zones_; |
| 39 | + /// The initial locations of the atoms. |
| 40 | + std::unordered_map<const Atom*, Location> initialLocations_; |
28 | 41 |
|
29 | 42 | public:
|
30 |
| - NAComputation() = default; |
31 |
| - NAComputation(NAComputation&& qc) noexcept = default; |
32 |
| - NAComputation& operator=(NAComputation&& qc) noexcept = default; |
33 |
| - NAComputation(const NAComputation& qc) |
34 |
| - : initialPositions(qc.initialPositions) { |
35 |
| - operations.reserve(qc.operations.size()); |
36 |
| - std::transform(qc.operations.cbegin(), qc.operations.cend(), |
37 |
| - std::back_inserter(operations), |
38 |
| - [](const auto& op) { return op->clone(); }); |
| 43 | + /// Returns an iterator to the beginning of the operations. |
| 44 | + [[nodiscard]] auto begin() -> auto { return operations_.begin(); } |
| 45 | + |
| 46 | + /// Returns an iterator to the beginning of the operations. |
| 47 | + [[nodiscard]] auto begin() const -> auto { return operations_.begin(); } |
| 48 | + |
| 49 | + /// Returns an iterator to the end of the operations. |
| 50 | + [[nodiscard]] auto end() -> auto { return operations_.end(); } |
| 51 | + |
| 52 | + /// Returns an iterator to the end of the operations. |
| 53 | + [[nodiscard]] auto end() const -> auto { return operations_.end(); } |
| 54 | + |
| 55 | + /// Returns the number of operations in the NAComputation. |
| 56 | + [[nodiscard]] auto size() const -> std::size_t { return operations_.size(); } |
| 57 | + |
| 58 | + /// Returns a reference to the operation at the given index. |
| 59 | + /// @param i The index of the operation. |
| 60 | + /// @return A reference to the operation at the given index. |
| 61 | + [[nodiscard]] auto operator[](const std::size_t i) -> Op& { |
| 62 | + return *operations_[i]; |
| 63 | + } |
| 64 | + |
| 65 | + /// Returns a const reference to the operation at the given index. |
| 66 | + /// @param i The index of the operation. |
| 67 | + /// @return A const reference to the operation at the given index. |
| 68 | + [[nodiscard]] auto operator[](const std::size_t i) const -> const Op& { |
| 69 | + return *operations_[i]; |
| 70 | + } |
| 71 | + |
| 72 | + /// Clears the operations in the NAComputation. |
| 73 | + auto clear() -> void { operations_.clear(); } |
| 74 | + |
| 75 | + /// Returns the number of atoms used in the NAComputation. |
| 76 | + [[nodiscard]] auto getAtomsSize() const -> std::size_t { |
| 77 | + return atoms_.size(); |
39 | 78 | }
|
40 |
| - NAComputation& operator=(const NAComputation& qc) { |
41 |
| - if (this != &qc) { |
42 |
| - initialPositions = qc.initialPositions; |
43 |
| - operations.clear(); |
44 |
| - operations.reserve(qc.operations.size()); |
45 |
| - std::transform(qc.operations.cbegin(), qc.operations.cend(), |
46 |
| - std::back_inserter(operations), |
47 |
| - [](const auto& op) { return op->clone(); }); |
48 |
| - } |
49 |
| - return *this; |
| 79 | + |
| 80 | + /// Returns the atoms used in the NAComputation. |
| 81 | + [[nodiscard]] auto getAtoms() const -> auto& { return atoms_; } |
| 82 | + |
| 83 | + /// Returns the zones used in global operations within the NAComputation. |
| 84 | + [[nodiscard]] auto getZones() const -> auto& { return zones_; } |
| 85 | + |
| 86 | + /// Returns the initial locations of the atoms. |
| 87 | + [[nodiscard]] auto getInitialLocations() const -> auto& { |
| 88 | + return initialLocations_; |
50 | 89 | }
|
51 |
| - virtual ~NAComputation() = default; |
52 |
| - template <class T> auto emplaceBack(std::unique_ptr<T>&& op) -> void { |
53 |
| - static_assert(std::is_base_of_v<NAOperation, T>, |
54 |
| - "T must be a subclass of NAOperation."); |
55 |
| - operations.emplace_back(std::move(op)); |
| 90 | + |
| 91 | + /// Returns the location of the given atom after the given operation. |
| 92 | + /// @param atom The atom to get the location for. |
| 93 | + /// @param op The operation to get the location after. |
| 94 | + /// @return The location of the atom after the operation. |
| 95 | + [[nodiscard]] auto getLocationOfAtomAfterOperation(const Atom& atom, |
| 96 | + const Op& op) const |
| 97 | + -> Location; |
| 98 | + |
| 99 | + /// Emplaces a new atom with the given name and returns a reference to the |
| 100 | + /// newly created atom. |
| 101 | + /// @param name The name of the atom. |
| 102 | + /// @return A reference to the newly created atom. |
| 103 | + auto emplaceBackAtom(std::string name) -> const Atom& { |
| 104 | + return *atoms_.emplace_back(std::make_unique<Atom>(std::move(name))); |
56 | 105 | }
|
57 |
| - template <class T> auto emplaceBack(const std::unique_ptr<T>& op) -> void { |
58 |
| - static_assert(std::is_base_of_v<NAOperation, T>, |
59 |
| - "T must be a subclass of NAOperation."); |
60 |
| - operations.emplace_back(std::move(op)); |
| 106 | + |
| 107 | + /// Emplaces a new zone with the given name and returns a reference to the |
| 108 | + /// newly created zone. |
| 109 | + /// @param name The name of the zone. |
| 110 | + /// @return A reference to the newly created zone. |
| 111 | + auto emplaceBackZone(std::string name) -> const Zone& { |
| 112 | + return *zones_.emplace_back(std::make_unique<Zone>(std::move(name))); |
61 | 113 | }
|
62 |
| - template <class T, class... Args> auto emplaceBack(Args&&... args) -> void { |
63 |
| - static_assert(std::is_base_of_v<NAOperation, T>, |
64 |
| - "T must be a subclass of NAOperation."); |
65 |
| - operations.emplace_back(std::make_unique<T>(std::forward<Args>(args)...)); |
| 114 | + |
| 115 | + /// Emplaces a new initial location for the given atom with the given location |
| 116 | + /// and returns a reference to the newly created location. |
| 117 | + /// @param atom The atom to set the initial location for. |
| 118 | + /// @param loc The location of the atom. |
| 119 | + /// @return A reference to the newly created location. |
| 120 | + auto emplaceInitialLocation(const Atom& atom, const Location& loc) |
| 121 | + -> const Location& { |
| 122 | + return initialLocations_.emplace(&atom, loc).first->second; |
66 | 123 | }
|
67 |
| - auto clear(const bool clearInitialPositions = true) -> void { |
68 |
| - operations.clear(); |
69 |
| - if (clearInitialPositions) { |
70 |
| - initialPositions.clear(); |
71 |
| - } |
| 124 | + |
| 125 | + /// Emplaces a new initial location for the given atom with the given |
| 126 | + /// arguments and returns a reference to the newly created location. |
| 127 | + /// @param atom The atom to set the initial location for. |
| 128 | + /// @param loc The parameters for the location of the atom. |
| 129 | + /// @return A reference to the newly created location. |
| 130 | + template <typename... Args> |
| 131 | + auto emplaceInitialLocation(const Atom& atom, Args&&... loc) |
| 132 | + -> const Location& { |
| 133 | + return initialLocations_ |
| 134 | + .emplace(&atom, |
| 135 | + Location{static_cast<double>(std::forward<Args>(loc))...}) |
| 136 | + .first->second; |
72 | 137 | }
|
73 |
| - [[nodiscard]] auto size() const -> std::size_t { return operations.size(); } |
74 |
| - [[nodiscard]] auto getInitialPositions() const |
75 |
| - -> const std::vector<std::shared_ptr<Point>>& { |
76 |
| - return initialPositions; |
| 138 | + |
| 139 | + /// Emplaces a new operation of type T with the given operation and returns a |
| 140 | + /// reference to the newly created operation. |
| 141 | + /// @tparam T The concrete type of the operation. |
| 142 | + /// @param op The operation to emplace. |
| 143 | + /// @return A reference to the newly created operation. |
| 144 | + template <class T> auto emplaceBack(T&& op) -> const Op& { |
| 145 | + return *operations_.emplace_back(std::make_unique<T>(std::forward<T>(op))); |
77 | 146 | }
|
78 |
| - auto emplaceInitialPosition(std::shared_ptr<Point> p) -> void { |
79 |
| - initialPositions.emplace_back(std::move(p)); |
| 147 | + |
| 148 | + /// Emplaces a new operation of type T with the given arguments and returns a |
| 149 | + /// reference to the newly created operation. |
| 150 | + /// @tparam T The concrete type of the operation. |
| 151 | + /// @param args The arguments for the operation. |
| 152 | + /// @return A reference to the newly created operation. |
| 153 | + template <class T, typename... Args> |
| 154 | + auto emplaceBack(Args&&... args) -> const Op& { |
| 155 | + return *operations_.emplace_back( |
| 156 | + std::make_unique<T>(std::forward<Args>(args)...)); |
80 | 157 | }
|
| 158 | + |
| 159 | + /// Returns a string representation of the NAComputation. |
81 | 160 | [[nodiscard]] auto toString() const -> std::string;
|
| 161 | + /// Outputs the NAComputation to the given output stream, i.e., the string |
| 162 | + /// returned by toString(). |
| 163 | + /// @param os The output stream to print the NAComputation to. |
| 164 | + /// @param qc The NAComputation to print. |
| 165 | + /// @return The output stream after printing the NAComputation. |
82 | 166 | friend auto operator<<(std::ostream& os, const NAComputation& qc)
|
83 | 167 | -> std::ostream& {
|
84 | 168 | return os << qc.toString();
|
85 | 169 | }
|
86 |
| - // Iterators (pass-through) |
87 |
| - auto begin() noexcept { return operations.begin(); } |
88 |
| - [[nodiscard]] auto begin() const noexcept { return operations.begin(); } |
89 |
| - [[nodiscard]] auto cbegin() const noexcept { return operations.cbegin(); } |
90 |
| - auto end() noexcept { return operations.end(); } |
91 |
| - [[nodiscard]] auto end() const noexcept { return operations.end(); } |
92 |
| - [[nodiscard]] auto cend() const noexcept { return operations.cend(); } |
93 |
| - auto rbegin() noexcept { return operations.rbegin(); } |
94 |
| - [[nodiscard]] auto rbegin() const noexcept { return operations.rbegin(); } |
95 |
| - [[nodiscard]] auto crbegin() const noexcept { return operations.crbegin(); } |
96 |
| - auto rend() noexcept { return operations.rend(); } |
97 |
| - [[nodiscard]] auto rend() const noexcept { return operations.rend(); } |
98 |
| - [[nodiscard]] auto crend() const noexcept { return operations.crend(); } |
99 |
| - |
100 |
| - [[nodiscard]] auto validateAODConstraints() const -> bool; |
| 170 | + |
| 171 | + /// Validates the NAComputation and checks whether all AOD constraints are |
| 172 | + /// fulfilled. |
| 173 | + /// Specifically, |
| 174 | + /// - each atom is loaded before it is moved |
| 175 | + /// - the relative order of loaded atoms is preserved |
| 176 | + /// - each atom is loaded before it is stored |
| 177 | + /// - each atom is stored before it is loaded (again) |
| 178 | + /// @returns a pair of a Boolean indicating whether the NAComputation is valid |
| 179 | + /// and a string containing the error message if the NAComputation is invalid. |
| 180 | + [[nodiscard]] auto validate() const -> std::pair<bool, std::string>; |
101 | 181 | };
|
102 | 182 | } // namespace na
|
0 commit comments