aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
48#include <algorithm>
49#include <iostream>
50
51#include <agrum/agrum.h>
52
53// to facilitate parsing
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58namespace gum {
59
60 /* ==========================================================================*/
61 /* ==========================================================================*/
62 /* === GENERIC UNDIRECTED EDGES IMPLEMENTATION === */
63 /* ==========================================================================*/
64 /* ==========================================================================*/
65
66 // basic constructor
67 INLINE Edge::Edge(NodeId aN1, NodeId aN2) :
68 n1(std::min(aN1, aN2)), n2(std::max(aN1, aN2)) { // for debugging purposes
69 GUM_CONSTRUCTOR(Edge);
70 }
71
72 // copy constructor
73 INLINE Edge::Edge(const Edge& src) : n1(src.n1), n2(src.n2) { // for debugging purposes
74 GUM_CONS_CPY(Edge);
75 }
76
77 // copy operator
78 INLINE Edge& Edge::operator=(const Edge& src) {
79 // for debugging purposes
80 GUM_OP_CPY(Edge)
81 n1 = src.n1;
82 n2 = src.n2;
83 return *this;
84 }
85
86 // destructor
87 INLINE Edge::~Edge() { // for debugging purposes
88 GUM_DESTRUCTOR(Edge);
89 }
90
91 // returns an extremal node of an edge given the ID of the other one
92 INLINE NodeId Edge::other(NodeId id) const {
93 if (id == n1) return n2;
94 else if (id == n2) return n1;
95 else { GUM_ERROR(InvalidNode, id << " does not belong to this edge") }
96 }
97
98 // returns one extremal node ID (whichever one it is is unspecified)
99 INLINE NodeId Edge::first() const { return n1; }
100
101 // returns the second extremal node
102 INLINE NodeId Edge::second() const { return n2; }
103
104 // Returns the value of a key as a Size
105 INLINE Size HashFunc< Edge >::castToSize(const Edge& key) {
106 return Size(key.first()) * HashFuncConst::pi + Size(key.second()) * HashFuncConst::gold;
107 }
108
109 // Computes the hashed value of a key.
110 INLINE Size HashFunc< Edge >::operator()(const Edge& key) const {
111 return castToSize(key) & this->hash_mask_;
112 }
113
114 /* ========================================================================*/
115 /* ========================================================================*/
116 /* === GENERIC DIRECTED EDGES IMPLEMENTATION ===*/
117 /* ========================================================================*/
118 /* ========================================================================*/
119
120 // basic constructor.
121 INLINE Arc::Arc(NodeId tail, NodeId head) : n1(tail), n2(head) { // for debugging purposes
122 GUM_CONSTRUCTOR(Arc);
123 }
124
125 // copy constructor
126 INLINE Arc::Arc(const Arc& src) : n1(src.n1), n2(src.n2) { // for debugging purposes
127 GUM_CONS_CPY(Arc);
128 }
129
130 // copy operator
131 INLINE Arc& Arc::operator=(const Arc& src) {
132 // for debugging purposes
133 GUM_OP_CPY(Arc);
134 n1 = src.n1;
135 n2 = src.n2;
136 return *this;
137 }
138
139 // destructor
140 INLINE Arc::~Arc() { // for debugging purposes
141 GUM_DESTRUCTOR(Arc);
142 }
143
144 // returns the tail of the arc
145 INLINE NodeId Arc::tail() const { return n1; }
146
147 // modifies the tail of the arc
148 INLINE void Arc::_setTail_(NodeId id) { n1 = id; }
149
150 // returns the head of the arc
151 INLINE NodeId Arc::head() const { return n2; }
152
153 // modifies the head of the arc
154 INLINE void Arc::_setHead_(NodeId id) { n2 = id; }
155
156 // returns an extremal node of an edge given the ID of the other one
157 INLINE NodeId Arc::other(NodeId id) const {
158 if (id == n1) return n2;
159 else if (id == n2) return n1;
160 else { GUM_ERROR(InvalidNode, id << " does not belong to this arc") }
161 }
162
163 // returns one extremal node ID (whichever one it is is unspecified)
164 INLINE NodeId Arc::first() const { return n1; }
165
166 // returns the second extremal node
167 INLINE NodeId Arc::second() const { return n2; }
168
169 // reverses the direction of the arc
170 INLINE void Arc::operator-() {
171 NodeId n_temp = n1;
172 n1 = n2;
173 n2 = n_temp;
174 }
175
176 // Returns the value of a key as a Size
177 INLINE Size HashFunc< Arc >::castToSize(const Arc& key) {
178 return Size(key.first()) * HashFuncConst::pi + Size(key.second()) * HashFuncConst::gold;
179 }
180
181 // Computes the hashed value of a key.
182 INLINE Size HashFunc< Arc >::operator()(const Arc& key) const {
183 return castToSize(key) & this->hash_mask_;
184 }
185
186
187} /* namespace gum */
188
189#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:72
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