qxLib
utils.h
Go to the documentation of this file.
1 /**
2 
3  @file utils.h
4  @author Khrapov
5  @date 6.08.2022
6  @copyright © Nick Khrapov, 2022. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <algorithm>
12 #include <array>
13 #include <span>
14 
15 namespace qx
16 {
17 
18 /**
19  @brief Call destructors
20  @tparam iterator_t - iterator type
21  @param itStart - start iterator
22  @param itEnd - end iterator
23 **/
24 template<class iterator_t>
25 inline void destruct(iterator_t itStart, iterator_t itEnd);
26 
27 /**
28  @brief Fill array with value in constructor
29  @tparam N - number of elements in array
30  @tparam T - array value type
31  @param init_val - init value
32  @retval - filled array
33 **/
34 template<size_t N, class T>
35 constexpr auto make_array(T init_val = T());
36 
37 /**
38  @brief Join arrays at compile-time
39  @tparam T - array value_type
40  @tparam LeftLength - left array length
41  @tparam RightLength - right array length
42  @param rhs - left array
43  @param lhs - right array
44  @retval - new array where elements from the right array placed after elements from the left array
45 **/
46 template<class T, std::size_t LeftLength, std::size_t RightLength>
47 constexpr std::array<T, LeftLength + RightLength> join_arrays(
48  std::array<T, LeftLength> rhs,
49  std::array<T, RightLength> lhs);
50 
51 /**
52  @brief Convert a span to an array
53  @tparam N - span and array compile time size
54  @tparam T - span and array type
55  @param span - span object
56  @retval - array object
57 **/
58 template<std::size_t N, class T>
59 constexpr std::array<T, N> span_to_array(const std::span<T, N>& span);
60 
61 /**
62  @brief Create a container by constructing each element from the corresponding
63  element of the original container
64  @tparam result_container_t - target container type, must support value_type and push_back
65  @tparam container_t - original container type, must support forward iteration
66  @param from - original container
67  @retval - target container
68 **/
69 template<class result_container_t, class container_t>
70 result_container_t make_container(const container_t& from);
71 
72 /**
73  @brief Get the size of memory allocated for container elements
74  @tparam container_t - container type
75  @param container - container const ref
76  @retval - the size of memory allocated for container elements
77 **/
78 template<class container_t>
79 constexpr size_t bytes_size(const container_t& container);
80 
81 } // namespace qx
82 
83 #include <qx/containers/utils.inl>
void destruct(iterator_t itStart, iterator_t itEnd)
Call destructors.
Definition: utils.inl:14
constexpr std::array< T, N > span_to_array(const std::span< T, N > &span)
Convert a span to an array.
Definition: utils.inl:44
constexpr size_t bytes_size(const container_t &container)
Get the size of memory allocated for container elements.
Definition: utils.inl:63
constexpr auto make_array(T init_val=T())
Fill array with value in constructor.
Definition: utils.inl:25
result_container_t make_container(const container_t &from)
Create a container by constructing each element from the corresponding element of the original contai...
Definition: utils.inl:52
constexpr std::array< T, LeftLength+RightLength > join_arrays(std::array< T, LeftLength > rhs, std::array< T, RightLength > lhs)
Join arrays at compile-time.
Definition: utils.inl:33