Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynarray.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <tpl_dynArray.H>
41#include <ah-unique.H>
42
43using namespace Aleph;
44using namespace std;
45
46namespace {
47
49{
50 DynArray<int> arr;
51 EXPECT_TRUE(arr.is_empty());
52 EXPECT_EQ(arr.size(), 0u);
53
54 arr.append(42);
55 EXPECT_EQ(arr.size(), 1u);
57 EXPECT_EQ(arr.access(0), 42);
58
59 arr.append(7);
60 EXPECT_EQ(arr.size(), 2u);
61 EXPECT_EQ(arr.access(1), 7);
62}
63
65{
66 DynArray<int> src;
67 for (int v : {3, 6, 9})
68 src.append(v);
69
70 auto copy = src.to_array();
71
72 ASSERT_EQ(copy.size(), src.size());
73 for (size_t i = 0; i < copy.size(); ++i)
74 EXPECT_EQ(copy(i), src(i));
75}
76
78{
79 DynArray<int> arr;
81
82 arr.reserve(0, 3);
83 ASSERT_EQ(arr.size(), 4u);
84 for (size_t i = 0; i < arr.size(); ++i)
85 EXPECT_EQ(arr.access(i), 123);
86
87 arr.access(2) = 77;
88 EXPECT_EQ(arr.access(2), 77);
89
90 auto & ref = arr.touch(10);
91 ref = 99;
92 EXPECT_EQ(arr.size(), 11u);
93 EXPECT_EQ(arr.access(10), 99);
94}
95
97{
98 DynArray<int> arr;
99 arr.append(1);
100 arr.append(2);
101 arr.append(1);
102 arr.append(3);
103 arr.append(2);
104 arr.append(4);
105 arr.append(4);
106
107 in_place_unique(arr);
108
109 ASSERT_EQ(arr.size(), 4u);
110 EXPECT_EQ(arr.access(0), 1);
111 EXPECT_EQ(arr.access(1), 2);
112 EXPECT_EQ(arr.access(2), 3);
113 EXPECT_EQ(arr.access(3), 4);
114}
115
117{
118 DynArray<int> arr;
119
120 EXPECT_THROW(arr.pop(), std::underflow_error);
121 EXPECT_THROW(arr.top(), std::underflow_error);
122 EXPECT_THROW(arr.get_first(), std::underflow_error);
123 EXPECT_THROW(arr.get_last(), std::underflow_error);
124
125 int dummy = 0;
126 EXPECT_THROW(arr.remove(dummy), std::underflow_error);
127}
128
130{
131 DynArray<int> arr;
132 EXPECT_THROW(arr.reserve(5, 4), std::domain_error);
133}
134
136{
137 DynArray<int> arr;
138 arr.reserve(5);
139 ASSERT_EQ(arr.size(), 5u);
140 for (size_t i = 0; i < arr.size(); ++i)
141 arr.access(i) = static_cast<int>(i * 2);
142 for (size_t i = 0; i < arr.size(); ++i)
143 EXPECT_EQ(arr.access(i), static_cast<int>(i * 2));
144}
145
147{
148 DynArray<int> arr;
149 for (int i = 0; i < 6; ++i)
150 arr.append(i);
151
152 auto it = arr.get_it(3);
153 ASSERT_TRUE(it.has_curr());
154 EXPECT_EQ(it.get_curr(), 3);
155 it.next();
156 EXPECT_EQ(it.get_curr(), 4);
157
158 const auto & carr = arr;
159 auto cit = carr.get_it(5);
160 EXPECT_EQ(cit.get_curr(), 5);
161 EXPECT_THROW(carr.get_it(6), std::out_of_range);
162}
163
165{
166 DynArray<int> arr;
167 arr.adjust(10);
168 EXPECT_EQ(arr.size(), 10u);
169 arr.cut(3);
170 EXPECT_EQ(arr.size(), 3u);
171 arr.empty();
172 EXPECT_TRUE(arr.is_empty());
173 arr.append(1);
174 arr.append(2);
175 arr.cut(2);
176 EXPECT_EQ(arr.size(), 2u);
177
178 arr.clear();
179 EXPECT_TRUE(arr.is_empty());
180 EXPECT_EQ(arr.size(), 0u);
181
182 arr.append(10);
183 EXPECT_TRUE(arr.contains(10));
184 EXPECT_FALSE(arr.contains(20));
185}
186
188{
189 DynArray<int> arr;
190 EXPECT_THROW(arr.reserve(4, 3), std::domain_error);
191 EXPECT_EQ(arr.size(), 0u);
192}
193
195{
196 DynArray<int> arr;
197 arr.reserve(2, 5);
198 ASSERT_EQ(arr.size(), 6u);
199 arr.touch(20) = 100;
200 EXPECT_EQ(arr.size(), 21u);
201 arr.cut(6);
202 EXPECT_EQ(arr.size(), 6u);
203}
204
206{
207 DynArray<int> arr;
208 for (int i = 0; i < 5; ++i)
209 arr.push(i);
210 EXPECT_EQ(arr.get_first(), 0);
211 EXPECT_EQ(arr.get_last(), 4);
212
213 arr.insert(-1);
214 EXPECT_EQ(arr.get_first(), -1);
215
216 EXPECT_EQ(arr.pop(), 4);
217 EXPECT_EQ(arr.size(), 5u);
218 EXPECT_EQ(arr.top(), arr.get_last());
219}
220
221} // namespace
Deduplicate sequential Aleph containers in-place.
void push(const T &data)
void adjust(const size_t dim)
Set a new dimension.
Iterator get_it()
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
Array< T > to_array() const
Copy contents into Aleph::Array (requires copyable elements).
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
T & insert(const T &item)
T & get_last() const
Return a modifiable reference to the last item of array (as if this was a queue)
T & get_first() const
Return a modifiable reference to the first item of array (as if this was a queue)
void set_default_initial_value(const T &value) noexcept
Set the default value.
void clear() noexcept
Empties the container.
T & touch(const size_t i)
Touch the entry i.
size_t size() const noexcept
Return the current dimension of array.
T pop()
Remove the last item of array (as if this was a stack)
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & top() const
Return a modifiable reference to the last item of stack.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
void empty() noexcept
Empty the array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
bool contains(const Type &item) const
Test if an item is present in the container using equality.
Definition ah-dry.H:563
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
Definition ah-unique.H:74
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
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.
Lazy and scalable dynamic array implementation.