aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DFSCode.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 a DFSCode.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #include <ostream>
30 #include <vector>
31 
32 #include <agrum/PRM/gspan/edgeCode.h>
33 
34 #ifndef GUM_DFS_CODE_H
35 # define GUM_DFS_CODE_H
36 
37 namespace gum {
38  namespace prm {
39  namespace gspan {
40 
41  /**
42  * @class DFSCode
43  * @headerfile DFSCode.h <agrum/PRM/gspan/DFSCode.h>
44  * @brief Reprensent a Depth First Search coding of a graph.
45  *
46  * A DFSCode is composed of EdgeCode. Each EdgeCode is either
47  * a forward edge or a backward edge.
48  *
49  * Regarding memory allocation EdgeCode are shared between related
50  * DFSCode, so delete DFSCode in a bottom up fashion.
51  */
52  class DFSCode {
53  public:
54  /**
55  * Returns true of e2 is a valid neighbor for e1 (i.e. it respect the
56  * neighborhood restriction) if e1 precedes e2 in a DFSCode.
57  *
58  * @param e1 An EdgeCode.
59  * @param e2 Another EdgeCode.
60  * @return Returns true of e2 is a valid neighbor for e1 (i.e. it
61  *respect
62  * the neighborhood restriction) if e1 precedes e2 in a DFSCode.
63  */
64  static bool validNeighbors(EdgeCode* e1, EdgeCode* e2);
65 
66  /**
67  * Default constructor.
68  * Create an empty DFSCode.
69  */
70  DFSCode();
71 
72  /**
73  * @brief Copy constructor.
74  *
75  * Proceeds with a deep copy.
76  */
77  DFSCode(const DFSCode& source);
78 
79  /**
80  * @brief Destructor.
81  * This will delete all children of this DFSCode, with their respective
82  * EdgeCode.
83  */
84  ~DFSCode();
85 
86  /**
87  * The vector containing the EdgeCode composing this DFSCode.
88  */
89  std::vector< EdgeCode* > codes;
90 
91  /**
92  * @brief Copy operator.
93  *
94  * Proceeds with a deep copy.
95  */
96  DFSCode& operator=(const DFSCode& source);
97 
98  /**
99  * Equality operator.
100  * @param code The code tested for equality with this.
101  * @return Returns true if this and code are equal.
102  */
103  bool operator==(const DFSCode& code) const;
104 
105  /**
106  * Difference operator.
107  * @param code The code tested for difference with this.
108  * @return Returns true if this and code are different.
109  */
110  bool operator!=(const DFSCode& code) const;
111 
112  /**
113  * Lesser than operator.
114  * @param code The code on which the test is made.
115  * @return Returns true if this is lesser than code.
116  */
117  bool operator<(const DFSCode& code) const;
118 
119  /**
120  * Lesser or equal than operator.
121  * @param code The code on which the test is made.
122  * @return Returns true if this is lesser than code.
123  */
124  bool operator<=(const DFSCode& code) const;
125 
126  /// Code alias.
127  typedef std::vector< EdgeCode* >::iterator iterator;
128 
129  /// Code alias.
130  typedef std::vector< EdgeCode* >::const_iterator const_iterator;
131  };
132 
133  /**
134  * Print code in out.
135  * @param out The stream in which code is printed.
136  * @param code The printed DFSCode.
137  * @return Returns out after printing code in it.
138  */
139  std::ostream& operator<<(std::ostream& out, const DFSCode& code);
140 
141  inline bool DFSCode::validNeighbors(EdgeCode* e1, EdgeCode* e2) {
142  if (e1->isBackward()) {
143  if (e2->isForward()) {
144  return (e2->i <= e1->i) && (e2->j = (e1->i + 1));
145  } else {
146  return (e2->i == e1->i) && (e1->j < e2->j);
147  }
148  } else {
149  // e1 is a forward edge
150  if (e2->isForward()) {
151  return (e2->i <= e1->j) && (e2->j == (e1->j + 1));
152  } else {
153  return (e2->i == e1->j) && (e2->j < e1->i);
154  }
155  }
156  }
157 
158  } /* namespace gspan */
159  } /* namespace prm */
160 } /* namespace gum */
161 
162 # ifndef GUM_NO_INLINE
163 # include <agrum/PRM/gspan/DFSCode_inl.h>
164 # endif // GUM_NO_INLINE
165 
166 #endif /* GUM_DFS_CODE_H */