Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test-memarray.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 <iostream>
29# include <memory>
30# include <concepts>
31# include <stdexcept>
32# include <ah-errors.H>
33# include <tpl_memArray.H>
34
35using namespace std;
36using namespace Aleph;
37
38int g_count = -1;
39
40struct Foo
41{
42 std::unique_ptr<int> ptr;
43
45
46 Foo(int i) : ptr(std::make_unique<int>(i)) {}
47
48 Foo(const Foo & f) : ptr(f.ptr ? std::make_unique<int>(*f.ptr) : nullptr) {}
49
50 Foo(Foo && f) noexcept = default;
51
52 Foo & operator = (const Foo & f)
53 {
54 if (this != &f)
55 ptr = f.ptr ? std::make_unique<int>(*f.ptr) : nullptr;
56 return *this;
57 }
58
59 Foo & operator = (Foo && f) noexcept = default;
60
61 ~Foo() = default;
62
63 operator int () const
64 {
65 ah_domain_error_if(not ptr) << "Attempt to access null Foo";
66 return *ptr;
67 }
68};
69
70template <typename T>
71 requires std::convertible_to<T, int>
72void print(const MemArray<T> & s)
73{
74 cout << "capacity = " << s.capacity() << endl
75 << "size = " << s.size() << endl;
76
77 for (size_t i = 0; i < s.size(); ++i)
78 {
79 int val = s[i];
80 cout << val << " ";
81 }
82 cout << endl
83 << endl;
84}
85
86template <typename T>
87 requires std::constructible_from<T, int>
89{
91
92 for (int i = 0; i < n; ++i)
93 ret.put(T(i));
94
95 return ret;
96}
97
98int main(int argc, char * argv[])
99{
101
102 print(s);
103
104 int n = 1000;
105 if (argc > 1)
106 {
107 try { n = stoi(argv[1]); }
108 catch (const std::invalid_argument &) { n = 1000; }
109 catch (const std::out_of_range &) { n = 1000; }
110 ah_invalid_argument_if(n <= 0) << "n must be > 0";
111 }
112
113 for (int i = 0; i < n; ++i)
114 s.put(i);
115
116 print(s);
117
118 int m = n / 4;
119 if (argc > 2)
120 {
121 try { m = stoi(argv[2]); }
122 catch (const std::invalid_argument &) { m = n / 4; }
123 catch (const std::out_of_range &) { m = n / 4; }
124 ah_invalid_argument_if(m < 0 or m > n) << "m must be between 0 and n";
125 }
126
127 cout << "Extracting " << m << " items" << endl;
128 for (int i = 0; i < m; ++i)
129 cout << s.get() << " ";
130 cout << endl;
131
132 print(s);
133
134 MemArray<int> c(s);
135
136 print(c);
137
139
141}
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
Simple, scalable and fast dynamic array.
size_t size() const noexcept
Return the number of elements.
constexpr size_t capacity() const noexcept
The type of element of array.
T get(const size_t i=1)
Remove i elements from the end.
T & put(const T &item)
Put a copy of item at the end of sequence.
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.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
STL namespace.
int i
~Foo()=default
std::unique_ptr< int > ptr
Foo(const Foo &f)
Foo(Foo &&f) noexcept=default
Foo & operator=(const Foo &f)
Foo(int i)
void print(const MemArray< T > &s)
int g_count
MemArray< T > create_memarray(int n)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Simple, scalable, contiguous dynamic array.