17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__VECTORS 18 #define COM_BORA_SOFTWARE__BALAU_UTIL__VECTORS 39 template <
typename T,
typename ... PT>
40 static void pushBack(std::vector<T> & vector, T && first, PT && ... remaining) {
41 vector.emplace_back(std::forward<T>(first));
42 pushBack(vector, std::forward<PT>(remaining) ...);
58 template <
typename T,
typename ... PT>
59 static std::vector<T>
pushBack(T && first, PT && ... remaining) {
60 std::vector<T> vector;
61 vector.emplace_back(std::forward<T>(first));
62 pushBack(vector, std::forward<PT>(remaining) ...);
71 template <
typename T>
static std::vector<T>
pushBack() {
72 return std::vector<T>();
87 template <
typename T,
typename T2,
typename Compare = std::equal_to<T>,
typename Replace = std::function<
void (T &, T2 &&)>>
88 static void pushBackOrReplace(std::vector<T> & dst, T2 && element, Compare compare = std::equal_to<T>(), Replace replace = DefaultReplace<T, T2>()) {
89 auto existing = std::find_if(
90 dst.begin(), dst.end(), [&element, &compare] (
const auto & dstElement) {
return compare(dstElement, element); }
93 if (existing != dst.end()) {
94 replace(*existing, std::forward<T2>(element));
96 dst.push_back(std::forward<T2>(element));
105 template <
typename T>
static void append(std::vector<T> & dst,
const std::vector<T> & src) {
106 dst.insert(std::end(dst), std::begin(src), std::end(src));
112 template <
typename T>
static void move(std::vector<T> & dst,
const std::vector<T> & src) {
113 std::move(src.begin(), src.end(), std::back_inserter(dst));
121 template <
typename D,
typename S>
122 static std::vector<D>
map(
const std::vector<S> & input, std::function<D (
const S &)> f) {
124 std::transform(input.begin(), input.end(), std::back_inserter(ret), f);
134 std::vector<char> ret;
135 ret.resize(str.length());
136 std::memcpy(ret.data(), str.c_str(), str.length());
143 static std::vector<char32_t>
toCharVector(
const std::u32string & str) {
144 std::vector<char32_t> ret;
145 ret.resize(str.length());
146 std::memcpy(ret.data(), str.c_str(), str.length() *
sizeof(char32_t));
153 template <
typename T>
static std::vector<std::string>
toStringVector(
const std::vector<T> & vector) {
154 std::vector<std::string> ret;
155 ret.reserve(vector.size());
159 for (
size_t m = 0; m < vector.size(); m++) {
160 ret.emplace_back(
toString(vector[m]));
169 template <
typename T>
static std::vector<std::u32string>
toString32Vector(
const std::vector<T> & vector) {
170 std::vector<std::u32string> ret;
171 ret.reserve(vector.size());
173 for (
size_t m = 0; m < vector.size(); m++) {
186 return std::string(vector.data(), vector.size());
193 return std::u16string(vector.data(), vector.size());
200 return std::u32string(vector.data(), vector.size());
206 private:
template <
typename T,
typename T2>
struct DefaultReplace {
207 void operator () (T & lhs, T2 && rhs) {
208 lhs = std::forward<T2>(rhs);
219 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__VECTORS Pre-defined universal to-string functions.
static void pushBack(std::vector< T > &vector, T &&first, PT &&... remaining)
Populate an existing vector via emplace back of multiple elements.
Definition: Vectors.hpp:40
static std::vector< char > toCharVector(const std::string &str)
Convert the supplied UTF-8 string to a char vector.
Definition: Vectors.hpp:133
static std::vector< T > pushBack()
Create and populate a vector via emplace back of multiple elements (empty case).
Definition: Vectors.hpp:71
U8String< AllocatorT > toString(const ZipEntryInfo &info)
Print the zip entry info as a UTF-8 string.
Definition: Zip.hpp:113
static std::vector< std::u32string > toString32Vector(const std::vector< T > &vector)
Convert the supplied vector to a vector of UTF-32 strings.
Definition: Vectors.hpp:169
Utilities for vectors.
Definition: Vectors.hpp:31
Balau::U8String< AllocatorT > toString(const BalauException &e)
Base class toString<AllocatorT> function for Balau exceptions.
Definition: BalauException.hpp:122
static std::vector< T > pushBack(T &&first, PT &&... remaining)
Create and populate a vector via emplace back of multiple elements.
Definition: Vectors.hpp:59
static void pushBackOrReplace(std::vector< T > &dst, T2 &&element, Compare compare=std::equal_to< T >(), Replace replace=DefaultReplace< T, T2 >())
Appends the supplied element to the vector or replaces an existing element if a matching one is found...
Definition: Vectors.hpp:88
static std::vector< char32_t > toCharVector(const std::u32string &str)
Convert the supplied UTF-32 string to a char32_t vector.
Definition: Vectors.hpp:143
static std::vector< std::string > toStringVector(const std::vector< T > &vector)
Convert the supplied vector to a vector of UTF-8 strings.
Definition: Vectors.hpp:153
static void move(std::vector< T > &dst, const std::vector< T > &src)
Moves the source vector into the destination vector.
Definition: Vectors.hpp:112
static std::u32string charsToString(const std::vector< char32_t > &vector)
Convert the characters in the supplied char32_t vector to a UTF-32.
Definition: Vectors.hpp:199
static void append(std::vector< T > &dst, const std::vector< T > &src)
Appends the source vector to the destination vector.
Definition: Vectors.hpp:105
static std::string charsToString(const std::vector< char > &vector)
Convert the characters in the supplied char vector to a UTF-8.
Definition: Vectors.hpp:185
static std::vector< D > map(const std::vector< S > &input, std::function< D(const S &)> f)
Perform a map operator from one vector to another (transform the input elements to a vector of differ...
Definition: Vectors.hpp:122
static void pushBack(std::vector< T > &)
Populate an existing vector via emplace back of multiple elements (end case).
Definition: Vectors.hpp:51
Balau::U32String< AllocatorT > toString32(const P1 &p1, const P2 &p2, const P &... p)
Concatenates the to-string values of the input arguments.
Definition: ToStringA.hpp:1267
static std::u16string charsToString(const std::vector< char16_t > &vector)
Convert the characters in the supplied char16_t vector to a UTF-16.
Definition: Vectors.hpp:192