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 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 <iostream>
34# include <memory>
35# include <concepts>
36# include <stdexcept>
37# include <ah-errors.H>
38# include <tpl_memArray.H>
39
40using namespace std;
41using namespace Aleph;
42
43int g_count = -1;
44
45struct Foo
46{
47 std::unique_ptr<int> ptr;
48
50
51 Foo(int i) : ptr(std::make_unique<int>(i)) {}
52
53 Foo(const Foo & f) : ptr(f.ptr ? std::make_unique<int>(*f.ptr) : nullptr) {}
54
55 Foo(Foo && f) noexcept = default;
56
57 Foo & operator = (const Foo & f)
58 {
59 if (this != &f)
60 ptr = f.ptr ? std::make_unique<int>(*f.ptr) : nullptr;
61 return *this;
62 }
63
64 Foo & operator = (Foo && f) noexcept = default;
65
66 ~Foo() = default;
67
68 operator int () const
69 {
70 ah_domain_error_if(not ptr) << "Attempt to access null Foo";
71 return *ptr;
72 }
73};
74
75template <typename T>
76 requires std::convertible_to<T, int>
77void print(const MemArray<T> & s)
78{
79 cout << "capacity = " << s.capacity() << endl
80 << "size = " << s.size() << endl;
81
82 for (size_t i = 0; i < s.size(); ++i)
83 {
84 int val = s[i];
85 cout << val << " ";
86 }
87 cout << endl
88 << endl;
89}
90
91template <typename T>
92 requires std::constructible_from<T, int>
94{
96
97 for (int i = 0; i < n; ++i)
98 ret.put(T(i));
99
100 return ret;
101}
102
103int main(int argc, char * argv[])
104{
106
107 print(s);
108
109 int n = 1000;
110 if (argc > 1)
111 {
112 try { n = stoi(argv[1]); }
113 catch (const std::invalid_argument &) { n = 1000; }
114 catch (const std::out_of_range &) { n = 1000; }
115 ah_invalid_argument_if(n <= 0) << "n must be > 0";
116 }
117
118 for (int i = 0; i < n; ++i)
119 s.put(i);
120
121 print(s);
122
123 int m = n / 4;
124 if (argc > 2)
125 {
126 try { m = stoi(argv[2]); }
127 catch (const std::invalid_argument &) { m = n / 4; }
128 catch (const std::out_of_range &) { m = n / 4; }
129 ah_invalid_argument_if(m < 0 or m > n) << "m must be between 0 and n";
130 }
131
132 cout << "Extracting " << m << " items" << endl;
133 for (int i = 0; i < m; ++i)
134 cout << s.get() << " ";
135 cout << endl;
136
137 print(s);
138
139 MemArray<int> c(s);
140
141 print(c);
142
144
146}
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:527
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
int main()
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.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.