Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_CONTAINERS_UTILS_HPP_
2#define IV_SRC_CONTAINERS_UTILS_HPP_
3
4#include <cstdint>
5#include <fmt/format.h>
6#include <memory>
7#include <numeric>
8#include <string>
9#include <vector>
10
11namespace iv::utils
12{
13
14namespace map
15{
16
17}
18
19namespace vector
20{
21
22template<typename T>
23void concatenate(std::vector<T> *v, const std::vector<T> &other)
24{
25 v->insert(v->end(), other.begin(), other.end());
26}
27
28void createData(std::vector<bool> *bytes, const unsigned char *data, uint32_t numBytesData);
29
30void createDataInOrder(std::vector<bool> *bytes, const unsigned char *data, uint32_t numBytesData);
31
32template<typename T>
33T summatory(std::vector<T> &v)
34{
35 static_assert(std::is_arithmetic_v<T>, "T must be arithmetic type");
36 return std::accumulate(v.begin(), v.end(), T {0});
37}
38
39template<typename T>
40std::string toString(char separator, const std::vector<T> &v)
41{
42
43 if (v.empty())
44 {
45 return "{}";
46 }
47
48 std::string result = "{";
49
50 for (const auto &item: v)
51 {
52 result += fmt::format("{}{}", item, separator);
53 }
54
55 // Remove the last separator
56 result.pop_back();
57 result += "}";
58
59 return result;
60}
61
62template<typename T>
63std::unique_ptr<uint32_t[]> getUIntPointer(std::vector<T> v)
64{
65 static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
66
67 uint64_t numPoints = v.size();
68
69 if (numPoints > 0)
70 {
71 auto points = std::make_unique<uint32_t[]>(numPoints);
72
73 for (uint64_t indPoint = 0; indPoint < numPoints; indPoint++)
74 {
75 points[indPoint] = static_cast<uint32_t>(v.at(indPoint));
76 }
77
78 return points;
79 }
80
81 return nullptr;
82}
83
84}// namespace vector
85
86}// namespace iv::utils
87
88#endif//IV_SRC_CONTAINERS_UTILS_HPP_
void createData(std::vector< bool > *bytes, const unsigned char *data, uint32_t numBytesData)
Definition utils.cpp:8
T summatory(std::vector< T > &v)
Definition utils.hpp:33
std::string toString(char separator, const std::vector< T > &v)
Definition utils.hpp:40
std::unique_ptr< uint32_t[]> getUIntPointer(std::vector< T > v)
Definition utils.hpp:63
void concatenate(std::vector< T > *v, const std::vector< T > &other)
Definition utils.hpp:23
void createDataInOrder(std::vector< bool > *bytes, const unsigned char *data, uint32_t numBytesData)
Definition utils.cpp:21
Definition utils.cpp:6