Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_indexArc_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <stdexcept>
41
42#include <tpl_graph.H>
43#include <tpl_indexArc.H>
44
45using namespace Aleph;
46
47namespace
48{
51
52 struct EvenArcInfo
53 {
54 bool operator()(UGraph::Arc* a) const noexcept
55 {
56 return (a->get_info() % 2) == 0;
57 }
58
59 bool operator()(const UGraph&, UGraph::Arc* a) const noexcept
60 {
61 return (*this)(a);
62 }
63 };
64}
65
67{
68 UGraph g;
69 auto n1 = g.insert_node(1);
70 auto n2 = g.insert_node(2);
71 auto a = g.insert_arc(n1, n2, 10);
72
73 IndexArc<UGraph> idx(g, true);
74
75 EXPECT_EQ(idx.size(), 1u);
76 EXPECT_EQ(idx.search(n1, n2), a);
77 EXPECT_EQ(idx.search(n2, n1), a);
78 EXPECT_EQ(idx.search_directed(n1, n2), a);
79 EXPECT_EQ(idx.search_directed(n2, n1), a);
80}
81
83{
84 DGraph g;
85 auto n1 = g.insert_node(1);
86 auto n2 = g.insert_node(2);
87 auto a12 = g.insert_arc(n1, n2, 10);
88
89 IndexArc<DGraph> idx(g, true);
90
91 EXPECT_EQ(idx.size(), 1u);
92 EXPECT_EQ(idx.search(n1, n2), a12);
93 EXPECT_EQ(idx.search(n2, n1), nullptr);
94
95 auto a21 = g.insert_arc(n2, n1, 20);
96 idx.insert(a21);
97 EXPECT_EQ(idx.size(), 2u);
98 EXPECT_EQ(idx.search(n2, n1), a21);
99}
100
102{
103 UGraph g;
104 auto n1 = g.insert_node(1);
105 auto n2 = g.insert_node(2);
106 auto a = g.insert_arc(n1, n2, 1);
107
108 IndexArc<UGraph> idx(g, false);
109 EXPECT_EQ(idx.size(), 0u);
110
111 EXPECT_EQ(idx.insert(a), a);
112 EXPECT_EQ(idx.size(), 1u);
113
114 EXPECT_EQ(idx.insert(a), a);
115 EXPECT_EQ(idx.size(), 1u);
116}
117
119{
120 UGraph g;
121 auto n1 = g.insert_node(1);
122 auto n2 = g.insert_node(2);
123
124 IndexArc<UGraph> idx(g, false);
125
126 // Insert a dummy arc that is not part of the graph, but has the same endpoints.
127 auto* dummy = new UGraph::Arc(0);
128 dummy->src_node = n1;
129 dummy->tgt_node = n2;
131 EXPECT_EQ(idx.size(), 1u);
132
133 auto a = g.insert_arc(n1, n2, 123);
134 EXPECT_EQ(idx.insert(a), dummy);
135 EXPECT_EQ(idx.size(), 1u);
136
137 idx.remove(dummy);
138 delete dummy;
139}
140
142{
143 UGraph g;
144 auto n1 = g.insert_node(1);
145 auto n2 = g.insert_node(2);
146
147 IndexArc<UGraph> idx(g, true);
148 EXPECT_EQ(idx.size(), 0u);
149
150 auto a = idx.insert_in_graph(n1, n2, 7);
151 EXPECT_EQ(g.get_num_arcs(), 1u);
152 EXPECT_EQ(idx.size(), 1u);
153 EXPECT_EQ(idx.search(n1, n2), a);
154
155 EXPECT_THROW(idx.insert_in_graph(n1, n2, 8), std::domain_error);
156
157 idx.remove_from_graph(a);
158 EXPECT_EQ(g.get_num_arcs(), 0u);
159 EXPECT_EQ(idx.size(), 0u);
160 EXPECT_EQ(idx.search(n1, n2), nullptr);
161}
162
164{
165 UGraph g;
166 auto n1 = g.insert_node(1);
167 auto n2 = g.insert_node(2);
168 auto n3 = g.insert_node(3);
169
170 auto a12 = g.insert_arc(n1, n2, 1);
171 auto a23 = g.insert_arc(n2, n3, 2);
172 (void)a12;
173 (void)a23;
174
175 IndexArc<UGraph> idx(g, true);
176 EXPECT_EQ(idx.size(), g.get_num_arcs());
177
178 idx.clear_index();
179 EXPECT_EQ(idx.size(), 0u);
180
181 idx.build_index();
182 EXPECT_EQ(idx.size(), g.get_num_arcs());
183}
184
186{
187 UGraph g;
188 auto n1 = g.insert_node(1);
189 auto n2 = g.insert_node(2);
190 auto n3 = g.insert_node(3);
191
192 auto a12 = g.insert_arc(n1, n2, 1); // odd
193 auto a23 = g.insert_arc(n2, n3, 2); // even
194
195 IndexArc<UGraph, Rand_Tree, EvenArcInfo> idx(g, true, EvenArcInfo{});
196
197 EXPECT_EQ(idx.size(), 1u);
198 EXPECT_EQ(idx.search(n2, n3), a23);
199 EXPECT_EQ(idx.search(n1, n2), nullptr);
200
201 // But the arc is in the graph.
202 EXPECT_EQ(Aleph::search_arc(g, n1, n2), a12);
203}
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
Index for fast arc lookup by its endpoint nodes.
GT_Arc * search(void *src, void *tgt) const
Search an arc that connects two nodes.
void build_index()
Insert into the index all arcs currently present in the graph.
void remove_from_graph(GT_Arc *a)
Remove an arc from both index and graph.
void remove(GT_Arc *e)
Remove an arc from the index.
size_t size() const
Return the number of arcs currently stored in the index.
GT_Arc * search_directed(void *src, void *tgt) const
Search an arc that connects two nodes as a directed pair.
void clear_index()
Remove all arcs from the index.
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info)
Create a new arc between two nodes, insert it into the graph and index it.
GT_Arc * insert(GT_Arc *e)
Insert an arc pointer into the index.
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:428
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
_Graph_Arc Arc
The node class type.
Definition tpl_graph.H:433
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
constexpr size_t get_num_arcs() const noexcept
Definition graph-dry.H:778
#define TEST(name)
GT::Arc * search_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Arc filtered searching given two nodes.
Definition tpl_graph.H:2421
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Generic graph and digraph implementations.
Arc indexing for fast lookup by endpoint nodes.