aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
graphElements_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 /** @file
23  * @brief Inlined implementation of directed and undirected edges
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #include <algorithm>
28 #include <iostream>
29 
30 #include <agrum/agrum.h>
31 
32 // to facilitate parsing
33 #include <agrum/tools/graphs/graphElements.h>
34 
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 
37 namespace gum {
38 
39  /* ===========================================================================
40  */
41  /* ===========================================================================
42  */
43  /* === GENERIC UNDIRECTED EDGES IMPLEMENTATION ===
44  */
45  /* ===========================================================================
46  */
47  /* ===========================================================================
48  */
49 
50  /// basic constructor
51 
52  INLINE Edge::Edge(NodeId aN1, NodeId aN2) :
53  n1(std::min(aN1, aN2)), n2(std::max(aN1, aN2)) {
54  // for debugging purposes
55  GUM_CONSTRUCTOR(Edge);
56  }
57 
58  /// copy constructor
59 
60  INLINE Edge::Edge(const Edge& src) : n1(src.n1), n2(src.n2) {
61  // for debugging purposes
62  GUM_CONS_CPY(Edge);
63  }
64 
65  /// copy operator
66 
67  INLINE Edge& Edge::operator=(const Edge& src) {
68  // for debugging purposes
69  GUM_OP_CPY(Edge);
70  n1 = src.n1;
71  n2 = src.n2;
72  return *this;
73  }
74 
75  /// destructor
76 
77  INLINE Edge::~Edge() {
78  // for debugging purposes
79  GUM_DESTRUCTOR(Edge);
80  }
81 
82  /// returns an extremal node of an edge given the ID of the other one
83 
84  INLINE NodeId Edge::other(NodeId id) const {
85  if (id == n1)
86  return n2;
87  else if (id == n2)
88  return n1;
89  else {
90  GUM_ERROR(IdError, id << " does not belong to this edge");
91  }
92  }
93 
94  /// returns one extremal node ID (whichever one it is is unspecified)
95 
96  INLINE NodeId Edge::first() const { return n1; }
97 
98  /// returns the second extremal node
99 
100  INLINE NodeId Edge::second() const { return n2; }
101 
102  /// check if two undirected edges are equal
103 
104  INLINE bool Edge::operator==(const Edge& src) const {
105  return ((n1 == src.n1) && (n2 == src.n2));
106  }
107 
108  /// check if two undirected edges are different
109 
110  INLINE bool Edge::operator!=(const Edge& src) const {
111  return ((n1 != src.n1) || (n2 != src.n2));
112  }
113 
114 
115  // Returns the value of a key as a Size
116  INLINE Size HashFunc< Edge >::castToSize(const Edge& key) {
117  return Size(key.first()) * HashFuncConst::pi
118  + Size(key.second()) * HashFuncConst::gold;
119  }
120 
121  // Computes the hashed value of a key.
122  INLINE Size HashFunc< Edge >::operator()(const Edge& key) const {
123  return castToSize(key) & this->hash_mask_;
124  }
125 
126 
127  /* ===========================================================================
128  */
129  /* ===========================================================================
130  */
131  /* === GENERIC DIRECTED EDGES IMPLEMENTATION ===
132  */
133  /* ===========================================================================
134  */
135  /* ===========================================================================
136  */
137 
138  /// basic constructor.
139 
140  INLINE Arc::Arc(NodeId tail, NodeId head) : n1(tail), n2(head) {
141  // for debugging purposes
142  GUM_CONSTRUCTOR(Arc);
143  }
144 
145  /// copy constructor
146 
147  INLINE Arc::Arc(const Arc& src) : n1(src.n1), n2(src.n2) {
148  // for debugging purposes
149  GUM_CONS_CPY(Arc);
150  }
151 
152  /// copy operator
153 
154  INLINE Arc& Arc::operator=(const Arc& src) {
155  // for debugging purposes
156  GUM_OP_CPY(Arc);
157  n1 = src.n1;
158  n2 = src.n2;
159  return *this;
160  }
161 
162  /// destructor
163 
164  INLINE Arc::~Arc() {
165  // for debugging purposes
166  GUM_DESTRUCTOR(Arc);
167  }
168 
169  /// returns the tail of the arc
170 
171  INLINE NodeId Arc::tail() const { return n1; }
172 
173  /// modifies the tail of the arc
174 
175  INLINE void Arc::setTail__(NodeId id) { n1 = id; }
176 
177  /// returns the head of the arc
178 
179  INLINE NodeId Arc::head() const { return n2; }
180 
181  /// modifies the head of the arc
182 
183  INLINE void Arc::setHead__(NodeId id) { n2 = id; }
184 
185  /// returns an extremal node of an edge given the ID of the other one
186 
187  INLINE NodeId Arc::other(NodeId id) const {
188  if (id == n1)
189  return n2;
190  else if (id == n2)
191  return n1;
192  else {
193  GUM_ERROR(IdError, id << " does not belong to this arc");
194  }
195  }
196 
197  /// returns one extremal node ID (whichever one it is is unspecified)
198 
199  INLINE NodeId Arc::first() const { return n1; }
200 
201  /// returns the second extremal node
202 
203  INLINE NodeId Arc::second() const { return n2; }
204 
205  /// check if two arcs are equal
206 
207  INLINE bool Arc::operator==(const Arc& src) const {
208  return ((n1 == src.n1) && (n2 == src.n2));
209  }
210 
211  /// check if two arcs are different
212 
213  INLINE bool Arc::operator!=(const Arc& src) const {
214  return ((n1 != src.n1) || (n2 != src.n2));
215  }
216 
217  /// reverses the direction of the arc
218 
219  INLINE void Arc::operator-() {
220  NodeId n_temp = n1;
221  n1 = n2;
222  n2 = n_temp;
223  }
224 
225 
226  // Returns the value of a key as a Size
227  INLINE Size HashFunc< Arc >::castToSize(const Arc& key) {
228  return Size(key.first()) * HashFuncConst::pi
229  + Size(key.second()) * HashFuncConst::gold;
230  }
231 
232  // Computes the hashed value of a key.
233  INLINE Size HashFunc< Arc >::operator()(const Arc& key) const {
234  return castToSize(key) & this->hash_mask_;
235  }
236 
237 
238 } /* namespace gum */
239 
240 #endif /* DOXYGEN_SHOULD_SKIP_THIS */