Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-stl-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
33#ifndef AH_STL_UTILS_H
34#define AH_STL_UTILS_H
35
50#include <list>
51#include <vector>
52#include <tuple>
53#include <utility>
54
55#include <htlist.H>
56#include <tpl_dynDlist.H>
57#include <tpl_array.H>
58#include <tpl_dynArray.H>
59#include <tpl_dynSetTree.H>
60#include <ah-convert-common.H>
61
65namespace Aleph
66{
67
84template<class F, class...Ts, std::size_t...Is>
85inline void tuple_for_each(const std::tuple<Ts...> & tuple,
86 F func, std::index_sequence<Is...>)
87{
88 // Use fold expression (C++17) or initializer list expansion
89 // The (void) cast silences unused-value warnings
90 using expander = int[];
91 (void)func; // Silence warning when tuple is empty
92 (void)expander { 0, ((void)func(std::get<Is>(tuple)), 0)... };
93}
94
104template<class F, class...Ts>
105inline void tuple_for_each(const std::tuple<Ts...> & tuple, F func)
106{
107 tuple_for_each(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
108}
109
110// vector_to_DynList(), to_vector() and vector_to_DynArray() are defined once,
111// in ah-convert-common.H, which ah-convert.H includes too.
112
121template <typename T>
122[[nodiscard]] inline DynList<T> to_DynList(const std::vector<T> & v)
123{
124 return vector_to_DynList(v);
125}
126
136template <typename Iterator>
137[[nodiscard]] inline auto range_to_DynList(Iterator begin, Iterator end)
138 -> DynList<std::decay_t<decltype(*begin)>>
139{
140 DynList<std::decay_t<decltype(*begin)>> ret;
141 for (auto it = begin; it != end; ++it)
142 ret.append(*it);
143 return ret;
144}
145
154template <typename T>
155[[nodiscard]] inline Array<T> to_Array(const std::vector<T> & v)
156{
157 Array<T> ret;
158 for (const auto & item : v)
159 ret.append(item);
160 return ret;
161}
162
173template <typename T, class Op>
174[[nodiscard]] inline auto map_vector(const std::vector<T> & v, Op op)
175 -> std::vector<std::decay_t<decltype(op(v[0]))>>
176{
177 using ResultType = std::decay_t<decltype(op(v[0]))>;
178 std::vector<ResultType> ret;
179 ret.reserve(v.size());
180 for (const auto & item : v)
181 ret.push_back(op(item));
182 return ret;
183}
184
194template <typename T, typename... Args>
195[[nodiscard]] inline std::vector<T> variadic_to_vector(Args&&... args)
196{
197 return { static_cast<T>(std::forward<Args>(args))... };
198}
199
209template <typename T, typename... Args>
210[[nodiscard]] inline DynList<T> variadic_to_DynList(Args&&... args)
211{
212 return to_DynList<T>(variadic_to_vector<T>(std::forward<Args>(args)...));
213}
214
215
216/* Boost Software License - Version 1.0 - August 17th, 2003 Permission
217 is hereby granted, free of charge, to any person or organization
218 obtaining a copy of the software and accompanying documentation
219 covered by this license (the "Software") to use, reproduce,
220 display, distribute, execute, and transmit the Software, and to
221 prepare derivative works of the Software, and to permit
222 third-parties to whom the Software is furnished to do so, all
223 subject to the following:
224
225 The copyright notices in the Software and this entire statement,
226 including the above license grant, this restriction and the
227 following disclaimer, must be included in all copies of the
228 Software, in whole or in part, and all derivative works of the
229 Software, unless such copies or derivative works are solely in the
230 form of machine-executable object code generated by a source
231 language processor.
232
233 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
234 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
235 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
236 NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
237 DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER
238 LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM,
239 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
240 DEALINGS IN THE SOFTWARE.
241*/
242
251template<unsigned... Indexes>
253{
258 template<unsigned N>
259 using append = index_tuple<Indexes..., N>;
260};
261
270template<unsigned Size>
272{
273 using type = typename make_index_tuple<Size-1>::type::template append<Size-1>;
274};
275
278template<>
279struct make_index_tuple<0u>
280{
281 using type = index_tuple<>;
282};
284
291template<typename... Types>
292using to_index_tuple = typename make_index_tuple<sizeof...(Types)>::type;
293
295// Based on:
296// http://stackoverflow.com/questions/10604794/convert-stdtuple-to-stdarray-c11
297
298template<class Container, typename T, typename... U, unsigned... I>
299[[nodiscard]] inline static Container
300tuple_to_container_impl(const std::tuple<T, U...> & t, index_tuple<I...>)
301{
302 return build_container<Container>(std::get<I>(t)...);
303}
305
318template<class Container, typename T, typename... U>
319[[nodiscard]] inline Container tuple_to_container(const std::tuple<T, U...> & t)
320{
321 using IndexTuple = typename make_index_tuple<1 + sizeof...(U)>::type;
322 return tuple_to_container_impl<Container>(t, IndexTuple());
323}
324
334template<typename T, typename... U>
335[[nodiscard]] inline DynList<T> tuple_to_dynlist(const std::tuple<T, U...> & t)
336{
337 return tuple_to_container<DynList<T>>(t);
338}
339
349template<typename T, typename... U>
350[[nodiscard]] inline Array<T> tuple_to_array(const std::tuple<T, U...> & t)
351{
352 return tuple_to_container<Array<T>>(t);
353}
354
366template <typename StlContainer>
367[[nodiscard]] inline auto stl_container_to_dynList(const StlContainer & container)
369{
371 for (const auto & item : container)
372 ret.append(item);
373 return ret;
374}
375
384template <typename T>
385[[nodiscard]] inline DynList<T> list_to_DynList(const std::list<T> & l)
386{
387 DynList<T> ret;
388 for (const auto & item : l)
389 ret.append(item);
390 return ret;
391}
392
401template <typename T>
402[[nodiscard]] inline std::list<T> DynList_to_list(const DynList<T> & l)
403{
404 std::list<T> ret;
405 for (const auto & item : l)
406 ret.push_back(item);
407 return ret;
408}
409
421template <typename T>
422[[nodiscard]] inline std::vector<T> DynArray_to_vector(const DynArray<T> & arr)
423{
424 std::vector<T> ret;
425 ret.reserve(arr.size());
426 for (size_t i = 0; i < arr.size(); ++i)
427 ret.push_back(arr[i]);
428 return ret;
429}
430
431} // end namespace Aleph
432
434// Keep backward compatibility: expose commonly used functions at global scope
438using Aleph::to_Array;
439using Aleph::to_vector;
453
454#endif // AH_STL_UTILS_H
Conversions between std::vector and Aleph-w containers shared by ah-convert.H and ah-stl-utils....
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
size_t size() const noexcept
Return the current dimension of array.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Singly linked list implementations with head-tail access.
FFT< double > F
Main namespace for Aleph-w library functions.
Array< T > tuple_to_array(const std::tuple< T, U... > &t)
Convert a tuple to an Array.
auto range_to_DynList(Iterator begin, Iterator end) -> DynList< std::decay_t< decltype(*begin)> >
Convert an iterator range to a DynList.
DynList< T > to_DynList(const Array< T > &a)
Convert an Array to a DynList.
Definition ah-convert.H:148
DynList< T > list_to_DynList(const std::list< T > &l)
Convert a std::list to a DynList.
typename make_index_tuple< sizeof...(Types)>::type to_index_tuple
Convenience alias to generate an index tuple from a parameter pack.
std::vector< T > variadic_to_vector(Args &&... args)
Convert a variadic parameter pack to a std::vector.
DynList< T > variadic_to_DynList(Args &&... args)
Convert a variadic parameter pack to a DynList.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
DynList< T > vector_to_DynList(const std::vector< T > &v)
Convert a std::vector to a DynList.
DynList< T > tuple_to_dynlist(const std::tuple< T, U... > &t)
Convert a tuple to a DynList.
auto stl_container_to_dynList(const StlContainer &container) -> DynList< typename StlContainer::value_type >
Convert any STL container to a DynList.
Container tuple_to_container(const std::tuple< T, U... > &t)
Convert a tuple to an Aleph-w container.
DynArray< T > vector_to_DynArray(const std::vector< T > &v)
Convert a std::vector to a DynArray.
std::vector< T > DynArray_to_vector(const DynArray< T > &arr)
Convert a DynArray to a std::vector.
Array< typename C::Item_Type > to_Array(const C &c)
Convert a container to an Array.
Definition ah-convert.H:168
std::list< T > DynList_to_list(const DynList< T > &l)
Convert a DynList to a std::list.
void tuple_for_each(const std::tuple< Ts... > &tuple, F func, std::index_sequence< Is... >)
Apply a function to each element in a tuple (tuple first).
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
auto map_vector(const std::vector< T > &v, Op op) -> std::vector< std::decay_t< decltype(op(v[0]))> >
Map a function over a std::vector.
A type that represents a parameter pack of zero or more integers.
Unary metafunction that generates an index_tuple containing [0, Size).
typename make_index_tuple< Size-1 >::type::template append< Size-1 > type
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.
DynList< int > l