aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
graphElements_inl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
49#include <algorithm>
50#include <iostream>
51
52#include <agrum/agrum.h>
53
54// to facilitate parsing
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59namespace gum {
60
61 /* ==========================================================================*/
62 /* ==========================================================================*/
63 /* === GENERIC UNDIRECTED EDGES IMPLEMENTATION === */
64 /* ==========================================================================*/
65 /* ==========================================================================*/
66
67 // basic constructor
68 INLINE Edge::Edge(NodeId aN1, NodeId aN2) :
69 n1(std::min(aN1, aN2)), n2(std::max(aN1, aN2)) { // for debugging purposes
70 GUM_CONSTRUCTOR(Edge);
71 }
72
73 // copy constructor
74 INLINE Edge::Edge(const Edge& src) : n1(src.n1), n2(src.n2) { // for debugging purposes
75 GUM_CONS_CPY(Edge);
76 }
77
78 // move constructor
79 INLINE Edge::Edge(Edge&& src) noexcept : n1(src.n1), n2(src.n2) { // for debugging purposes
80 GUM_CONS_MOV(Edge);
81 }
82
83 // copy operator
84 INLINE Edge& Edge::operator=(const Edge& src) {
85 // for debugging purposes
86 GUM_OP_CPY(Edge)
87 n1 = src.n1;
88 n2 = src.n2;
89 return *this;
90 }
91
92 // move operator
93 INLINE Edge& Edge::operator=(Edge&& src) noexcept {
94 // for debugging purposes
95 GUM_OP_MOV(Edge);
96 n1 = src.n1;
97 n2 = src.n2;
98 return *this;
99 }
100
101 // destructor
102 INLINE Edge::~Edge() { // for debugging purposes
103 GUM_DESTRUCTOR(Edge);
104 }
105
106 // returns an extremal node of an edge given the ID of the other one
107 INLINE NodeId Edge::other(NodeId id) const {
108 if (id == n1) return n2;
109 else if (id == n2) return n1;
110 else { GUM_ERROR(InvalidNode, id << " does not belong to this edge") }
111 }
112
113 // returns one extremal node ID (whichever one it is is unspecified)
114 INLINE NodeId Edge::first() const { return n1; }
115
116 // returns the second extremal node
117 INLINE NodeId Edge::second() const { return n2; }
118
119 // Returns the value of a key as a Size
120 INLINE Size HashFunc< Edge >::castToSize(const Edge& key) {
121 return Size(key.first()) * HashFuncConst::pi + Size(key.second()) * HashFuncConst::gold;
122 }
123
124 // Computes the hashed value of a key.
125 INLINE Size HashFunc< Edge >::operator()(const Edge& key) const {
126 return castToSize(key) & this->hash_mask_;
127 }
128
129 /* ========================================================================*/
130 /* ========================================================================*/
131 /* === GENERIC DIRECTED EDGES IMPLEMENTATION ===*/
132 /* ========================================================================*/
133 /* ========================================================================*/
134
135 // basic constructor.
136 INLINE Arc::Arc(NodeId tail, NodeId head) : n1(tail), n2(head) { // for debugging purposes
137 GUM_CONSTRUCTOR(Arc);
138 }
139
140 // copy constructor
141 INLINE Arc::Arc(const Arc& src) : n1(src.n1), n2(src.n2) { // for debugging purposes
142 GUM_CONS_CPY(Arc);
143 }
144
145 // move constructor
146 INLINE Arc::Arc(Arc&& src) noexcept : n1(src.n1), n2(src.n2) { // for debugging purposes
147 GUM_CONS_MOV(Arc);
148 }
149
150 // copy operator
151 INLINE Arc& Arc::operator=(const Arc& src) {
152 // for debugging purposes
153 GUM_OP_CPY(Arc);
154 n1 = src.n1;
155 n2 = src.n2;
156 return *this;
157 }
158
159 // move operator
160 INLINE Arc& Arc::operator=(Arc&& src) noexcept {
161 // for debugging purposes
162 GUM_OP_MOV(Arc);
163 n1 = src.n1;
164 n2 = src.n2;
165 return *this;
166 }
167
168 // destructor
169 INLINE Arc::~Arc() { // for debugging purposes
170 GUM_DESTRUCTOR(Arc);
171 }
172
173 // returns the tail of the arc
174 INLINE NodeId Arc::tail() const { return n1; }
175
176 // modifies the tail of the arc
177 INLINE void Arc::_setTail_(NodeId id) { n1 = id; }
178
179 // returns the head of the arc
180 INLINE NodeId Arc::head() const { return n2; }
181
182 // modifies the head of the arc
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 INLINE NodeId Arc::other(NodeId id) const {
187 if (id == n1) return n2;
188 else if (id == n2) return n1;
189 else { GUM_ERROR(InvalidNode, id << " does not belong to this arc") }
190 }
191
192 // returns one extremal node ID (whichever one it is is unspecified)
193 INLINE NodeId Arc::first() const { return n1; }
194
195 // returns the second extremal node
196 INLINE NodeId Arc::second() const { return n2; }
197
198 // reverses the direction of the arc
199 INLINE void Arc::operator-() {
200 NodeId n_temp = n1;
201 n1 = n2;
202 n2 = n_temp;
203 }
204
205 // Returns the value of a key as a Size
206 INLINE Size HashFunc< Arc >::castToSize(const Arc& key) {
207 return Size(key.first()) * HashFuncConst::pi + Size(key.second()) * HashFuncConst::gold;
208 }
209
210 // Computes the hashed value of a key.
211 INLINE Size HashFunc< Arc >::operator()(const Arc& key) const {
212 return castToSize(key) & this->hash_mask_;
213 }
214
215
216} /* namespace gum */
217
218#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Edge(NodeId aN1, NodeId aN2)
constructs a new edge (aN1,aN2)
Exception : node does not exist.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
some utils for topology : NodeId, Edge, Arc and consorts ...
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46