aGrUM  0.14.2
graphElements_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN and Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
25 #include <algorithm>
26 #include <iostream>
27 
28 #include <agrum/agrum.h>
29 
30 // to facilitate parsing
32 
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 
35 namespace gum {
36 
37  /* ===========================================================================
38  */
39  /* ===========================================================================
40  */
41  /* === GENERIC UNDIRECTED EDGES IMPLEMENTATION ===
42  */
43  /* ===========================================================================
44  */
45  /* ===========================================================================
46  */
47 
49 
50  INLINE Edge::Edge(NodeId aN1, NodeId aN2) :
51  n1(std::min(aN1, aN2)), n2(std::max(aN1, aN2)) {
52  // for debugging purposes
53  GUM_CONSTRUCTOR(Edge);
54  }
55 
57 
58  INLINE Edge::Edge(const Edge& src) : n1(src.n1), n2(src.n2) {
59  // for debugging purposes
60  GUM_CONS_CPY(Edge);
61  }
62 
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 
74 
75  INLINE Edge::~Edge() {
76  // for debugging purposes
77  GUM_DESTRUCTOR(Edge);
78  }
79 
81 
82  INLINE NodeId Edge::other(NodeId id) const {
83  if (id == n1)
84  return n2;
85  else if (id == n2)
86  return n1;
87  else {
88  GUM_ERROR(IdError, id << " does not belong to this edge");
89  }
90  }
91 
93 
94  INLINE NodeId Edge::first() const { return n1; }
95 
97 
98  INLINE NodeId Edge::second() const { return n2; }
99 
101 
102  INLINE bool Edge::operator==(const Edge& src) const {
103  return ((n1 == src.n1) && (n2 == src.n2));
104  }
105 
107 
108  INLINE bool Edge::operator!=(const Edge& src) const {
109  return ((n1 != src.n1) || (n2 != src.n2));
110  }
111 
112 
113  // Returns the value of a key as a Size
114  INLINE Size HashFunc< Edge >::castToSize(const Edge& key) {
115  return Size(key.first()) * HashFuncConst::pi
116  + Size(key.second()) * HashFuncConst::gold;
117  }
118 
119  // Computes the hashed value of a key.
120  INLINE Size HashFunc< Edge >::operator()(const Edge& key) const {
121  return castToSize(key) & this->_hash_mask;
122  }
123 
124 
125  /* ===========================================================================
126  */
127  /* ===========================================================================
128  */
129  /* === GENERIC DIRECTED EDGES IMPLEMENTATION ===
130  */
131  /* ===========================================================================
132  */
133  /* ===========================================================================
134  */
135 
137 
138  INLINE Arc::Arc(NodeId tail, NodeId head) : n1(tail), n2(head) {
139  // for debugging purposes
140  GUM_CONSTRUCTOR(Arc);
141  }
142 
144 
145  INLINE Arc::Arc(const Arc& src) : n1(src.n1), n2(src.n2) {
146  // for debugging purposes
147  GUM_CONS_CPY(Arc);
148  }
149 
151 
152  INLINE Arc& Arc::operator=(const Arc& src) {
153  // for debugging purposes
154  GUM_OP_CPY(Arc);
155  n1 = src.n1;
156  n2 = src.n2;
157  return *this;
158  }
159 
161 
162  INLINE Arc::~Arc() {
163  // for debugging purposes
164  GUM_DESTRUCTOR(Arc);
165  }
166 
168 
169  INLINE NodeId Arc::tail() const { return n1; }
170 
172 
173  INLINE void Arc::__setTail(NodeId id) { n1 = id; }
174 
176 
177  INLINE NodeId Arc::head() const { return n2; }
178 
180 
181  INLINE void Arc::__setHead(NodeId id) { n2 = id; }
182 
184 
185  INLINE NodeId Arc::other(NodeId id) const {
186  if (id == n1)
187  return n2;
188  else if (id == n2)
189  return n1;
190  else {
191  GUM_ERROR(IdError, id << " does not belong to this arc");
192  }
193  }
194 
196 
197  INLINE NodeId Arc::first() const { return n1; }
198 
200 
201  INLINE NodeId Arc::second() const { return n2; }
202 
204 
205  INLINE bool Arc::operator==(const Arc& src) const {
206  return ((n1 == src.n1) && (n2 == src.n2));
207  }
208 
210 
211  INLINE bool Arc::operator!=(const Arc& src) const {
212  return ((n1 != src.n1) || (n2 != src.n2));
213  }
214 
216 
217  INLINE void Arc::operator-() {
218  NodeId n_temp = n1;
219  n1 = n2;
220  n2 = n_temp;
221  }
222 
223 
224  // Returns the value of a key as a Size
225  INLINE Size HashFunc< Arc >::castToSize(const Arc& key) {
226  return Size(key.first()) * HashFuncConst::pi
227  + Size(key.second()) * HashFuncConst::gold;
228  }
229 
230  // Computes the hashed value of a key.
231  INLINE Size HashFunc< Arc >::operator()(const Arc& key) const {
232  return castToSize(key) & this->_hash_mask;
233  }
234 
235 
236 } /* namespace gum */
237 
238 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
static constexpr Size pi
Definition: hashFunc.h:76
NodeId second() const
returns the node ID of the other extremal node ID
void __setTail(NodeId id)
modifies the tail of the arc
static constexpr Size gold
Definition: hashFunc.h:74
NodeId n1
the extremal nodes of the edge (their order is unimportant)
Edge & operator=(const Edge &src)
copy operator
NodeId n2
bool operator==(const Arc &src) const
checks whether two arcs are equal
STL namespace.
NodeId other(NodeId id) const
returns an extremal node of an edge given the ID of the other one
Edge(NodeId aN1, NodeId aN2)
constructs a new edge (aN1,aN2)
bool operator!=(const Arc &src) const
check if two arcs are different
~Edge()
destructor
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void operator-()
reverses the direction of the arc
NodeId head() const
returns the head of the arc
NodeId second() const
returns the node ID of the other extremal node ID
NodeId first() const
returns one extremal node ID (whichever one it is is unspecified)
Arc(NodeId tail, NodeId head)
basic constructor. Creates tail -> head.
bool operator!=(const Edge &src) const
checks whether two undirected edges are different
void __setHead(NodeId id)
modifies the head of the arc
~Arc()
destructor
Arc & operator=(const Arc &src)
copy operator
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
NodeId n1
the extremal nodes of the edge (their order is unimportant)
bool operator==(const Edge &src) const
checks whether two undirected edges are equal
NodeId first() const
returns one extremal node ID (whichever one it is is unspecified)
Size NodeId
Type for node ids.
Definition: graphElements.h:97
NodeId tail() const
returns the tail of the arc
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
NodeId other(NodeId id) const
returns an extremal node of an edge given the ID of the other one
some utils for topology : NodeId, Edge, Arc and consorts ...