4#include <gtest/gtest.h>
25 bool operator()(
const int & a,
const int & b)
const {
return a < b; }
30 bool operator()(
const int & a,
const int & b) {
return a < b; }
35 bool operator()(
int x)
const {
return x > 0; }
40 void operator()(
int)
const {}
45 void operator()(
int & x)
const { ++x; }
50 std::optional<int> operator()(
int x)
const
52 return x > 0 ? std::optional<int>(x) :
std::nullopt;
58 void operator()()
const {}
63 int operator()(
int acc,
int x)
const {
return acc + x; }
68 void operator()(
int,
int)
const {}
75 bool operator()(
Graph::Node *)
const {
return true; }
80 bool operator()(
Graph::Arc *)
const {
return true; }
86static_assert(CallableWith<IsPositive &, const int &>);
87static_assert(CallableWith<ReturnsVoid &, int>);
88static_assert(not CallableWith<IsPositive &>);
89static_assert(not CallableWith<bool (std::optional<int>::*)() const,
std::optional<int> &>);
91static_assert(PredicateWith<IsPositive &, const int &>);
92static_assert(not PredicateWith<ReturnsVoid &, const int &>);
94static_assert(PredicateWith<MaybePositive &, const int &>);
95static_assert(not std::predicate<MaybePositive &, const int &>);
104static_assert(
can_introsort<bool (*)(
const int &,
const int &)>);
171TEST(AhConceptsFunctionalTest, ConstrainedCallsStillRun)
176 EXPECT_EQ(
l.
foldl(0, Accumulate()), 6);
194 int node_info(
Graph::Node * p) {
return p->get_info(); }
200TEST(AhConceptsFunctionalTest, MemberMapsAndFoldsAcceptLambdasWithoutExplicitType)
208 EXPECT_EQ(infos.size(), 2u);
209 EXPECT_EQ(g.
nodes_map(&node_info).size(), 2u);
211 EXPECT_EQ(g.
arcs_map(n1, [](
Graph::Arc * a) { return a->get_info(); }).get_first(), 7);
213 EXPECT_EQ(g.
foldl_arcs(0, [](
const int & acc,
Graph::Arc * a) { return acc + a->get_info(); }), 7);
214 EXPECT_EQ(g.
foldl_arcs(n1, 0, [](
const int & acc,
Graph::Arc * a) { return acc + a->get_info(); }), 7);
217 auto halves = g.template nodes_map<double>([](
Graph::Node * p) {
return p->get_info() / 2.0; });
218 EXPECT_DOUBLE_EQ(halves.get_first() + halves.get_last(), 1.5);
224TEST(AhConceptsFunctionalTest, MemberInOutArcMapsAndFoldsAcceptLambdas)
231 auto arc_info = [](
Graph::Arc * arc) {
return arc->get_info(); };
232 auto add_info = [](
const int & acc,
Graph::Arc * arc) {
return acc + arc->get_info(); };
234 EXPECT_EQ(g.template in_arcs_map<int>(b, arc_info).get_first(), 5);
243TEST(AhConceptsFunctionalTest, FreeGraphOverloadsStayUnambiguous)
254 EXPECT_EQ(visits, 3);
256 EXPECT_EQ((Aleph::nodes_map<Graph, long>(g, [](
Graph::Node * p) {
return p->get_info(); }).
size()), 2u);
264TEST(AhConceptsFunctionalTest, MapHelpersDeduceTFromStdFunctionArguments)
277 EXPECT_EQ((Aleph::nodes_map<Graph>(g, nf).
size()), 3u);
278 EXPECT_EQ((Aleph::arcs_map<Graph>(g, af).
size()), 3u);
279 EXPECT_EQ((Aleph::arcs_map<Graph>(g, n1, af).
size()), 2u);
280 EXPECT_EQ((Aleph::map_in_arcs<Graph>(n3, af).
size()), 2u);
281 EXPECT_EQ((Aleph::map_out_arcs<Graph>(n1, af).
size()), 2u);
285 EXPECT_EQ((Aleph::nodes_map<Graph, int>(g, [](
Graph::Node * p) {
return p->get_info(); }).
size()), 3u);
286 EXPECT_EQ((Aleph::nodes_map<Graph, int>(g, nf).
size()), 3u);
291 auto la = [](
Graph::Arc * a) {
return a->get_info() + 1; };
292 EXPECT_EQ((Aleph::map_in_arcs<Graph, int>(n3, af).
size()), 2u);
293 EXPECT_EQ((Aleph::map_in_arcs<Graph, int>(n3, la).
size()), 2u);
294 EXPECT_EQ((Aleph::map_out_arcs<Graph, int>(n1, af).
size()), 2u);
295 EXPECT_EQ((Aleph::map_out_arcs<Graph, int>(n1, la).
size()), 2u);
296 EXPECT_EQ((Aleph::map_in_arcs<Graph, int>(g, n3, af).
size()), 2u);
297 EXPECT_EQ((Aleph::arcs_map<Graph, int>(g, af).
size()), 3u);
298 EXPECT_EQ((Aleph::arcs_map<Graph, int>(g, n1, af).
size()), 2u);
299 EXPECT_EQ((Aleph::map_out_arcs<Graph, int>(n1, af).
foldl(0, [](
int acc,
int x) {
return acc + x; })),
Functional programming utilities for Aleph-w containers.
Dynamic heap of elements of type T ordered by a comparison functor.
Doubly-linked list (defined in tpl_dynList.H).
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)
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &op) const
Exclusive partition of container according to a filter criterion.
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< Container, Operation >::type, T > void mutable_for_each(Operation &operation)
Apply a mutable operation to each element of the container.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
auto out_arcs_map(Node *p, Op op) const
Return a list of outcoming arcs of a node mapped to items of type given by transformation op.
bool traverse_nodes(Operation &op) const
Conditioned traversal of all the nodes of a graph.
T foldl_in_arcs(Node *p, const T &init, Op op) const
Fold the incoming arcs of a node.
T foldl_nodes(const T &init, Op op) const
Folding of nodes on a graph.
auto nodes_map(Op op) const
Map the nodes of a graph to a specific range.
T foldl_arcs(const T &init, Op op) const
Folding of arcs on a graph.
auto arcs_map(Op operation) const
Map the arcs of a graph to a specific range.
auto filter_arcs(Op &op) const
Filter the arcs of graph satisfying a condition.
T foldl_out_arcs(Node *p, const T &init, Op op) const
Fold-left over outcoming arcs of a node.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
void for_each_arc(const GT &g, Op operation, SA sa=SA())
Traverse all the arcs of graph filtering some ones according to a condition and executing an operatio...
bool forall_arc(const GT &g, Op cond, SA sa=SA())
Return true if condition cond is met on every filtered arc of the graph.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
void each(const size_t start, const size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
auto map_out_arcs(typename GT::Node *p, Op op)
Map the outcoming arcs to a transformation,.
T foldl(const Container &container, const T &init, Operation operation)
Classic left fold (reduce).
auto map_in_arcs(typename GT::Node *p, Op op)
Map the incoming arcs to a transformation,.
Itor::difference_type count_if(Itor beg, const Itor &end, Operation op)
Count elements satisfying a predicate.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
bool operator()(const int &a, const int &b) const
bool operator()(const int &a, const int &b)
Lazy and scalable dynamic array implementation.
Dynamic binary heap with node-based storage.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.