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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27
28# include <gsl/gsl_rng.h>
29# include <cstdlib>
30# include <cassert>
31# include <iostream>
32# include <ahFunctional.H>
33# include <ahSort.H>
34# include <htlist.H>
35# include <tpl_dynDlist.H>
36# include <tpl_dynSetTree.H>
37# include <tpl_hash.H>
38
39# include <ctime>
40using namespace std;
41
43
44template <template <class> class C, typename T>
45void print_seq(const C<T> & c)
46{
47 for (typename C<T>::Iterator it(c); it.has_curr(); it.next())
48 cout << it.get_curr() << " ";
49
50 cout << endl;
51}
52
53int main(int argc, char *argv[])
54{
55 int n = argc > 1 ? atoi(argv[1]) : 100;
56 unsigned long seed = argc > 2 ? atoi(argv[2]) : std::time(NULL);
57
58 cout << argv[0] << " " << n << " " << seed << endl;
59
62
63 DynList<int> l1({1,2,3,4,5,6,7,8,9});
64
65 cout << "** indexes(l1) = ";
67 {
68 cout << "(" << t.first << "," << t.second << ")";
69 });
70 cout << endl;
71
72 DynDlist<int> l2({1,2,3,4,5,6,7,8,9});
73 HashSet<int> l3({0, 1,2,3,4,5,6,7,8,9});
74
75 auto to_str = /* Lambda */ [&l1] (int i) -> std::string
76 {
77 return std::to_string(i);
78 };
79
81
84 print_seq(l3.keys());
86
88
89 for (DynList<std::pair<int,int>>::Iterator it(lz1); it.has_curr(); it.next())
90 cout << it.get_curr().first << "," << it.get_curr().second << endl;
91
92 {
93 cout << "Building random list" << endl;
95 for (long i = 0; i < n; ++i)
97
98 cout << endl
99 << "building array copy of previous list" << endl;
101 l.for_each(/* Lambda */ [&a](long i)
102 {
103 //cout << i << endl;
104 a.append(i);
105 });
106
107 cout << endl
108 << "Copying array to a tree" << endl;
109 DynSetTree<long> tree;
110 a.for_each(/* Lambda */ [&tree] (long i)
111 {
112 tree.append(i);
113 });
114
115 cout << endl
116 << "Sorting array for ulterior fast searching";
117 quicksort_op(a);
118
119 cout << endl
120 << "traversing all the keys of tree and verifiying them with array"
121 << endl;
122 assert(tree.all(/* Lambda */ [&a] (long i)
123 {
124 return a(binary_search(a, i)) == i;
125 }));
126 cout << "done!" << endl
127 << endl
128 << "Traversing all the keys of array and verifiying then with the tree"
129 << endl;
130 assert(a.all(/* Lambda */ [&tree] (long i)
131 {
132 return tree.search(i) != NULL;
133 }));
134 cout << "done!" << endl
135 << endl;
136 }
137
138 cout << "Take 1/4:";
139 range(10).take(3).for_each([] (long i) { cout << " " << i; });
140 cout << endl
141 << "Drop 1/4";
142 range(10).drop(3).for_each([] (long i) { cout << " " << i; });
143 cout << endl;
144
146 return 0;
147}
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
Dynamic singly linked list with functional programming support.
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:1631
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Definition ah-dry.H:957
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
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:1680
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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:1729
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:48
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.
Unified hash table interface.
DynList< int > l