Vectors.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__VECTORS
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__VECTORS
19 
20 #include <Balau/Type/ToString.hpp>
21 
22 #include <cstring>
23 #include <memory>
24 #include <vector>
25 
26 namespace Balau::Util {
27 
31 struct Vectors final {
33 
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) ...);
43  }
44 
50  template <typename T>
51  static void pushBack(std::vector<T> & ) {}
52 
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) ...);
63  return vector;
64  }
65 
71  template <typename T> static std::vector<T> pushBack() {
72  return std::vector<T>();
73  }
74 
76 
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); }
91  );
92 
93  if (existing != dst.end()) {
94  replace(*existing, std::forward<T2>(element));
95  } else {
96  dst.push_back(std::forward<T2>(element));
97  }
98  }
99 
101 
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));
107  }
108 
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));
114  }
115 
117 
121  template <typename D, typename S>
122  static std::vector<D> map(const std::vector<S> & input, std::function<D (const S &)> f) {
123  std::vector<D> ret;
124  std::transform(input.begin(), input.end(), std::back_inserter(ret), f);
125  return ret;
126  }
127 
129 
133  static std::vector<char> toCharVector(const std::string & str) {
134  std::vector<char> ret;
135  ret.resize(str.length());
136  std::memcpy(ret.data(), str.c_str(), str.length());
137  return ret;
138  }
139 
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));
147  return ret;
148  }
149 
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());
156 
158 
159  for (size_t m = 0; m < vector.size(); m++) {
160  ret.emplace_back(toString(vector[m]));
161  }
162 
163  return ret;
164  }
165 
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());
172 
173  for (size_t m = 0; m < vector.size(); m++) {
174  ret.emplace_back(toString32(vector[m]));
175  }
176 
177  return ret;
178  }
179 
181 
185  static std::string charsToString(const std::vector<char> & vector) {
186  return std::string(vector.data(), vector.size());
187  }
188 
192  static std::u16string charsToString(const std::vector<char16_t> & vector) {
193  return std::u16string(vector.data(), vector.size());
194  }
195 
199  static std::u32string charsToString(const std::vector<char32_t> & vector) {
200  return std::u32string(vector.data(), vector.size());
201  }
202 
204 
205  // The default copy/move replace function used in pushBackOrReplace.
206  private: template <typename T, typename T2> struct DefaultReplace {
207  void operator () (T & lhs, T2 && rhs) {
208  lhs = std::forward<T2>(rhs);
209  }
210  };
211 
212  Vectors() = delete;
213  Vectors(const Vectors &) = delete;
214  Vectors & operator = (const Vectors &) = delete;
215 };
216 
217 } // namespace Balau::Util
218 
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
Utility functions.
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