47#include <gtest/gtest.h>
55using namespace testing;
184 auto n = g.insert_node(10);
193 vector<Graph::Node*>
nodes;
194 for (
int i = 0; i < 100; ++i)
195 nodes.push_back(g.insert_node(i));
198 for (
int i = 0; i < 100; ++i)
204 auto n1 = g.insert_node(1);
205 auto n2 = g.insert_node(2);
206 auto a = g.insert_arc(n1, n2, 1.5);
217 auto n = g.insert_node(1);
218 auto a = g.insert_arc(n, n, 0.0);
237 for (
int i = 0; i < 5; ++i)
244 auto n = g.get_first_node();
251 vector<Graph::Node*>
nodes;
252 for (
auto it = g.get_node_it(); it.has_curr(); it.next())
253 nodes.push_back(it.get_curr());
260 g.remove_node(
nodes[0]);
267 while (g.get_num_nodes() > 0)
268 g.remove_node(g.get_first_node());
314 g.disconnect_arc(a1);
335 for (
int i = 0; i < 5; ++i)
349 for (
auto it = g.get_node_it(); it.has_curr(); it.next())
358 for (
auto it = g.get_arc_it(); it.has_curr(); it.next())
367 for (
auto it = g.get_arc_it(
nodes[0]); it.has_curr(); it.next())
375 auto it = g.get_node_it();
384 auto it = g.get_node_it();
397 return n->get_info() % 20 == 0;
405 return a->get_info() > 0.25;
487 moved = std::move(g);
495 auto n =
g2.insert_node(100);
496 g2.insert_arc(n, n, 0.5);
498 size_t g1_nodes = g.get_num_nodes();
526 auto n =
copy.insert_node(999);
527 copy.insert_arc(n, n, 99.0);
545 for (
int i = 0; i < 10; ++i)
546 target.insert_node(i * 100);
571 for (
int i = 0; i < 5; ++i)
633 for (
size_t i = 0; i <
arcs.size(); ++i)
670 size_t node_count = 0;
683 auto node_list = path.
nodes();
693 auto arc_list = path.
arcs();
770 vector<TestDigraph::Arc*>
arcs;
774 for (
int i = 0; i < 5; ++i)
775 nodes.push_back(
dg.insert_node(i));
839 auto n1 = g.insert_node(1);
840 auto n2 = g.insert_node(2);
841 auto a = g.insert_arc(n1, n2, 1.0);
849 auto n1 = g.insert_node(1);
850 auto n2 = g.insert_node(2);
851 auto n3 = g.insert_node(3);
852 auto a = g.insert_arc(n2, n3, 1.0);
860 auto n1 = g.insert_node(1);
861 auto n2 = g.insert_node(2);
862 auto a = g.insert_arc(n1, n2, 1.0);
877 vector<TestDigraph::Arc*>
arcs;
881 for (
int i = 0; i < 4; ++i)
882 nodes.push_back(
dg.insert_node(i));
895 for (
auto it = dg.get_arc_it(
nodes[0]); it.has_curr(); it.next())
896 if (dg.get_src_node(it.get_curr()) ==
nodes[0])
902 for (
auto it = dg.get_arc_it(
nodes[3]); it.has_curr(); it.next())
903 if (dg.get_src_node(it.get_curr()) ==
nodes[3])
911 for (
auto it = dg.get_arc_it(
nodes[0]); it.has_curr(); it.next())
936 for (
int i = 1; i <= 5; ++i)
963 return n->get_info() > 0;
971 return a->get_info() > 0;
979 return acc + n->get_info();
987 return acc + a->get_info();
1007 auto n1 =
g2.insert_node(1);
1008 auto n2 =
g2.insert_node(2);
1028 for (
size_t i = 0; i < NUM_NODES; ++i)
1031 EXPECT_EQ(g.get_num_nodes(), NUM_NODES);
1036 vector<Graph::Node*>
nodes;
1037 for (
size_t i = 0; i < NUM_NODES; ++i)
1038 nodes.push_back(g.insert_node(
static_cast<int>(i)));
1040 std::mt19937
rng(42);
1041 std::uniform_int_distribution<size_t> dist(0, NUM_NODES - 1);
1043 for (
size_t i = 0; i < NUM_ARCS; ++i)
1045 size_t src = dist(
rng);
1046 size_t tgt = dist(
rng);
1047 g.insert_arc(
nodes[src],
nodes[tgt],
static_cast<double>(i));
1055 vector<Graph::Node*>
nodes;
1056 for (
size_t i = 0; i < 100; ++i)
1057 nodes.push_back(g.insert_node(
static_cast<int>(i)));
1062 for (
size_t i = 0; i < 50; ++i)
1063 g.remove_node(
nodes[i]);
1075 return a->get_info() > b->get_info();
1078 auto it = g.get_node_it();
1079 int prev = it.get_curr()->get_info();
1082 while (it.has_curr())
1084 EXPECT_GE(prev, it.get_curr()->get_info());
1085 prev = it.get_curr()->get_info();
1093 return a->get_info() > b->get_info();
1096 auto it = g.get_arc_it();
1097 double prev = it.get_curr()->get_info();
1100 while (it.has_curr())
1102 EXPECT_GE(prev, it.get_curr()->get_info());
1103 prev = it.get_curr()->get_info();
1125 "Node_Iterator must satisfy BasicGraphIterator");
1127 "Arc_Iterator must satisfy BasicGraphIterator");
1129 "Node_Iterator must satisfy GraphNodeIterator");
1131 "Arc_Iterator must satisfy GraphArcIterator");
1135 "Digraph::Node_Iterator must satisfy BasicGraphIterator");
1137 "Digraph::Arc_Iterator must satisfy BasicGraphIterator");
1181 auto* arc = it.get_curr();
1185 auto* tgt = it.get_tgt_node();
1190template <BasicGraphIterator It>
1194 for (; it.has_curr(); it.next())
Generic directed graph (digraph) wrapper template.
DynList & swap(DynList &l) noexcept
Swap this with l.
void next()
Advances the iterator to the next filtered element.
size_t size() const noexcept
Count the number of elements of the list.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Filtered iterator on the nodes of a graph.
Iterator on nodes and arcs of a path.
bool has_current_node() const noexcept
Return true if the iterator has a current node.
bool is_cycle() const
Return true if this is a cycle; throws if path is empty.
void insert(Arc *arc)
Insert an arc as the first of a path.
DynList< Arc * > arcs() const
Return a list with the arcs of a path (order, according to the path)
size_t size() const noexcept
Return the path length in nodes.
bool contains_node(Node *node) const noexcept
Return true if node belongs to the path.
void insert_directed(Node *p)
Append a node to a directed path.
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
bool is_empty() const noexcept
Return true if the path is empty.
bool contains_arc(Arc *arc) const noexcept
Return true if arc belongs to the path.
Node * remove_first_node()
Remove the first node of a path.
void append(Arc *arc)
Append an arc to the path.
void append_directed(Node *p)
Append a node to a directed path.
DynList< Node * > nodes() const
Return a list with the nodes of path (order according to the path)
Node * get_last_node() const
Return the last node of path; throws overflow_error if path is empty.
Node * remove_last_node()
Remove the last node of path.
vector< TestDigraph::Node * > nodes
vector< TestDigraph::Arc * > arcs
vector< TestDigraph::Node * > nodes
vector< TestDigraph::Arc * > arcs
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
size_t num_arcs
data associated to the node. Access it with get_info()
auto get_arc_it() const noexcept
Obtains an iterator to the arc of graph.
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
vector< Graph::Node * > nodes
vector< Graph::Node * > nodes
vector< Graph::Arc * > arcs
static constexpr size_t NUM_ARCS
static constexpr size_t NUM_NODES
vector< Graph::Arc * > arcs
vector< Graph::Node * > nodes
Concept for basic graph iterators.
Concept for graph arc iterators.
Concept for graph node iterators.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
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.
GT::Arc * search_directed_arc(const GT &g, typename GT::Node *src, typename GT::Node *tgt, SA sa=SA()) noexcept
Searching of directed arc linking two nodes.
Path< GT > find_path_depth_first(const GT &g, typename GT::Node *start_node, typename GT::Node *end_node)
Depth-first search of a path between two nodes.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Filtered iterator on all the arcs of a graph.
Arc of graph implemented with double-linked adjacency lists.
Node belonging to a graph implemented with a double linked adjacency list.
bool operator()(Graph::Node *n) const noexcept
bool operator()(Graph::Arc *a) const noexcept
Generic graph and digraph implementations.
TEST_F(GraphBasicTest, DefaultConstructorCreatesEmptyGraph)
size_t count_elements(It it)