aGrUM  0.16.0
treeOperator_tpl.h
Go to the documentation of this file.
1 
33 
34 #define ALLOCATE(x) SmallObjectAllocator::instance().allocate(x)
35 #define DEALLOCATE(x, y) SmallObjectAllocator::instance().deallocate(x, y)
36 
37 namespace gum {
38 
39  template < typename GUM_SCALAR,
40  template < typename >
41  class COMBINEOPERATOR,
42  template < typename >
43  class TerminalNodePolicy >
44  INLINE
48  __dt1(dt1),
49  __dt2(dt2), __combine() {
50  GUM_CONSTRUCTOR(TreeOperator);
51 
52  __rd =
54  }
55 
56  template < typename GUM_SCALAR,
57  template < typename >
58  class COMBINEOPERATOR,
59  template < typename >
60  class TerminalNodePolicy >
61  INLINE
65  const HashTable< const DiscreteVariable*, Idx > givenContext) :
66  __dt1(dt1),
67  __dt2(dt2), __combine(), __context(givenContext) {
68  GUM_CONSTRUCTOR(TreeOperator);
69 
70  __rd =
72  }
73 
74  template < typename GUM_SCALAR,
75  template < typename >
76  class COMBINEOPERATOR,
77  template < typename >
78  class TerminalNodePolicy >
81  GUM_DESTRUCTOR(TreeOperator);
82  }
83 
84  // This function is the main function. To be call every time an operation
85  // between the two given Function Graphs is required
86  template < typename GUM_SCALAR,
87  template < typename >
88  class COMBINEOPERATOR,
89  template < typename >
90  class TerminalNodePolicy >
93  __rd->manager()->setRootNode(__xPloreDT1(__dt1->root()));
94 
95  return __rd;
96  }
97 
98  // Main recursion function, called every time we move on a node to determine
99  // what we have to do
100  template < typename GUM_SCALAR,
101  template < typename >
102  class COMBINEOPERATOR,
103  template < typename >
104  class TerminalNodePolicy >
105  INLINE NodeId
107  NodeId currentNodeId) {
108  if (__dt1->isTerminalNode(currentNodeId)) {
109  __curDT1Leaf = currentNodeId;
110  return __xPloreDT2(__dt2->root());
111  }
112 
113  const InternalNode* currentNode = __dt1->node(currentNodeId);
114 
115  if (!__rd->variablesSequence().exists(currentNode->nodeVar()))
116  __rd->add(*(currentNode->nodeVar()));
117 
118  NodeId* sonsMap = static_cast< NodeId* >(
119  ALLOCATE(sizeof(NodeId) * currentNode->nodeVar()->domainSize()));
120  for (Idx moda = 0; moda < currentNode->nodeVar()->domainSize(); ++moda) {
121  __context.insert(currentNode->nodeVar(), moda);
122  sonsMap[moda] = __xPloreDT1(currentNode->son(moda));
123  __context.erase(currentNode->nodeVar());
124  }
125  return __checkRedundancy(currentNode->nodeVar(), sonsMap);
126  }
127 
128  template < typename GUM_SCALAR,
129  template < typename >
130  class COMBINEOPERATOR,
131  template < typename >
132  class TerminalNodePolicy >
133  INLINE NodeId
135  NodeId currentNodeId) {
136  if (__dt2->isTerminalNode(currentNodeId))
137  return __rd->manager()->addTerminalNode(__combine(
138  __dt1->nodeValue(__curDT1Leaf), __dt2->nodeValue(currentNodeId)));
139 
140  const InternalNode* currentNode = __dt2->node(currentNodeId);
141 
142  if (!__rd->variablesSequence().exists(currentNode->nodeVar()))
143  __rd->add(*(currentNode->nodeVar()));
144 
145  if (__context.exists(currentNode->nodeVar()))
146  return __xPloreDT2(currentNode->son(__context[currentNode->nodeVar()]));
147 
148  NodeId* sonsMap = static_cast< NodeId* >(
149  ALLOCATE(sizeof(NodeId) * currentNode->nodeVar()->domainSize()));
150  for (Idx moda = 0; moda < currentNode->nodeVar()->domainSize(); ++moda) {
151  __context.insert(currentNode->nodeVar(), moda);
152  sonsMap[moda] = __xPloreDT2(currentNode->son(moda));
153  __context.erase(currentNode->nodeVar());
154  }
155  return __checkRedundancy(currentNode->nodeVar(), sonsMap);
156  }
157 
158  template < typename GUM_SCALAR,
159  template < typename >
160  class COMBINEOPERATOR,
161  template < typename >
162  class TerminalNodePolicy >
165  bool diff = false;
166  for (Idx moda = 1; moda < var->domainSize() && !diff; ++moda)
167  if (sonsMap[0] != sonsMap[moda]) diff = true;
168 
169  if (!diff) {
170  NodeId zero = sonsMap[0];
171  DEALLOCATE(sonsMap, sizeof(NodeId) * var->domainSize());
172  return zero;
173  }
174 
175  return __rd->manager()->addInternalNode(var, sonsMap);
176  }
177 
178 } // namespace gum
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const DiscreteVariable * nodeVar() const
Returns the node variable.
NodeId __checkRedundancy(const DiscreteVariable *, NodeId *)
NodeId son(Idx modality) const
Returns the son at a given index.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Class used to perform Decision Tree Operation in the FMDP Framework.
Definition: treeOperator.h:53
HashTable< const DiscreteVariable *, Idx > __context
Definition: treeOperator.h:111
NodeId __xPloreDT2(NodeId currentNodeId)
The main recursion function.
The class for generic Hash Tables.
Definition: hashTable.h:679
virtual Size domainSize() const =0
NodeId __xPloreDT1(NodeId currentNodeId)
The main recursion function.
const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __dt1
The two function graphs used for the operation.
Definition: treeOperator.h:102
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * compute()
Computes and builds the Function Graph that is the result of the operation.
Structure used to represent a node internal structure.
Definition: internalNode.h:102
Class implementingting a function graph.
#define DEALLOCATE(x, y)
const COMBINEOPERATOR< GUM_SCALAR > __combine
The function to be performed on the leaves.
Definition: treeOperator.h:109
~TreeOperator()
Default destructor.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __rd
The resulting function graph.
Definition: treeOperator.h:106
TreeOperator(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *dt1, const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *dt2)
Default constructor.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getTreeInstance()
Returns an arborescent instance.
const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __dt2
Definition: treeOperator.h:103
Size Idx
Type for indexes.
Definition: types.h:53
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define ALLOCATE(x)