Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynlist.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
33
38# include <gtest/gtest.h>
39
40# include <htlist.H>
41
42using namespace testing;
43using namespace Aleph;
44
45struct List_of_25_items : public Test
46{
47 size_t n = 0;
51 {
52 for (size_t i = 0; i < 25; ++i, ++n)
53 list.append(i + 1);
54
55 const DynList<int> l = list;
56 rlist = l.rev();
57 }
58};
59
61{
62 DynList<int> list;
63 EXPECT_TRUE(list.is_empty());
66
67 list.append(2);
68 EXPECT_FALSE(list.is_empty());
71 EXPECT_EQ(list.get_first(), list.get_last());
72
73 list.insert(1);
74 EXPECT_FALSE(list.is_empty());
77
78 EXPECT_EQ(list.get_first(), 1);
79 EXPECT_EQ(list.get_last(), 2);
80
81 list.insert(0);
82 list.append(3);
83 EXPECT_EQ(list.get_first(), 0);
84 EXPECT_EQ(list.get_last(), 3);
85
86 EXPECT_EQ(list.remove_first(), 0);
87 EXPECT_EQ(list.size(), 3);
88 EXPECT_EQ(list.get_last(), 3);
89
90 EXPECT_EQ(list.remove_first(), 1);
91 EXPECT_EQ(list.size(), 2);
92 EXPECT_EQ(list.get_last(), 3);
93
94 EXPECT_EQ(list.remove_first(), 2);
95 EXPECT_EQ(list.size(), 1);
96 EXPECT_EQ(list.get_last(), 3);
97
100
101 EXPECT_EQ(list.remove_first(), 3);
102 EXPECT_TRUE(list.is_empty());
103 EXPECT_EQ(list.size(), 0);
104
105 EXPECT_THROW(list.rotate_left(1), std::domain_error);
107}
108
110{
111 DynList<int> laux, list;
112 laux.insert(2);
113 list.append(std::move(laux));
114
117
118 laux.insert(1);
119 list.insert(std::move(laux));
121 EXPECT_EQ(list.size(), 2);
122 EXPECT_EQ(list.get_first(), 1);
123 EXPECT_EQ(list.get_last(), 2);
124}
125
127{
128 EXPECT_EQ(list.get_first(), 1);
129 EXPECT_EQ(list.get_last(), 25);
130 EXPECT_EQ(list.size(), 25);
131 EXPECT_FALSE(list.is_empty());
132 EXPECT_FALSE(list.is_unitarian());
133 EXPECT_FALSE(list.is_unitarian_or_empty());
134}
135
137{
138 int i = 1;
139 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
140 {
141 EXPECT_EQ(it.get_curr(), i);
142 EXPECT_EQ(it.get_pos(), i - 1);
143 }
144}
145
147{
148 DynList<int> l, r;
149 list.split(l, r);
150
151 EXPECT_TRUE(list.is_empty());
152 EXPECT_EQ(l.size(), 13);
153 EXPECT_EQ(r.size(), 12);
154 EXPECT_EQ(l.get_first(), 1);
155 EXPECT_EQ(l.get_last(), 13);
156 EXPECT_EQ(r.get_first(), 14);
157 EXPECT_EQ(r.get_last(), 25);
158
159 int i = 1;
160 for (auto it = l.get_it(); it.has_curr(); it.next(), ++i)
161 EXPECT_EQ(it.get_curr(), i);
162
163 for (auto it = r.get_it(); it.has_curr(); it.next(), ++i)
164 EXPECT_EQ(it.get_curr(), i);
165
166 list.append(r);
167 list.insert(l);
168 i = 1;
169 for (auto it = l.get_it(); it.has_curr(); it.next(), ++i)
170 EXPECT_EQ(it.get_curr(), i);
171}
172
174{
176 laux.swap(list);
177
178 EXPECT_TRUE(list.is_empty());
179 EXPECT_EQ(list.size(), 0);
181
182 int i = 1;
183 for (auto it = laux.get_it(); it.has_curr(); it.next(), ++i)
184 EXPECT_EQ(it.get_curr(), i);
185}
186
188{
189 list.reverse();
190 int i = 25;
191 for (auto it = list.get_it(); it.has_curr(); it.next(), --i)
192 EXPECT_EQ(it.get_curr(), i);
193
194 list.reverse();
195
196 i = 1;
197 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
198 EXPECT_EQ(it.get_curr(), i);
199
200 const DynList<int> list_cpy = list;
201 const DynList<int> rlist_cpy = rlist;
202
203 EXPECT_TRUE(eq(list_cpy.rev(), rlist));
204 EXPECT_TRUE(eq(rlist_cpy.rev(), list));
205
206 DynList<int> l, r;
207 list.split(l, r);
208
209 EXPECT_TRUE(list.is_empty());
210
211 l.reverse();
212 r.reverse();
213 list.insert(l);
214 list.insert(r);
215
216 list.reverse();
217 i = 1;
218 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
219 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
220
221}
222
224{
225 list.rotate_left(3);
226
227 {
228 auto it = list.get_it();
229 for (int i = 0, n = 4; i < 3; ++i, ++n, it.next())
230 EXPECT_EQ(it.get_curr(), n);
231 }
232
233 list.rotate_left(22);
234 int i = 1;
235 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
236 EXPECT_EQ(it.get_curr(), i);
237}
238
240{
241 DynList<int> ll = { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 };
242 DynList<int> lg = { 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 };
243
244 ll.reverse();
245
246 list.insert(std::move(ll));
247 list.append(std::move(lg));
248
251
252 int i = -9;
253 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
254 EXPECT_EQ(it.get_curr(), i);
255}
256
258{
259 DynList<int> m;
260 size_t n = 0;
261 auto ret = m.traverse([&n] (int) { ++n; return true; });
263 EXPECT_EQ(n, 0);
264}
265
267{
268 EXPECT_TRUE(list.size() > 0);
269 EXPECT_EQ(list.size(), n);
270 int N = 0;
271 auto ret = list.traverse([&N, this] (int i) { ++N; return i < n/2; });
273 EXPECT_EQ(N, n/2);
274}
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
T & get_last() const
Return the last item of the list.
Definition htlist.H:1663
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
DynList & rev() noexcept
Definition htlist.H:1769
DynList & reverse() noexcept
Definition htlist.H:1763
DynList & swap(DynList &l) noexcept
Swap this with l.
Definition htlist.H:1448
Iterator on HTList.
Definition htlist.H:1083
bool has_curr() const noexcept
Return true if iterator has current item.
Definition htlist.H:1150
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
void rotate_left(size_t n)
Rotate to left the list n positions.
Definition htlist.H:1359
size_t split(HTList &l, HTList &r) noexcept
Definition htlist.H:977
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
bool is_unitarian() const noexcept
Return true if the list contains exactly just one element.
Definition htlist.H:528
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Definition htlist.H:535
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
#define TEST(name)
TEST_F(List_of_25_items, Basic_operations)
Definition dynlist.cc:126
#define N
Definition fib.C:294
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void reverse(Itor beg, Itor end)
Reverse elements in a range.
Definition ahAlgo.H:1094
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:121
bool traverse(Node *root, Op op)
void rotate_left(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements left by m positions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:95
DynList< int > rlist
Definition dynlist.cc:49
DynList< int > list
Definition dynlist.cc:48
DynList< int > l