aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
fusionContext.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of the Fusion Context class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 
29 // =========================================================================
30 #ifndef GUM_FUSION_CONTEXT_H
31 #define GUM_FUSION_CONTEXT_H
32 // =========================================================================
33 #include <agrum/tools/core/multiPriorityQueue.h>
34 // =========================================================================
35 #include <agrum/tools/graphs/graphElements.h>
36 // =========================================================================
37 #include <agrum/FMDP/learning/core/templateStrategy.h>
38 #include <agrum/FMDP/learning/datastructure/leaves/abstractLeaf.h>
39 #include <agrum/FMDP/learning/datastructure/leaves/leafPair.h>
40 // =========================================================================
41 
42 namespace gum {
43 
44  /**
45  * @class FusionContext fusionContext.h
46  * <agrum/FMDP/learning/datastructure/leaves/fusionContext.h>
47  * @brief Contains leaves situation after a merging have been made
48  * @ingroup fmdp_group
49  *
50  */
51 
53 
54 
55  template < bool isInitial = false >
56  class FusionContext {
57  public:
58  // ==========================================================================
59  /// @name Constructor & destructor.
60  // ==========================================================================
61  /// @{
62 
63  // ###################################################################
64  /// Default constructor
65  // ###################################################################
67 
68  // ###################################################################
69  /// Default destructor
70  // ###################################################################
71  ~FusionContext();
72 
73  // ============================================================================
74  /// Allocators and Deallocators redefinition
75  // ============================================================================
76  void* operator new(size_t s) { return SmallObjectAllocator::instance().allocate(s); }
77  void operator delete(void* p) {
78  SmallObjectAllocator::instance().deallocate(p, sizeof(FusionContext));
79  }
80 
81  /// @}
82 
83  // ==========================================================================
84  /// @name Associated Leaves Handling methods
85  // ==========================================================================
86  /// @{
87 
88  // ###################################################################
89  ///
90  // ###################################################################
91  public:
92  bool containsAssociatedLeaf(AbstractLeaf* l) {
93  return _containsAssociatedLeaf_(l, Int2Type< isInitial >());
94  }
95 
96  private:
97  bool _containsAssociatedLeaf_(AbstractLeaf* l, Int2Type< false >) {
98  return _leaf2Pair_.exists(l);
99  }
100  bool _containsAssociatedLeaf_(AbstractLeaf*, Int2Type< true >) { return false; }
101 
102  // ###################################################################
103  ///
104  // ###################################################################
105  public:
106  bool associateLeaf(AbstractLeaf* l) { return _associateLeaf_(l, Int2Type< isInitial >()); }
107 
108  private:
109  bool _associateLeaf_(AbstractLeaf*, Int2Type< false >);
110  bool _associateLeaf_(AbstractLeaf*, Int2Type< true >) { return false; }
111 
112 
113  // ###################################################################
114  ///
115  // ###################################################################
116  public:
117  bool updateAssociatedLeaf(AbstractLeaf* l) {
118  return _updateAssociatedLeaf_(l, Int2Type< isInitial >());
119  }
120 
121  private:
123  bool _updateAssociatedLeaf_(AbstractLeaf*, Int2Type< true >) { return false; }
124 
125  public:
127  return _updateAllAssociatedLeaves_(Int2Type< isInitial >());
128  }
129 
130  private:
131  bool _updateAllAssociatedLeaves_(Int2Type< false >);
132  bool _updateAllAssociatedLeaves_(Int2Type< true >) { return false; }
133 
134 
135  // ###################################################################
136  ///
137  /// @warning : won't delete Associated Pair created (because subsequent
138  /// fusioncontexts might be using it)
139  // ###################################################################
140  public:
141  bool deassociateLeaf(AbstractLeaf* l) { return _deassociateLeaf_(l, Int2Type< isInitial >()); }
142 
143  private:
144  bool _deassociateLeaf_(AbstractLeaf*, Int2Type< false >);
145  bool _deassociateLeaf_(AbstractLeaf*, Int2Type< true >) { return false; }
146 
147  /// @}
148 
149  public:
150  // ==========================================================================
151  /// @name Pair handling methods
152  // ==========================================================================
153  /// @{
154 
155  // ###################################################################
156  ///
157  // ###################################################################
158  bool addPair(LeafPair* p);
159 
160  // ###################################################################
161  ///
162  // ###################################################################
163  bool updatePair(LeafPair* p);
164 
165  // ###################################################################
166  ///
167  // ###################################################################
168  bool removePair(LeafPair* p);
169 
170 
171  pair_iterator beginPairs() { return _pairsHeap_.allValues().beginSafe(); }
172  pair_iterator endPairs() { return _pairsHeap_.allValues().endSafe(); }
173 
174  /// @}
175 
176  // ==========================================================================
177  /// @name Best Pair access methods
178  // ==========================================================================
179  /// @{
180 
181  // ###################################################################
182  ///
183  // ###################################################################
184  LeafPair* top() { return !_pairsHeap_.empty() ? _pairsHeap_.top() : nullptr; }
185 
186  // ###################################################################
187  ///
188  // ###################################################################
189  double topLikelyhood() { return !_pairsHeap_.empty() ? _pairsHeap_.topPriority() : 1.0; }
190 
191  /// @}
192 
193  // ==========================================================================
194  /// @name FusionContext Leaf and associated pairs handling methods
195  // ==========================================================================
196  /// @{
197 
198  // ###################################################################
199  ///
200  // ###################################################################
201  AbstractLeaf* leaf() { return _leaf_; }
202 
203  // ###################################################################
204  ///
205  // ###################################################################
206  LeafPair* leafAssociatedPair(AbstractLeaf* l) { return _leaf2Pair_.getWithDefault(l, nullptr); }
207 
208  // ###################################################################
209  ///
210  // ###################################################################
211  public:
212  Set< LeafPair* > associatedPairs() { return _associatedPairs_(Int2Type< isInitial >()); }
213 
214  private:
215  Set< LeafPair* > _associatedPairs_(Int2Type< false >);
216  Set< LeafPair* > _associatedPairs_(Int2Type< true >) { return Set< LeafPair* >(); }
217  /// @}
218 
219  public:
220  std::string toString();
221 
222  private:
223  MultiPriorityQueue< LeafPair*, double, std::less< double > > _pairsHeap_;
224 
226 
228  };
229 
230 
231 } /* namespace gum */
232 
233 #include <agrum/FMDP/learning/datastructure/leaves/fusionContext_tpl.h>
234 
235 #endif // GUM_FUSION_CONTEXT_H
MultiPriorityQueue< LeafPair *, double, std::less< double > > _pairsHeap_
bool _containsAssociatedLeaf_(AbstractLeaf *, Int2Type< true >)
void operator delete(void *p)
Default constructor.
Definition: fusionContext.h:77
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
bool removePair(LeafPair *p)
Set< LeafPair *> _associatedPairs_(Int2Type< true >)
pair_iterator endPairs()
void * operator new(size_t s)
Allocators and Deallocators redefinition.
Definition: fusionContext.h:76
bool _deassociateLeaf_(AbstractLeaf *, Int2Type< true >)
AbstractLeaf * leaf()
LeafPair * leafAssociatedPair(AbstractLeaf *l)
bool _updateAllAssociatedLeaves_(Int2Type< true >)
Set< LeafPair *> associatedPairs()
FusionContext(AbstractLeaf *)
Default constructor.
HashTable< AbstractLeaf *, LeafPair *> _leaf2Pair_
bool _associateLeaf_(AbstractLeaf *, Int2Type< true >)
bool _updateAssociatedLeaf_(AbstractLeaf *, Int2Type< true >)
bool addPair(LeafPair *p)
AbstractLeaf * _leaf_
bool deassociateLeaf(AbstractLeaf *l)
bool containsAssociatedLeaf(AbstractLeaf *l)
Definition: fusionContext.h:92
LeafPair * top()
bool updateAllAssociatedLeaves()
~FusionContext()
Default destructor.
bool associateLeaf(AbstractLeaf *l)
std::string toString()
bool updateAssociatedLeaf(AbstractLeaf *l)
bool updatePair(LeafPair *p)
pair_iterator beginPairs()