Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDynArray.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 <cerrno>
29# include <climits>
30# include <cstdlib>
31# include <ctime>
32# include <iostream>
33# include <cmath>
34
35# include <tpl_dynarray_set.H>
36
37
38# include <cassert>
39using namespace std;
40
41# define RAND( x ) (unsigned long) ( x * ( rand() / (RAND_MAX+1.0) ) )
42
43
44# include <cassert>
45using namespace Aleph;
46# include <cassert>
47using namespace std;
48
49struct Test
50{
51 char a1;
52 char a2;
53};
54
55
56int main(int argc,char *argv[])
57{
58 unsigned long seed,index;
59 unsigned long i, value, val;
60
61 unsigned long NUM_ITE = 1000;
62 if (argc > 1)
63 {
64 char * endptr = nullptr;
65 errno = 0;
66 const unsigned long parsed = strtoul(argv[1], &endptr, 10);
67 if (errno != 0 or endptr == argv[1] or *endptr != '\0'
68 or parsed > static_cast<unsigned long>(INT_MAX))
69 {
70 cerr << "Invalid iteration count: " << argv[1] << endl;
71 return 1;
72 }
74 }
75
76 if (argc > 2)
77 seed = atol(argv[2]);
78 else
79 seed = std::time(nullptr);
80
81 srand (seed);
82
84
85 t[10]->a1 = 'a';
86 t[10]->a2 = 'b';
87
88 cout << t[10]->a1 << t[10000]->a1 << endl;
89
90 cout << "./testDynArray " << NUM_ITE << " " << seed << endl;
91
92 try
93 {
94 {
95 DynArray<unsigned long> v1(8,8,8), v2(NUM_ITE);
96
97 for(i = 0; i < NUM_ITE; i++)
98 {
99 value = RAND(NUM_ITE);
100 index = RAND(NUM_ITE);
101 v1[index] = value;
102 v2[index] = v1[index];
103 val = v1[index];
104 value = v2[index];
105 cout << "(" << val << ") (" << value << ") ";
106 assert(v2[index] == v1[index]);
107 }
108
109 {
111
113
114 v4 = v1;
115
116 v4.swap(v2);
117 }
118
119 v2.reserve(0, 2*NUM_ITE);
120
121 }
122 DynArray<unsigned long> v(10, 10, 6);
123
124 v.reserve(0, 2*NUM_ITE);
125
126 v.reserve(NUM_ITE, 4*NUM_ITE);
127 }
128 catch(const std::overflow_error&)
129 {
130 cout << "Overflow!" << endl;
131 }
132 catch(const std::bad_alloc&)
133 {
134 cout << "Not enough memory!" << endl;
135 }
136 catch(const std::invalid_argument&)
137 {
138 cout << "invalid_argument!" << endl;
139 }
140 catch (const std::exception & e)
141 {
142 cout << e.what() << endl;
143 }
144 cout << endl;
145
146 {
147 DynArray<int> s(12,10,4);
148
149 s.append(10);
150
151 cout << s.access(0) << " ****************" << endl;
152
153 cout << s.access(0) << " ****************" << endl;
154
155 s.append(16);
156
157 cout << s[1] << " ****************" << endl;
158
159 }
160
161 {
162 DynArray_Set<int> s(12,10,4);
163
164 s.append(10);
165
166 cout << s[0] << " ****************" << endl;
167
168 cout << s.access(0) << " ****************" << endl;
169
170 s.append(16);
171
172 cout << s[1] << " ****************" << endl;
173
174 }
175
177 for (size_t i = 0; i < NUM_ITE; ++i)
178 a.touch(i) = i;
179
180 for (DynArray<int>::Iterator it(a); it.has_curr(); it.next())
181 cout << it.get_curr() << " ";
182 cout << endl;
183
185 for (it.reset_last(); it.has_curr(); it.prev())
186 cout << it.get_curr() << " ";
187 cout << endl;
188}
189
190
191
192
193
194
195
196
197
198
199
Iterator on the items of array.
void reset_last() noexcept
Reset the iterator to the last item.
void prev()
Move the current a position backward.
bool has_curr() const noexcept
Return true if there is current item.
T & get_curr() const
Return the current item.
Set-like container backed by a dynamic array.
T & touch(const size_t i)
Touch the entry i.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
STL namespace.
char a1
char a2
#define RAND(x)
ValueArg< size_t > seed
Definition testHash.C:48
Array-based dynamic set.