Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
array_utils.H
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
65# ifndef ARRAY_UTILS_H
66# define ARRAY_UTILS_H
67
68# include <cstdlib>
69# include <cassert>
70# include <stdexcept>
71
72# include <ah-errors.H>
73# include <iostream>
74# include <utility>
75
76namespace Aleph
77{
78
95template <class Tarray> inline
96void open_gap(Tarray & ptr, size_t n, size_t pos = 0, size_t num_entries = 1)
97{
98 ah_out_of_range_error_if(pos >= n) << "pos is greater than n";
99
100 const long navail = long(n) - pos;
102 << "num_entries is greater than number of available entries";
103
104 for (long k = n - num_entries - 1, i = n - 1; k >= long(pos); --i, --k)
105 ptr[i] = std::move(ptr[k]);
106}
107
124template <typename T> inline
125void close_gap(T * ptr, size_t n, size_t pos, size_t num_entries = 1)
126{
127 assert(ptr);
128
129 ah_out_of_range_error_if(pos >= n) << "pos is greater than n";
130
131 const long navail = n - pos;
133 << "num_entries is greater than pos";
134
135 for (size_t i = pos; i < n - num_entries; ++i)
136 ptr[i] = std::move(ptr[i + num_entries]);
137}
138
154template <typename T> inline
155void reverse(T * ptr, const size_t n) noexcept
156{
157 for (size_t i = 0; i < n/2; ++i)
158 std::swap(ptr[i], ptr[n - i - 1]);
159}
160
179template <typename T> inline
180void rotate_left(T * ptr, const size_t n, size_t m) noexcept
181{
182 if (n <= 1 or m == 0)
183 return;
184
185 m = m % n; // Safe: n > 0 guaranteed
186 if (m == 0)
187 return;
188
189 reverse(ptr, m);
190 reverse(ptr + m, n - m);
191 reverse(ptr, n);
192}
193
212template <typename T> inline
213void rotate_right(T * ptr, const size_t n, size_t m) noexcept
214{
215 if (n <= 1 or m == 0)
216 return;
217
218 m = m % n; // Safe: n > 0 guaranteed
219 if (m == 0)
220 return;
221
222 reverse(ptr, n);
223 reverse(ptr, m);
224 reverse(ptr + m, n - m);
225}
226
227} // namespace Aleph
228
229# endif
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:579
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
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
void rotate_left(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements left by m positions.
void open_gap(Tarray &ptr, size_t n, size_t pos=0, size_t num_entries=1)
Open a gap in an array by shifting elements right.
Definition array_utils.H:96
void rotate_right(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements right by m positions.
void close_gap(T *ptr, size_t n, size_t pos, size_t num_entries=1)
Close a gap in an array by shifting elements left.
DynList< T > maps(const C &c, Op op)
Classic map operation.