Visual Computing Library
Loading...
Searching...
No Matches
nested_initializer_lists.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MISC_NESTED_INITIALIZER_LISTS_H
24#define VCL_MISC_NESTED_INITIALIZER_LISTS_H
25
26/* @link https://zcrou.com/blog/dev/nested-initializers */
27
28#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <initializer_list>
32#include <list>
33
34namespace vcl {
35
36// Traits
37namespace detail {
38
39// recursive case: a nested list is a list of L-1 levels
40template<typename T, std::size_t L>
41struct NestedInitializerListsTraits
42{
43 using type = std::initializer_list<
44 typename NestedInitializerListsTraits<T, L - 1>::type>;
45};
46
47// base case: no list, only the type T
48template<typename T>
49struct NestedInitializerListsTraits<T, 0>
50{
51 using type = T;
52};
53
54} // namespace detail
55
56// Processors that allow to automatically iterate through nested initializer
57// lists
58
65template<typename T, std::size_t L>
66using NestedInitializerLists = detail::NestedInitializerListsTraits<T, L>::type;
67
68// Recursive part.
69
76template<typename T, std::size_t L>
78{
79public:
103 static std::list<size_t> maxDimensionsLevels(
105 {
106 std::list<size_t> final;
107 bool first = true;
108 for (auto nested : values) {
109 std::list<size_t> l =
111 nested);
112
113 if (first) {
114 first = false;
115 for (size_t s : l)
116 final.push_back(s);
117 }
118 else {
119 assert(l.size() == final.size());
120 std::list<size_t>::iterator it = l.begin();
121 for (size_t& s : final) {
122 if (s < *it)
123 s = *it;
124 ++it;
125 }
126 }
127 }
128 final.push_front(values.size());
129 return final;
130 }
131
154 template<typename T_Function>
164
192 template<typename T_Function>
193 static void processElements(
196 std::list<size_t> sizes)
197 {
198 size_t curr_size = sizes.front();
199 sizes.pop_front();
200 for (auto nested : values) {
203 }
204
205 // 0 in left values
206 if (values.size() < curr_size) {
207 std::size_t count = 1;
208 for (size_t s : sizes)
209 count *= s;
210
211 while (count-- > 0)
212 function(static_cast<T>(0));
213 }
214 }
215};
216
217// Last level.
218
219template<typename T>
221{
222public:
224
225 static std::list<size_t> maxDimensionsLevels(InitializerList values)
226 {
227 std::list<size_t> dim;
228 dim.push_back(values.size());
229 return dim;
230 }
231
232 template<typename T_Function>
233 static void processElements(
236 std::list<size_t> sizes)
237 {
238 size_t row_size = 1;
239 for (size_t s : sizes)
240 row_size *= s;
241 std::for_each(values.begin(), values.end(), function);
242
243 // 0 in left values
244 if (values.size() < row_size) {
245 std::size_t count = row_size - values.size();
246 while (count-- > 0) {
247 function(static_cast<T>(0));
248 }
249 }
250 }
251
252 template<typename T_Function>
254 {
255 std::for_each(values.begin(), values.end(), function);
256 }
257};
258
259} // namespace vcl
260
261#endif // VCL_MISC_NESTED_INITIALIZER_LISTS_H
The NestedInitializerListsProcessor class.
Definition nested_initializer_lists.h:78
static void processElements(NestedInitializerLists< T, L > values, T_Function function, std::list< size_t > sizes)
Applies the lambda function passed as parameter to all the elements of the NestedInitializerLists.
Definition nested_initializer_lists.h:193
static std::list< size_t > maxDimensionsLevels(NestedInitializerLists< T, L > values)
Returns a list containing the maximum size of elements for every dimension.
Definition nested_initializer_lists.h:103
static void processElements(NestedInitializerLists< T, L > values, T_Function function)
Applies the lambda function passed as parameter to all the elements of the NestedInitializerLists.
Definition nested_initializer_lists.h:155
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43