Logo AND Algorithmique Numérique Distribuée

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