DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
tuple_utils.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_TUPLE_UTILS_H
13 #define DOCWIRE_TUPLE_UTILS_H
14 
15 #include <tuple>
16 
24 {
25 
34 template <std::size_t Start, std::size_t Length, typename... Ts>
35 auto subrange(const std::tuple<Ts...>& tuple) {
36  if constexpr (Length == 0) {
37  return std::tuple<>();
38  } else {
39  static_assert(Start < sizeof...(Ts), "Start index out of range");
40  static_assert(Start + Length <= sizeof...(Ts), "Length out of range");
41  return std::tuple_cat(std::make_tuple(std::get<Start>(tuple)), subrange<Start + 1, Length - 1>(tuple));
42  }
43 }
44 
51 template <std::size_t Start, std::size_t Length, typename T>
52 using subrange_t = decltype(subrange<Start, Length>(std::declval<T>()));
53 
63 template <typename T>
64 auto remove_first(T&& tuple) {
65  static_assert(std::tuple_size_v<T> > 0, "Cannot remove the first element of an empty tuple.");
66  return subrange<1, std::tuple_size_v<T> - 1>(tuple);
67 }
68 
75 template <typename T>
76 using remove_first_t = decltype(remove_first(std::declval<T>()));
77 
87 template <typename T>
88 auto remove_last(T&& tuple) {
89  static_assert(std::tuple_size_v<T> > 0, "Cannot remove the last element of an empty tuple.");
90  return subrange<0, std::tuple_size_v<T> - 1>(tuple);
91 }
92 
99 template <typename T>
100 using remove_last_t = decltype(remove_last(std::declval<T>()));
101 
108 template <typename T>
109 auto first_element(T&& tuple) {
110  static_assert(std::tuple_size_v<T> > 0, "Cannot get the first element of an empty tuple.");
111  return std::get<0>(tuple);
112 }
113 
120 template <typename T>
121 using first_element_t = decltype(first_element(std::declval<T>()));
122 
129 template <typename T>
130 auto last_element(T&& tuple) {
131  static_assert(std::tuple_size_v<T> > 0, "Cannot get the last element of an empty tuple.");
132  return std::get<std::tuple_size_v<T> - 1>(tuple);
133 }
134 
141 template <typename T>
142 using last_element_t = decltype(last_element(std::declval<T>()));
143 
144 } // namespace docwire::tuple_utils
145 
146 #endif //DOCWIRE_TUPLE_UTILS_H
Provides transformations for tuples.
Definition: tuple_utils.h:24
decltype(last_element(std::declval< T >())) last_element_t
Type alias for the type of the last element of a tuple.
Definition: tuple_utils.h:142
auto subrange(const std::tuple< Ts... > &tuple)
Returns subrange of a tuple.
Definition: tuple_utils.h:35
decltype(remove_first(std::declval< T >())) remove_first_t
Type alias for the type of the result of removing the first element from a tuple.
Definition: tuple_utils.h:76
auto last_element(T &&tuple)
Returns the last element of a tuple.
Definition: tuple_utils.h:130
auto remove_last(T &&tuple)
Removes the last element from a tuple.
Definition: tuple_utils.h:88
decltype(remove_last(std::declval< T >())) remove_last_t
Type alias for the type of the result of removing the last element from a tuple.
Definition: tuple_utils.h:100
decltype(subrange< Start, Length >(std::declval< T >())) subrange_t
Type alias for the type of the result of subrange of a tuple.
Definition: tuple_utils.h:52
decltype(first_element(std::declval< T >())) first_element_t
Type alias for the type of the first element of a tuple.
Definition: tuple_utils.h:121
auto remove_first(T &&tuple)
Removes the first element from a tuple.
Definition: tuple_utils.h:64
auto first_element(T &&tuple)
Returns the first element of a tuple.
Definition: tuple_utils.h:109