flow.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #ifndef ENTT_GRAPH_FLOW_HPP
  2. #define ENTT_GRAPH_FLOW_HPP
  3. #include <algorithm>
  4. #include <cstddef>
  5. #include <functional>
  6. #include <iterator>
  7. #include <memory>
  8. #include <type_traits>
  9. #include <utility>
  10. #include <vector>
  11. #include "../config/config.h"
  12. #include "../container/dense_map.hpp"
  13. #include "../container/dense_set.hpp"
  14. #include "../core/compressed_pair.hpp"
  15. #include "../core/fwd.hpp"
  16. #include "../core/iterator.hpp"
  17. #include "../core/utility.hpp"
  18. #include "adjacency_matrix.hpp"
  19. #include "fwd.hpp"
  20. namespace entt {
  21. /**
  22. * @brief Utility class for creating task graphs.
  23. * @tparam Allocator Type of allocator used to manage memory and elements.
  24. */
  25. template<typename Allocator>
  26. class basic_flow {
  27. using alloc_traits = std::allocator_traits<Allocator>;
  28. static_assert(std::is_same_v<typename alloc_traits::value_type, id_type>, "Invalid value type");
  29. using task_container_type = dense_set<id_type, identity, std::equal_to<id_type>, typename alloc_traits::template rebind_alloc<id_type>>;
  30. using ro_rw_container_type = std::vector<std::pair<std::size_t, bool>, typename alloc_traits::template rebind_alloc<std::pair<std::size_t, bool>>>;
  31. using deps_container_type = dense_map<id_type, ro_rw_container_type, identity, std::equal_to<id_type>, typename alloc_traits::template rebind_alloc<std::pair<const id_type, ro_rw_container_type>>>;
  32. using adjacency_matrix_type = adjacency_matrix<directed_tag, typename alloc_traits::template rebind_alloc<std::size_t>>;
  33. void emplace(const id_type res, const bool is_rw) {
  34. ENTT_ASSERT(index.first() < vertices.size(), "Invalid node");
  35. if(!deps.contains(res) && sync_on != vertices.size()) {
  36. deps[res].emplace_back(sync_on, true);
  37. }
  38. deps[res].emplace_back(index.first(), is_rw);
  39. }
  40. void setup_graph(adjacency_matrix_type &matrix) const {
  41. for(const auto &elem: deps) {
  42. const auto last = elem.second.cend();
  43. auto it = elem.second.cbegin();
  44. while(it != last) {
  45. if(it->second) {
  46. // rw item
  47. if(auto curr = it++; it != last) {
  48. if(it->second) {
  49. matrix.insert(curr->first, it->first);
  50. } else if(const auto next = std::find_if(it, last, [](const auto &value) { return value.second; }); next != last) {
  51. for(; it != next; ++it) {
  52. matrix.insert(curr->first, it->first);
  53. matrix.insert(it->first, next->first);
  54. }
  55. } else {
  56. for(; it != next; ++it) {
  57. matrix.insert(curr->first, it->first);
  58. }
  59. }
  60. }
  61. } else {
  62. // ro item (first iteration only)
  63. if(const auto next = std::find_if(it, last, [](const auto &value) { return value.second; }); next != last) {
  64. for(; it != next; ++it) {
  65. matrix.insert(it->first, next->first);
  66. }
  67. } else {
  68. it = last;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. void transitive_closure(adjacency_matrix_type &matrix) const {
  75. const auto length = matrix.size();
  76. for(std::size_t vk{}; vk < length; ++vk) {
  77. for(std::size_t vi{}; vi < length; ++vi) {
  78. for(std::size_t vj{}; vj < length; ++vj) {
  79. if(matrix.contains(vi, vk) && matrix.contains(vk, vj)) {
  80. matrix.insert(vi, vj);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. void transitive_reduction(adjacency_matrix_type &matrix) const {
  87. const auto length = matrix.size();
  88. for(std::size_t vert{}; vert < length; ++vert) {
  89. matrix.erase(vert, vert);
  90. }
  91. for(std::size_t vj{}; vj < length; ++vj) {
  92. for(std::size_t vi{}; vi < length; ++vi) {
  93. if(matrix.contains(vi, vj)) {
  94. for(std::size_t vk{}; vk < length; ++vk) {
  95. if(matrix.contains(vj, vk)) {
  96. matrix.erase(vi, vk);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. public:
  104. /*! @brief Allocator type. */
  105. using allocator_type = Allocator;
  106. /*! @brief Unsigned integer type. */
  107. using size_type = std::size_t;
  108. /*! @brief Iterable task list. */
  109. using iterable = iterable_adaptor<typename task_container_type::const_iterator>;
  110. /*! @brief Adjacency matrix type. */
  111. using graph_type = adjacency_matrix_type;
  112. /*! @brief Default constructor. */
  113. basic_flow()
  114. : basic_flow{allocator_type{}} {}
  115. /**
  116. * @brief Constructs a flow builder with a given allocator.
  117. * @param allocator The allocator to use.
  118. */
  119. explicit basic_flow(const allocator_type &allocator)
  120. : index{0u, allocator},
  121. vertices{allocator},
  122. deps{allocator},
  123. sync_on{} {}
  124. /*! @brief Default copy constructor. */
  125. basic_flow(const basic_flow &) = default;
  126. /**
  127. * @brief Allocator-extended copy constructor.
  128. * @param other The instance to copy from.
  129. * @param allocator The allocator to use.
  130. */
  131. basic_flow(const basic_flow &other, const allocator_type &allocator)
  132. : index{other.index.first(), allocator},
  133. vertices{other.vertices, allocator},
  134. deps{other.deps, allocator},
  135. sync_on{other.sync_on} {}
  136. /*! @brief Default move constructor. */
  137. basic_flow(basic_flow &&) noexcept = default;
  138. /**
  139. * @brief Allocator-extended move constructor.
  140. * @param other The instance to move from.
  141. * @param allocator The allocator to use.
  142. */
  143. basic_flow(basic_flow &&other, const allocator_type &allocator)
  144. : index{other.index.first(), allocator},
  145. vertices{std::move(other.vertices), allocator},
  146. deps{std::move(other.deps), allocator},
  147. sync_on{other.sync_on} {}
  148. /**
  149. * @brief Default copy assignment operator.
  150. * @return This flow builder.
  151. */
  152. basic_flow &operator=(const basic_flow &) = default;
  153. /**
  154. * @brief Default move assignment operator.
  155. * @return This flow builder.
  156. */
  157. basic_flow &operator=(basic_flow &&) noexcept = default;
  158. /**
  159. * @brief Returns the associated allocator.
  160. * @return The associated allocator.
  161. */
  162. [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
  163. return allocator_type{index.second()};
  164. }
  165. /**
  166. * @brief Returns the identifier at specified location.
  167. * @param pos Position of the identifier to return.
  168. * @return The requested identifier.
  169. */
  170. [[nodiscard]] id_type operator[](const size_type pos) const {
  171. return vertices.cbegin()[pos];
  172. }
  173. /*! @brief Clears the flow builder. */
  174. void clear() noexcept {
  175. index.first() = {};
  176. vertices.clear();
  177. deps.clear();
  178. sync_on = {};
  179. }
  180. /**
  181. * @brief Exchanges the contents with those of a given flow builder.
  182. * @param other Flow builder to exchange the content with.
  183. */
  184. void swap(basic_flow &other) {
  185. using std::swap;
  186. std::swap(index, other.index);
  187. std::swap(vertices, other.vertices);
  188. std::swap(deps, other.deps);
  189. std::swap(sync_on, other.sync_on);
  190. }
  191. /**
  192. * @brief Returns the number of tasks.
  193. * @return The number of tasks.
  194. */
  195. [[nodiscard]] size_type size() const noexcept {
  196. return vertices.size();
  197. }
  198. /**
  199. * @brief Binds a task to a flow builder.
  200. * @param value Task identifier.
  201. * @return This flow builder.
  202. */
  203. basic_flow &bind(const id_type value) {
  204. sync_on += (sync_on == vertices.size());
  205. const auto it = vertices.emplace(value).first;
  206. index.first() = size_type(it - vertices.begin());
  207. return *this;
  208. }
  209. /**
  210. * @brief Turns the current task into a sync point.
  211. * @return This flow builder.
  212. */
  213. basic_flow &sync() {
  214. ENTT_ASSERT(index.first() < vertices.size(), "Invalid node");
  215. sync_on = index.first();
  216. for(const auto &elem: deps) {
  217. elem.second.emplace_back(sync_on, true);
  218. }
  219. return *this;
  220. }
  221. /**
  222. * @brief Assigns a resource to the current task with a given access mode.
  223. * @param res Resource identifier.
  224. * @param is_rw Access mode.
  225. * @return This flow builder.
  226. */
  227. basic_flow &set(const id_type res, bool is_rw = false) {
  228. emplace(res, is_rw);
  229. return *this;
  230. }
  231. /**
  232. * @brief Assigns a read-only resource to the current task.
  233. * @param res Resource identifier.
  234. * @return This flow builder.
  235. */
  236. basic_flow &ro(const id_type res) {
  237. emplace(res, false);
  238. return *this;
  239. }
  240. /**
  241. * @brief Assigns a range of read-only resources to the current task.
  242. * @tparam It Type of input iterator.
  243. * @param first An iterator to the first element of the range of elements.
  244. * @param last An iterator past the last element of the range of elements.
  245. * @return This flow builder.
  246. */
  247. template<typename It>
  248. std::enable_if_t<std::is_same_v<std::remove_const_t<typename std::iterator_traits<It>::value_type>, id_type>, basic_flow &>
  249. ro(It first, It last) {
  250. for(; first != last; ++first) {
  251. emplace(*first, false);
  252. }
  253. return *this;
  254. }
  255. /**
  256. * @brief Assigns a writable resource to the current task.
  257. * @param res Resource identifier.
  258. * @return This flow builder.
  259. */
  260. basic_flow &rw(const id_type res) {
  261. emplace(res, true);
  262. return *this;
  263. }
  264. /**
  265. * @brief Assigns a range of writable resources to the current task.
  266. * @tparam It Type of input iterator.
  267. * @param first An iterator to the first element of the range of elements.
  268. * @param last An iterator past the last element of the range of elements.
  269. * @return This flow builder.
  270. */
  271. template<typename It>
  272. std::enable_if_t<std::is_same_v<std::remove_const_t<typename std::iterator_traits<It>::value_type>, id_type>, basic_flow &>
  273. rw(It first, It last) {
  274. for(; first != last; ++first) {
  275. emplace(*first, true);
  276. }
  277. return *this;
  278. }
  279. /**
  280. * @brief Generates a task graph for the current content.
  281. * @return The adjacency matrix of the task graph.
  282. */
  283. [[nodiscard]] graph_type graph() const {
  284. graph_type matrix{vertices.size(), get_allocator()};
  285. setup_graph(matrix);
  286. transitive_closure(matrix);
  287. transitive_reduction(matrix);
  288. return matrix;
  289. }
  290. private:
  291. compressed_pair<size_type, allocator_type> index;
  292. task_container_type vertices;
  293. deps_container_type deps;
  294. size_type sync_on;
  295. };
  296. } // namespace entt
  297. #endif