aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
graphElements_inl.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 /** @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)) { // for debugging purposes
54  GUM_CONSTRUCTOR(Edge);
55  }
56 
57  /// copy constructor
58 
59  INLINE Edge::Edge(const Edge& src) : n1(src.n1), n2(src.n2) { // for debugging purposes
60  GUM_CONS_CPY(Edge);
61  }
62 
63  /// copy operator
64 
65  INLINE Edge& Edge::operator=(const Edge& src) {
66  // for debugging purposes
67  GUM_OP_CPY(Edge);
68  n1 = src.n1;
69  n2 = src.n2;
70  return *this;
71  }
72 
73  /// destructor
74 
75  INLINE Edge::~Edge() { // for debugging purposes
76  GUM_DESTRUCTOR(Edge);
77  }
78 
79  /// returns an extremal node of an edge given the ID of the other one
80 
81  INLINE NodeId Edge::other(NodeId id) const {
82  if (id == n1)
83  return n2;
84  else if (id == n2)
85  return n1;
86  else {
87  GUM_ERROR(IdError, id << " does not belong to this edge")
88  }
89  }
90 
91  /// returns one extremal node ID (whichever one it is is unspecified)
92 
93  INLINE NodeId Edge::first() const { return n1; }
94 
95  /// returns the second extremal node
96 
97  INLINE NodeId Edge::second() const { return n2; }
98 
99  /// check if two undirected edges are equal
100 
101  INLINE bool Edge::operator==(const Edge& src) const { return ((n1 == src.n1) && (n2 == src.n2)); }
102 
103  /// check if two undirected edges are different
104 
105  INLINE bool Edge::operator!=(const Edge& src) const { return ((n1 != src.n1) || (n2 != src.n2)); }
106 
107 
108  // Returns the value of a key as a Size
109  INLINE Size HashFunc< Edge >::castToSize(const Edge& key) {
110  return Size(key.first()) * HashFuncConst::pi + Size(key.second()) * HashFuncConst::gold;
111  }
112 
113  // Computes the hashed value of a key.
114  INLINE Size HashFunc< Edge >::operator()(const Edge& key) const {
115  return castToSize(key) & this->hash_mask_;
116  }
117 
118 
119  /* ===========================================================================
120  */
121  /* ===========================================================================
122  */
123  /* === GENERIC DIRECTED EDGES IMPLEMENTATION ===
124  */
125  /* ===========================================================================
126  */
127  /* ===========================================================================
128  */
129 
130  /// basic constructor.
131 
132  INLINE Arc::Arc(NodeId tail, NodeId head) : n1(tail), n2(head) { // for debugging purposes
133  GUM_CONSTRUCTOR(Arc);
134  }
135 
136  /// copy constructor
137 
138  INLINE Arc::Arc(const Arc& src) : n1(src.n1), n2(src.n2) { // for debugging purposes
139  GUM_CONS_CPY(Arc);
140  }
141 
142  /// copy operator
143 
144  INLINE Arc& Arc::operator=(const Arc& src) {
145  // for debugging purposes
146  GUM_OP_CPY(Arc);
147  n1 = src.n1;
148  n2 = src.n2;
149  return *this;
150  }
151 
152  /// destructor
153 
154  INLINE Arc::~Arc() { // for debugging purposes
155  GUM_DESTRUCTOR(Arc);
156  }
157 
158  /// returns the tail of the arc
159 
160  INLINE NodeId Arc::tail() const { return n1; }
161 
162  /// modifies the tail of the arc
163 
164  INLINE void Arc::_setTail_(NodeId id) { n1 = id; }
165 
166  /// returns the head of the arc
167 
168  INLINE NodeId Arc::head() const { return n2; }
169 
170  /// modifies the head of the arc
171 
172  INLINE void Arc::_setHead_(NodeId id) { n2 = id; }
173 
174  /// returns an extremal node of an edge given the ID of the other one
175 
176  INLINE NodeId Arc::other(NodeId id) const {
177  if (id == n1)
178  return n2;
179  else if (id == n2)
180  return n1;
181  else {
182  GUM_ERROR(IdError, id << " does not belong to this arc")
183  }
184  }
185 
186  /// returns one extremal node ID (whichever one it is is unspecified)
187 
188  INLINE NodeId Arc::first() const { return n1; }
189 
190  /// returns the second extremal node
191 
192  INLINE NodeId Arc::second() const { return n2; }
193 
194  /// check if two arcs are equal
195 
196  INLINE bool Arc::operator==(const Arc& src) const { return ((n1 == src.n1) && (n2 == src.n2)); }
197 
198  /// check if two arcs are different
199 
200  INLINE bool Arc::operator!=(const Arc& src) const { return ((n1 != src.n1) || (n2 != src.n2)); }
201 
202  /// reverses the direction of the arc
203 
204  INLINE void Arc::operator-() {
205  NodeId n_temp = n1;
206  n1 = n2;
207  n2 = n_temp;
208  }
209 
210 
211  // Returns the value of a key as a Size
212  INLINE Size HashFunc< Arc >::castToSize(const Arc& key) {
213  return Size(key.first()) * HashFuncConst::pi + Size(key.second()) * HashFuncConst::gold;
214  }
215 
216  // Computes the hashed value of a key.
217  INLINE Size HashFunc< Arc >::operator()(const Arc& key) const {
218  return castToSize(key) & this->hash_mask_;
219  }
220 
221 
222 } /* namespace gum */
223 
224 #endif /* DOXYGEN_SHOULD_SKIP_THIS */