Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDynDlist.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# include <ctime>
28# include <cstdlib>
29# include <cassert>
30
31# include <iostream>
32# include <string>
33
34# include <ahFunctional.H>
35# include <htlist.H>
36# include <tpl_dynDlist.H>
37
38using namespace std;
39
40
42{
43 for (DynDlist<int>::Iterator it(l); it.has_curr(); it.next())
44 cout << it.get_curr() << " ";
45
46 cout << "size = " << l.size() << " **" << endl;
47}
48
49
51{
52 int i = 0;
53 for (DynDlist<DynDlist<int> >::Iterator it(l); it.has_curr(); it.next())
54 {
55 cout << i++ << " : ";
56 imprime(it.get_curr());
57 cout << endl;
58 }
59}
60
61int main(int argc, char *argv[])
62{
63 int n = 1000;
64 if (argc > 1)
65 {
66 try
67 {
68 n = stoi(argv[1]);
69 }
70 catch (...)
71 {
72 n = 1000;
73 }
74 }
75
76 int m = 1000;
77 if (argc > 2)
78 {
79 try
80 {
81 m = stoi(argv[2]);
82 }
83 catch (...)
84 {
85 m = 1000;
86 }
87 }
88
89 // Validate inputs
90 if (n <= 0 or m <= 0)
91 {
92 cerr << "Error: n and m must be positive integers." << endl;
93 return 1;
94 }
95
96 unsigned int t = std::time(0);
97
98 if (argc > 3)
99 {
100 try
101 {
102 t = stoi(argv[3]);
103 }
104 catch (...)
105 {
106 t = std::time(0);
107 }
108 }
109
110 srand(t);
111
112 cout << argv[0] << " " << n << " " << m << " " << t << endl;
113
115 int i;
116
117 for (i = 0; i < n; i++)
118 {
120
121 for (int k = 0; k < m; ++k)
122 l.append(k);
123 }
124
125 imprime(list);
126
127 // Guard against empty list before calling get_first()
128 if (list.is_empty())
129 {
130 cerr << "Error: List is empty, cannot get first element." << endl;
131 return 1;
132 }
133
134 // DynDlist<int> te ( DynDlist<int>(list.get_first()) );
136
137 te = DynDlist<int>(list.get_first());
138
139 for (DynDlist<int>::Iterator it(te); it.has_curr(); it.next())
140 cout << it.get_curr() << endl;
141
142 // imprime(list);
143
144 {
145 DynDlist<int> l1 = range(10);
146 // DynDlist<int> l2 = range(10, 20);
147 l1.append(range(10, 20));
148 // expected output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
149 l1.for_each([] (auto i) { cout << i << " "; }); cout << endl;
150 // assert(l2.is_empty() and l2.size() == 0);
151 int i = 0;
152 assert(l1.all([&i] (auto k) { return k == i++; }));
153 }
154
155}
156
Functional programming utilities for Aleph-w containers.
Iterator dynamic list.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & get_first() const
Return a modifiable reference to first item in the list.
T & insert(const T &item)
Insert a copy of item at the beginning of the list.
T & append(const T &item)
Definition htlist.H:1271
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1065
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
Singly linked list implementations with head-tail access.
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.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
STL namespace.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
DynList< int > l1
void imprime(DynDlist< int > &l)
static int * k
Dynamic doubly linked list implementation.
DynList< int > l