Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testFunctional.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <gsl/gsl_rng.h>
34# include <cstdlib>
35# include <cassert>
36# include <iostream>
37# include <ahFunctional.H>
38# include <ahSort.H>
39# include <htlist.H>
40# include <tpl_dynDlist.H>
41# include <tpl_dynSetTree.H>
42# include <tpl_hash.H>
43
44# include <ctime>
45using namespace std;
46
48
49template <template <class> class C, typename T>
50void print_seq(const C<T> & c)
51{
52 for (typename C<T>::Iterator it(c); it.has_curr(); it.next())
53 cout << it.get_curr() << " ";
54
55 cout << endl;
56}
57
58int main(int argc, char *argv[])
59{
60 int n = argc > 1 ? atoi(argv[1]) : 100;
61 unsigned long seed = argc > 2 ? atoi(argv[2]) : std::time(NULL);
62
63 cout << argv[0] << " " << n << " " << seed << endl;
64
67
68 DynList<int> l1({1,2,3,4,5,6,7,8,9});
69
70 cout << "** indexes(l1) = ";
72 {
73 cout << "(" << t.first << "," << t.second << ")";
74 });
75 cout << endl;
76
77 DynDlist<int> l2({1,2,3,4,5,6,7,8,9});
78 HashSet<int> l3({0, 1,2,3,4,5,6,7,8,9});
79
80 auto to_str = /* Lambda */ [&l1] (int i) -> std::string
81 {
82 return std::to_string(i);
83 };
84
86
89 print_seq(l3.keys());
91
93
94 for (DynList<std::pair<int,int>>::Iterator it(lz1); it.has_curr(); it.next())
95 cout << it.get_curr().first << "," << it.get_curr().second << endl;
96
97 {
98 cout << "Building random list" << endl;
100 for (long i = 0; i < n; ++i)
102
103 cout << endl
104 << "building array copy of previous list" << endl;
106 l.for_each(/* Lambda */ [&a](long i)
107 {
108 //cout << i << endl;
109 a.append(i);
110 });
111
112 cout << endl
113 << "Copying array to a tree" << endl;
114 DynSetTree<long> tree;
115 a.for_each(/* Lambda */ [&tree] (long i)
116 {
117 tree.append(i);
118 });
119
120 cout << endl
121 << "Sorting array for ulterior fast searching";
122 quicksort_op(a);
123
124 cout << endl
125 << "traversing all the keys of tree and verifiying them with array"
126 << endl;
127 assert(tree.all(/* Lambda */ [&a] (long i)
128 {
129 return a(binary_search(a, i)) == i;
130 }));
131 cout << "done!" << endl
132 << endl
133 << "Traversing all the keys of array and verifiying then with the tree"
134 << endl;
135 assert(a.all(/* Lambda */ [&tree] (long i)
136 {
137 return tree.search(i) != NULL;
138 }));
139 cout << "done!" << endl
140 << endl;
141 }
142
143 cout << "Take 1/4:";
144 range(10).take(3).for_each([] (long i) { cout << " " << i; });
145 cout << endl
146 << "Drop 1/4";
147 range(10).drop(3).for_each([] (long i) { cout << " " << i; });
148 cout << endl;
149
151 return 0;
152}
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
int main()
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * append(const Key &key)
Aleph::DynList< T > take(const size_t n) const
Return a list with the first n elements seen in the container during its traversal.
Definition ah-dry.H:1764
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:796
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Definition ah-dry.H:984
Aleph::DynList< T > drop(const size_t n) const
Drop the first n elements seen in the container during its traversal.
Definition ah-dry.H:1812
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Singly linked list implementations with head-tail access.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
DynList< std::pair< size_t, typename Container::Key_Type > > indexes(const Container &c)
Return pairs of (index, key).
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
STL namespace.
Aleph::DynList< T > keys() const
Definition ah-dry.H:1863
DynList< char > l3
DynList< int > l1
DynList< int > l2
gsl_rng * r
void print_seq(const C< T > &c)
ValueArg< size_t > seed
Definition testHash.C:53
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.
Unified hash table interface.
DynList< int > l