Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Automatically remove nodes from parents
[simgrid.git] / src / mc / explo / odpor / WakeupTree.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
7 #define SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
8
9 #include "src/mc/explo/odpor/WakeupTreeIterator.hpp"
10 #include "src/mc/explo/odpor/odpor_forward.hpp"
11
12 #include <memory>
13 #include <optional>
14 #include <unordered_map>
15
16 namespace simgrid::mc::odpor {
17
18 class WakeupTreeNode {
19 private:
20   explicit WakeupTreeNode(const PartialExecution& u) : seq_(u) {}
21   explicit WakeupTreeNode(PartialExecution&& u) : seq_(std::move(u)) {}
22
23   WakeupTreeNode* parent_ = nullptr;
24
25   /** An ordered list of children of for this node in the tree */
26   std::list<WakeupTreeNode*> children_;
27
28   /** @brief The contents of the node */
29   PartialExecution seq_;
30
31   /** Allows the owning tree to insert directly into the child */
32   friend WakeupTree;
33   friend WakeupTreeIterator;
34
35 public:
36   ~WakeupTreeNode();
37   WakeupTreeNode(const WakeupTreeNode&)            = delete;
38   WakeupTreeNode(WakeupTreeNode&&)                 = default;
39   WakeupTreeNode& operator=(const WakeupTreeNode&) = delete;
40   WakeupTreeNode& operator=(WakeupTreeNode&&)      = default;
41
42   const auto begin() const { return this->children_.begin(); }
43   const auto end() const { return this->children_.end(); }
44   const auto rbegin() const { return this->children_.rbegin(); }
45   const auto rend() const { return this->children_.rend(); }
46
47   const PartialExecution& get_sequence() const { return seq_; }
48   const std::list<WakeupTreeNode*>& get_ordered_children() const { return children_; }
49   bool is_leaf() const { return children_.empty(); }
50   bool is_single_process() const { return seq_.size() == static_cast<size_t>(1); }
51   aid_t get_first_actor() const;
52
53   /** Insert a node `node` as a new child of this node */
54   void add_child(WakeupTreeNode* node);
55 };
56
57 class WakeupTree {
58 private:
59   WakeupTreeNode* root_;
60
61   /**
62    * @brief All of the nodes that are currently are a part of the tree
63    *
64    * @invariant Each node event maps itself to the owner of that node,
65    * i.e. the unique pointer that manages the data at the address. The tree owns all
66    * of the addresses that are referenced by the nodes WakeupTreeNode and Configuration.
67    * ODPOR guarantees that nodes are persisted as long as needed.
68    */
69   std::unordered_map<WakeupTreeNode*, std::unique_ptr<WakeupTreeNode>> nodes_;
70
71   void insert_node(std::unique_ptr<WakeupTreeNode> node);
72   void remove_node(WakeupTreeNode* node);
73   bool contains(WakeupTreeNode* node) const;
74
75   /**
76    * @brief Adds a new node to the tree, disconnected from
77    * any other, which represents the partial execution
78    * "fragment" `u`
79    */
80   WakeupTreeNode* make_node(const PartialExecution& u);
81
82   /* Allow the iterator to access the contents of the tree */
83   friend WakeupTreeIterator;
84
85 public:
86   WakeupTree();
87   explicit WakeupTree(std::unique_ptr<WakeupTreeNode> root);
88
89   auto begin() const { return WakeupTreeIterator(*this); }
90   auto end() const { return WakeupTreeIterator(); }
91
92   void remove_subtree_rooted_at(WakeupTreeNode* root);
93   static WakeupTree make_subtree_rooted_at(WakeupTreeNode* root);
94
95   /**
96    * @brief Whether or not this tree is considered empty
97    *
98    * @note Unlike other collection types, a wakeup tree is
99    * considered "empty" if it only contains the root node;
100    * that is, if it is "uninteresting". In such a case,
101    */
102   bool empty() const { return nodes_.size() == static_cast<size_t>(1); }
103
104   /**
105    * @brief Inserts an sequence `seq` of processes into the tree
106    * such that that this tree is a wakeup tree relative to the
107    * given execution
108    */
109   void insert(const Execution& E, const PartialExecution& seq);
110 };
111
112 } // namespace simgrid::mc::odpor
113 #endif