Aleph-w 7.10.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 <ah-random.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 (auto it = c.get_it(); 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
65 r.seed(seed);
66
67 DynList<int> l1({1,2,3,4,5,6,7,8,9});
68
69 cout << "** indexes(l1) = ";
71 {
72 cout << "(" << t.first << "," << t.second << ")";
73 });
74 cout << endl;
75
76 DynDlist<int> l2({1,2,3,4,5,6,7,8,9});
77 HashSet<int> l3({0, 1,2,3,4,5,6,7,8,9});
78
79 auto to_str = /* Lambda */ [&l1] (int i) -> std::string
80 {
81 return std::to_string(i);
82 };
83
84 DynList<std::string> l1_str = maps<std::string>(l1, to_str);
85
88 print_seq(l3.keys());
89 print_seq(l1_str);
90
92
93 for (auto it = lz1.get_it(); it.has_curr(); it.next())
94 cout << it.get_curr().first << "," << it.get_curr().second << endl;
95
96 {
97 cout << "Building random list" << endl;
99 for (long i = 0; i < n; ++i)
100 l.append(r.get());
101
102 cout << endl
103 << "building array copy of previous list" << endl;
105 l.for_each(/* Lambda */ [&a](long i)
106 {
107 //cout << i << endl;
108 a.append(i);
109 });
110
111 cout << endl
112 << "Copying array to a tree" << endl;
113 DynSetTree<long> tree;
114 a.for_each(/* Lambda */ [&tree] (long i)
115 {
116 tree.append(i);
117 });
118
119 cout << endl
120 << "Sorting array for ulterior fast searching";
121 quicksort_op(a);
122
123 cout << endl
124 << "traversing all the keys of tree and verifiying them with array"
125 << endl;
126 assert(tree.all(/* Lambda */ [&a] (long i)
127 {
128 return a(binary_search(a, i)) == i;
129 }));
130 cout << "done!" << endl
131 << endl
132 << "Traversing all the keys of array and verifiying then with the tree"
133 << endl;
134 assert(a.all(/* Lambda */ [&tree] (long i)
135 {
136 return tree.search(i) != NULL;
137 }));
138 cout << "done!" << endl
139 << endl;
140 }
141
142 cout << "Take 1/4:";
143 range(10).take(3).for_each([] (long i) { cout << " " << i; });
144 cout << endl
145 << "Drop 1/4";
146 range(10).drop(3).for_each([] (long i) { cout << " " << i; });
147 cout << endl;
148
149 return 0;
150}
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
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:1231
T & append(const T &item)
Definition htlist.H:1362
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * append(const Key &key)
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
Definition ah-random.H:277
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Definition ah-dry.H:1666
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:2696
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
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:2757
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
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:152
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
See items().
Definition ah-dry.H:2916
DynList< char > l3
DynList< int > l1
DynList< int > l2
void print_seq(const C< T > &c)
Gsl_Compatible_Rng r
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