Aleph-w 3.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
64namespace Aleph
65{
66
83template<class F, class...Ts, std::size_t...Is>
84inline void tuple_for_each(const std::tuple<Ts...> & tuple,
85 F func, std::index_sequence<Is...>)
86{
87 // Use fold expression (C++17) or initializer list expansion
88 // The (void) cast silences unused-value warnings
89 using expander = int[];
90 (void)func; // Silence warning when tuple is empty
91 (void)expander { 0, ((void)func(std::get<Is>(tuple)), 0)... };
92}
93
105template<class F, class...Ts>
106inline void tuple_for_each(const std::tuple<Ts...> & tuple, F func)
107{
108 tuple_for_each(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
109}
110
112
125template <typename T>
126[[nodiscard]] inline DynList<T> vector_to_DynList(const std::vector<T> & v)
127{
129 for (const auto & item : v)
130 ret.append(item);
131 return ret;
132}
133
142template <typename T>
143[[nodiscard]] inline DynList<T> to_DynList(const std::vector<T> & v)
144{
145 return vector_to_DynList(v);
146}
147
157template <typename Iterator>
158[[nodiscard]] inline auto range_to_DynList(Iterator begin, Iterator end)
159 -> DynList<std::decay_t<decltype(*begin)>>
160{
161 DynList<std::decay_t<decltype(*begin)>> ret;
162 for (auto it = begin; it != end; ++it)
163 ret.append(*it);
164 return ret;
165}
166
175template <typename T>
176[[nodiscard]] inline Array<T> to_Array(const std::vector<T> & v)
177{
179 for (const auto & item : v)
180 ret.append(item);
181 return ret;
182}
183
195template <typename Container>
196[[nodiscard]] inline std::vector<typename Container::Item_Type>
197to_vector(const Container & container)
198{
199 std::vector<typename Container::Item_Type> ret;
200 for (const auto & item : container)
201 ret.push_back(item);
202 return ret;
203}
204
215template <typename T, class Op>
216[[nodiscard]] inline auto map_vector(const std::vector<T> & v, Op op)
217 -> std::vector<std::decay_t<decltype(op(v[0]))>>
218{
219 using ResultType = std::decay_t<decltype(op(v[0]))>;
220 std::vector<ResultType> ret;
221 ret.reserve(v.size());
222 for (const auto & item : v)
223 ret.push_back(op(item));
224 return ret;
225}
226
236template <typename T, typename... Args>
237[[nodiscard]] inline std::vector<T> variadic_to_vector(Args&&... args)
238{
239 return { static_cast<T>(std::forward<Args>(args))... };
240}
241
251template <typename T, typename... Args>
253{
254 return to_DynList<T>(variadic_to_vector<T>(std::forward<Args>(args)...));
255}
256
257
258/* Boost Software License - Version 1.0 - August 17th, 2003 Permission
259 is hereby granted, free of charge, to any person or organization
260 obtaining a copy of the software and accompanying documentation
261 covered by this license (the "Software") to use, reproduce,
262 display, distribute, execute, and transmit the Software, and to
263 prepare derivative works of the Software, and to permit
264 third-parties to whom the Software is furnished to do so, all
265 subject to the following:
266
267 The copyright notices in the Software and this entire statement,
268 including the above license grant, this restriction and the
269 following disclaimer, must be included in all copies of the
270 Software, in whole or in part, and all derivative works of the
271 Software, unless such copies or derivative works are solely in the
272 form of machine-executable object code generated by a source
273 language processor.
274
275 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
276 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
277 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
278 NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
279 DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER
280 LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM,
281 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
282 DEALINGS IN THE SOFTWARE.
283*/
284
293template<unsigned... Indexes>
295{
300 template<unsigned N>
302};
303
312template<unsigned Size>
314{
315 using type = typename make_index_tuple<Size-1>::type::template append<Size-1>;
316};
317
320template<>
321struct make_index_tuple<0u>
322{
323 using type = index_tuple<>;
324};
326
333template<typename... Types>
334using to_index_tuple = typename make_index_tuple<sizeof...(Types)>::type;
335
337// Based on:
338// http://stackoverflow.com/questions/10604794/convert-stdtuple-to-stdarray-c11
339
340template<class Container, typename T, typename... U, unsigned... I>
341[[nodiscard]] inline static Container
342tuple_to_container_impl(const std::tuple<T, U...> & t, index_tuple<I...>)
343{
344 return build_container<Container>(std::get<I>(t)...);
345}
347
360template<class Container, typename T, typename... U>
361[[nodiscard]] inline Container tuple_to_container(const std::tuple<T, U...> & t)
362{
363 using IndexTuple = typename make_index_tuple<1 + sizeof...(U)>::type;
365}
366
376template<typename T, typename... U>
377[[nodiscard]] inline DynList<T> tuple_to_dynlist(const std::tuple<T, U...> & t)
378{
380}
381
391template<typename T, typename... U>
392[[nodiscard]] inline Array<T> tuple_to_array(const std::tuple<T, U...> & t)
393{
395}
396
408template <typename StlContainer>
409[[nodiscard]] inline auto stl_container_to_dynList(const StlContainer & container)
411{
413 for (const auto & item : container)
414 ret.append(item);
415 return ret;
416}
417
426template <typename T>
427[[nodiscard]] inline DynList<T> list_to_DynList(const std::list<T> & l)
428{
430 for (const auto & item : l)
431 ret.append(item);
432 return ret;
433}
434
443template <typename T>
444[[nodiscard]] inline std::list<T> DynList_to_list(const DynList<T> & l)
445{
446 std::list<T> ret;
447 for (const auto & item : l)
448 ret.push_back(item);
449 return ret;
450}
451
463template <typename T>
464[[nodiscard]] inline std::vector<T> DynArray_to_vector(const DynArray<T> & arr)
465{
466 std::vector<T> ret;
467 ret.reserve(arr.size());
468 for (size_t i = 0; i < arr.size(); ++i)
469 ret.push_back(arr[i]);
470 return ret;
471}
472
474
487template <typename T>
488[[nodiscard]] inline DynArray<T> vector_to_DynArray(const std::vector<T> & v)
489{
491 for (const auto & item : v)
492 ret.append(item);
493 return ret;
494}
495
496} // end namespace Aleph
497
499// Keep backward compatibility: expose commonly used functions at global scope
503using Aleph::to_Array;
504using Aleph::to_vector;
518
519#endif // AH_STL_UTILS_H
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
size_t size() const noexcept
Return the current dimension of array.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
#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
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:147
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:107
DynList< T > vector_to_DynList(const std::vector< T > &v)
Convert a std::vector to a DynList.
Definition ah-convert.H:250
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.
Array< typename C::Item_Type > to_Array(const C &c)
Convert a container to an Array.
Definition ah-convert.H:167
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.
Definition ah-convert.H:291
std::vector< T > DynArray_to_vector(const DynArray< T > &arr)
Convert a DynArray to a std::vector.
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.
Definition ah-convert.H:228
DynList< T > maps(const C &c, Op op)
Classic map operation.
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