Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use `std::shared_ptr<Transition>` for Execution
[simgrid.git] / src / mc / explo / odpor / WakeupTree.hpp
index 81356cd4cf939f5c32693fcdeb31d588708d3ea3..7a6eb272bcf97757c6ce124864d89630c583c678 100644 (file)
@@ -6,37 +6,57 @@
 #ifndef SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
 #define SIMGRID_MC_ODPOR_WAKEUP_TREE_HPP
 
+#include "src/mc/explo/odpor/WakeupTreeIterator.hpp"
 #include "src/mc/explo/odpor/odpor_forward.hpp"
 
 #include <memory>
+#include <optional>
 #include <unordered_map>
 
 namespace simgrid::mc::odpor {
 
 class WakeupTreeNode {
 private:
+  explicit WakeupTreeNode(const PartialExecution& u) : seq_(u) {}
+  explicit WakeupTreeNode(PartialExecution&& u) : seq_(std::move(u)) {}
+
+  WakeupTreeNode* parent_ = nullptr;
+
   /** An ordered list of children of for this node in the tree */
-  std::list<const WakeupTreeNode*> children_;
+  std::list<WakeupTreeNode*> children_;
 
   /** @brief The contents of the node */
-  ProcessSequence seq_;
+  PartialExecution seq_;
+
+  /** Allows the owning tree to insert directly into the child */
+  friend WakeupTree;
+  friend WakeupTreeIterator;
 
 public:
+  ~WakeupTreeNode();
+  WakeupTreeNode(const WakeupTreeNode&)            = delete;
+  WakeupTreeNode(WakeupTreeNode&&)                 = default;
+  WakeupTreeNode& operator=(const WakeupTreeNode&) = delete;
+  WakeupTreeNode& operator=(WakeupTreeNode&&)      = default;
+
   const auto begin() const { return this->children_.begin(); }
   const auto end() const { return this->children_.end(); }
   const auto rbegin() const { return this->children_.rbegin(); }
   const auto rend() const { return this->children_.rend(); }
 
-  const ProcessSequence& get_sequence() const { return seq_; }
-  const std::list<const WakeupTreeNode*>& get_ordered_children() const { return children_; }
-
+  const PartialExecution& get_sequence() const { return seq_; }
+  const std::list<WakeupTreeNode*>& get_ordered_children() const { return children_; }
   bool is_leaf() const { return children_.empty(); }
+  bool is_single_process() const { return seq_.size() == static_cast<size_t>(1); }
+  aid_t get_first_actor() const;
+
+  /** Insert a node `node` as a new child of this node */
+  void add_child(WakeupTreeNode* node);
 };
 
 class WakeupTree {
 private:
-  /** @brief The root node of the tree */
-  const WakeupTreeNode* const root;
+  WakeupTreeNode* root_;
 
   /**
    * @brief All of the nodes that are currently are a part of the tree
@@ -46,20 +66,47 @@ private:
    * of the addresses that are referenced by the nodes WakeupTreeNode and Configuration.
    * ODPOR guarantees that nodes are persisted as long as needed.
    */
-  std::unordered_map<const WakeupTreeNode*, std::unique_ptr<WakeupTreeNode>> nodes_;
+  std::unordered_map<WakeupTreeNode*, std::unique_ptr<WakeupTreeNode>> nodes_;
+
+  void insert_node(std::unique_ptr<WakeupTreeNode> node);
+  void remove_node(WakeupTreeNode* node);
+  bool contains(WakeupTreeNode* node) const;
+
+  /**
+   * @brief Adds a new node to the tree, disconnected from
+   * any other, which represents the partial execution
+   * "fragment" `u`
+   */
+  WakeupTreeNode* make_node(const PartialExecution& u);
 
   /* Allow the iterator to access the contents of the tree */
   friend WakeupTreeIterator;
 
 public:
   WakeupTree();
+  explicit WakeupTree(std::unique_ptr<WakeupTreeNode> root);
+
+  auto begin() const { return WakeupTreeIterator(*this); }
+  auto end() const { return WakeupTreeIterator(); }
+
+  void remove_subtree_rooted_at(WakeupTreeNode* root);
+  static WakeupTree make_subtree_rooted_at(WakeupTreeNode* root);
+
+  /**
+   * @brief Whether or not this tree is considered empty
+   *
+   * @note Unlike other collection types, a wakeup tree is
+   * considered "empty" if it only contains the root node;
+   * that is, if it is "uninteresting". In such a case,
+   */
+  bool empty() const { return nodes_.size() == static_cast<size_t>(1); }
 
   /**
    * @brief Inserts an sequence `seq` of processes into the tree
    * such that that this tree is a wakeup tree relative to the
    * given execution
    */
-  void insert(const Execution&, const ExecutionSequence& seq);
+  void insert(const Execution& E, const PartialExecution& seq);
 };
 
 } // namespace simgrid::mc::odpor