Utilities
Utilities header only library
ExtSTD.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_EXT_STD_H
7 #define UTILS_EXT_STD_H
8 #include <optional>
9 #include <vector>
10 #include <set>
11 #include <unordered_set>
12 #include <map>
13 #include <unordered_map>
14 #include <cmath>
15 
16 #include <iostream>
17 #include <fstream>
18 #include <algorithm>
19 #include <iomanip>
20 
21 #include <locale>
22 #include <functional>
23 
24 #include <string>
25 #include <sstream>
26 #include <regex>
27 #include <iterator>
28 #include <numeric>
29 
30 
31 
33 namespace std_ext
34 {
35 
36  const std::string WHITESPACES = " \t\n\r\f\v";
37 
38 
39  inline void StringTo(const std::string &str, float &out)
40  {
41  out = std::stof(str);
42  }
43  inline void StringTo(const std::string &str, double &out)
44  {
45  out = std::stod(str);
46  }
47  inline void StringTo(const std::string &str, int32_t &out)
48  {
49  out = std::stoi(str);
50  }
51  inline void StringTo(const std::string &str, int64_t &out)
52  {
53  out = std::stol(str);
54  }
55  inline void StringTo(const std::string &str, uint32_t &out)
56  {
57  out = static_cast<unsigned>(std::stoul(str));
58  }
59  inline void StringTo(const std::string &str, uint64_t &out)
60  {
61  out = std::stoul(str);
62  }
63 
68  inline std::wstring ToWString(const std::string &text)
69  {
70  return std::wstring(text.begin(), text.end());
71  }
72 
74  inline std::string ToStdString(const std::wstring &text)
75  {
76  return std::string(text.begin(), text.end());
77  }
78 
80  inline size_t CountOccurrences(const std::string &text, const std::string &key_to_search)
81  {
82  size_t count = 0;
83  for (size_t found_at_pos = 0; ; found_at_pos += key_to_search.length())
84  {
85  found_at_pos = text.find(key_to_search, found_at_pos);
86  if (found_at_pos == std::string::npos)
87  {
88  break;
89  }
90  count++;
91  }
92  return count;
93  }
94 
96  inline size_t CountOccurrences(const std::string &text, const std::string &key_to_search, const size_t &off, const size_t &length)
97  {
98  if (length < key_to_search.length())
99  {
100  return size_t(0);
101  }
102  size_t count = 0;
103  auto last_pos = off + length - key_to_search.length();
104  for (size_t found_at_pos = off; ; found_at_pos += key_to_search.length())
105  {
106  found_at_pos = text.find(key_to_search, found_at_pos);
107  if (found_at_pos == std::string::npos || found_at_pos >= last_pos)
108  {
109  break;
110  }
111  count++;
112  }
113  return count;
114  }
115 
117  inline std::string Replace(const std::string &text, const std::string &key_to_search, const std::string &replace_text)
118  {
119  std::string result = text;
120  for (size_t found_at_pos = 0; ; found_at_pos += replace_text.length())
121  {
122  found_at_pos = result.find(key_to_search, found_at_pos);
123  if (found_at_pos == std::string::npos)
124  {
125  break;
126  }
127  result.replace(found_at_pos, key_to_search.length(), replace_text);
128  }
129  return result;
130  }
131 
136  inline std::string Replace(const std::string &text, const std::map<std::string, std::string> &replacement_map)
137  {
138  std::string result = text;
139  for (const auto &mapped : replacement_map)
140  {
141  result = Replace(result, mapped.first, mapped.second);
142  }
143  return result;
144  }
145 
147  template <typename T>
148  inline void Replace(std::vector<T> &vector, const T old_elem, const T new_elem)
149  {
150  for (unsigned int i = 0; i < vector.size(); i++)
151  {
152  if (vector[i] == old_elem)
153  {
154  vector[i] = new_elem;
155  }
156  }
157  }
158 
160  inline std::string BoolToString(const bool value)
161  {
162  return value ? "true" : "false";
163  }
164 
173  inline std::string ToStringAutoPrecision(const double value, int max_digits = 6)
174  {
175  unsigned uvalue = static_cast<unsigned>(std::round(std::abs(value * std::pow(10, max_digits))));
176  while (uvalue % 10 == 0)
177  {
178  uvalue /= 10;
179  max_digits--;
180  }
181 
182  std::string result;
183  if (uvalue > 0)
184  {
185  while (uvalue > 0 && max_digits > 0)
186  {
187  result += ('0' + uvalue % 10);
188 
189  uvalue /= 10;
190  max_digits--;
191  }
192  while (max_digits-- > 0)
193  {
194  result += "0";
195  }
196  }
197  std::reverse(result.begin(), result.end());
198  if (result.empty())
199  {
200  result = std::to_string(static_cast<int>(value));
201  }
202  else if (static_cast<int>(value) != 0)
203  {
204  result = std::to_string(static_cast<int>(value)) + "." + result;
205  }
206  else
207  {
208  result = value > 0.0 ? "0." + result : "-0." + result;
209  }
210  return result;
211  }
212 
214  template <typename T>
215  inline std::string ToStringWithPrecision(const T value, const int n = 6)
216  {
217  std::ostringstream out;
218  out << std::setprecision(n) << value;
219  return out.str();
220  }
221 
223  template <typename T>
224  inline std::string ToStringWithZeroes(const T value, const int n = 6)
225  {
226  std::stringstream ss;
227  ss << std::setw(n) << std::setfill('0') << value;
228  return ss.str();
229  }
230 
232  template <typename T>
233  inline std::string ToString(const std::vector<T> &container, const std::string &separator = ", ")
234  {
235  std::string result;
236  for (auto i = 0; i < container.size(); i++)
237  {
238  result += std::to_string(container[i]);
239  if (i < container.size() - 1)
240  {
241  result += separator;
242  }
243  }
244  return result;
245  }
246 
248  inline std::string ToString(const std::vector<std::string> &container, const std::string &separator = ", ")
249  {
250  std::string result;
251  for (size_t i = 0; i < container.size(); i++)
252  {
253  result += container[i];
254  if (i < container.size() - 1)
255  {
256  result += separator;
257  }
258  }
259  return result;
260  }
261 
269  template<typename T>
270  inline std::string ToStringWithThousandSeparators(const T &input)
271  {
272  std::stringstream temp_stream;
273  temp_stream.imbue(std::locale(""));
274  temp_stream << input;
275  return temp_stream.str();
276  }
277 
279  inline void MakeUppercase(char *text)
280  {
281  std::transform(text, text + strlen(text), text, [](char c) {return static_cast<char>(::toupper(c)); });
282  }
283 
285  inline void MakeUppercase(std::string &text)
286  {
287  MakeUppercase(const_cast<char *>(text.c_str()));
288  }
289 
291  inline std::string Uppercase(const std::string &text)
292  {
293  std::string upper = text;
294  std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
295  return upper;
296  }
297 
299  inline void MakeLowercase(char *text)
300  {
301  std::transform(text, text + strlen(text), text, [](char c) {return static_cast<char>(::towlower(c)); });
302  }
303 
305  inline void MakeLowercase(std::string &text)
306  {
307  MakeLowercase(const_cast<char *>(text.c_str()));
308  }
309 
311  inline std::string Lowercase(const std::string &text)
312  {
313  std::string lower = text;
314  std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
315  return lower;
316  }
317 
319  inline bool EndsWith(const std::string &ending, const std::string &text)
320  {
321  return
322  ending.size() <= text.size() &&
323  std::equal(ending.rbegin(), ending.rend(), text.rbegin());
324  }
325 
327  inline bool Contains(const std::string &substring, const std::string &text)
328  {
329  return text.find(substring) != std::string::npos;
330  }
331 
333  inline bool Contains(const char character, const std::string &text)
334  {
335  return text.find(character) != std::string::npos;
336  }
337 
342  inline std::string ReplacePatternWithNumber(const std::string &base_string, const std::string &pattern, const int number)
343  {
344  const auto regex = std::regex("(" + pattern + ")+");
345 
346  const auto it_begin = std::sregex_iterator(base_string.begin(), base_string.end(), regex);
347  const auto it_end = std::sregex_iterator();
348 
349  auto res_string = base_string;
350  for (std::sregex_iterator i = it_begin; i != it_end; ++i)
351  {
352  const std::string match_str = i->str();
353  res_string = std::regex_replace(res_string, std::regex(match_str), ToStringWithZeroes(number, static_cast<int>(match_str.length())), std::regex_constants::format_first_only);
354  }
355 
356  return res_string;
357  }
358 
360  template <typename T>
361  bool Contains(const T &element, const std::initializer_list<T> &list)
362  {
363  return std::find(list.begin(), list.end(), element) != list.end();
364  }
365 
367  template <typename Type>
368  bool Contains(const Type &element, const std::vector<Type> &vector)
369  {
370  return std::find(vector.begin(), vector.end(), element) != vector.end();
371  }
372 
374  template <typename Type>
375  std::optional<size_t> GetIndex(const Type element, const std::vector<Type> &vector)
376  {
377  const auto found_element = std::find(vector.begin(), vector.end(), element);
378  if (found_element == vector.end())
379  {
380  return std::nullopt;
381  }
382  return found_element - vector.begin();
383  }
384 
386  template <typename Type>
387  bool Contains(const Type element, const std::set<Type> &set)
388  {
389  return !set.empty() && set.find(element) != set.end();
390  }
391 
393  template <typename T1, typename T2>
394  bool Contains(const T1 element, const std::unordered_set<T1, T2> &set)
395  {
396  return set.find(element) != set.end();
397  }
398 
400  template <typename T1, typename T2>
401  bool Contains(const T1 element, const std::map<T1, T2> &map)
402  {
403  return map.find(element) != map.end();
404  }
405 
407  template <typename T1, typename T2>
408  bool Contains(const T1 element, const std::unordered_map<T1, T2> &map)
409  {
410  return map.find(element) != map.end();
411  }
412 
414  template <typename Type>
415  bool ContainsCommonElement(const std::vector<Type> &elements1, const std::set<Type> &elements2)
416  {
417  return AnyOf(elements1, [&elements2](const auto & el) { return elements2.find(el) != elements2.end(); });
418  }
419 
421  template <typename Type>
422  void SortAndRemoveDuplicates(std::vector<Type> &vec)
423  {
424  std::sort(vec.begin(), vec.end());
425  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
426  }
427 
429  template <typename Type>
430  std::vector<Type> RemoveDuplicates(std::vector<Type> &vector)
431  {
432  std::vector<Type> res;
433  res.reserve(vector.size());
434  for (unsigned i = 0; i < vector.size(); i++)
435  {
436  if (!Contains(vector[i], res))
437  {
438  res.push_back(vector[i]);
439  }
440  }
441  return res;
442  }
443 
445  template <typename Type>
446  bool Erase(const Type &element, std::vector<Type> &vector)
447  {
448  const std::optional<size_t> loc = GetIndex(element, vector);
449  if (loc.has_value())
450  {
451  vector.erase(vector.begin() + loc.value());
452  }
453  return loc.has_value();
454  }
455 
457  template <typename Type>
458  bool Erase(const Type &element, std::set<Type> &set)
459  {
460  if (Contains(element, set))
461  {
462  set.erase(element);
463  return true;
464  }
465  return false;
466  }
467 
469  template <typename IndicesType, typename Type>
470  std::vector<IndicesType> CollectIndices(const std::vector<Type> &vector, const Type value)
471  {
472  std::vector<IndicesType> indices;
473  for (IndicesType i = 0; i < static_cast<IndicesType>(vector.size()); ++i)
474  {
475  if (vector[i] == value)
476  {
477  indices.emplace_back(i);
478  }
479  }
480  return indices;
481  }
482 
484  template <typename T, typename F>
485  inline void Foreach(const T &container, const F &Function)
486  {
487  std::for_each(container.begin(), container.end(), Function);
488  }
489 
491  template <typename T>
492  [[nodiscard]]
493  inline auto MaxIt(const std::vector<T> &vector)
494  {
495  return std::max_element(vector.begin(), vector.end());
496  }
497 
499  template <typename T>
500  [[nodiscard]]
501  inline auto MinIt(const std::vector<T> &vector)
502  {
503  return std::min_element(vector.begin(), vector.end());
504  }
505 
507  template <typename T>
508  [[nodiscard]]
509  inline T Max(const std::vector<T> &vector)
510  {
511  return *MaxIt(vector);
512  }
513 
515  template <typename T>
516  [[nodiscard]]
517  inline T Min(const std::vector<T> &vector)
518  {
519  return *MinIt(vector);
520  }
521 
523  template <typename T>
524  [[nodiscard]]
525  inline T Max(const T val)
526  {
527  return val;
528  }
529 
531  template <typename T, typename ... TS>
532  [[nodiscard]]
533  T Max(const T val1, const T val2, const TS ... vals)
534  {
535  return Max(val1 > val2 ? val1 : val2, vals...);
536  }
537 
539  template <typename T>
540  [[nodiscard]]
541  inline T Min(const T val)
542  {
543  return val;
544  }
545 
547  template <typename T, typename... TS>
548  [[nodiscard]]
549  inline T Min(const T val1, const T val2, const TS ... vals)
550  {
551  return Min(val1 < val2 ? val1 : val2, vals ...);
552  }
553 
555  template <typename T, typename F>
556  [[nodiscard]]
557  inline auto Max(const std::vector<T> &vector, const F &Function)
558  {
559  return *std::max_element(vector.begin(), vector.end(), [&](T const & lhs, T const & rhs) { return Function(lhs) < Function(rhs); });
560  }
561 
563  template <typename T, typename F>
564  [[nodiscard]]
565  inline auto Min(const std::vector<T> &vector, const F &Function)
566  {
567  return *std::min_element(vector.begin(), vector.end(), [&](T const & lhs, T const & rhs) { return Function(lhs) < Function(rhs); });
568  }
569 
571  template <typename T, typename F>
572  [[nodiscard]]
573  inline auto MaxValue(const std::vector<T> &vector, const F &Function)
574  {
575  return Function(Max(vector, Function));
576  }
577 
579  template <typename T, typename F>
580  [[nodiscard]]
581  inline auto MinValue(const std::vector<T> &vector, const F &Function)
582  {
583  return Function(Min(vector, Function));
584  }
585 
587  template <typename T, typename F>
588  [[nodiscard]]
589  inline auto MiddleValue(const std::vector<T> &vector, const F &Function)
590  {
591  const auto min_value = MinValue(vector, Function);
592  const auto max_value = MaxValue(vector, Function);
593  return (min_value + max_value) / 2.0f;
594  }
595 
597  template <typename T>
598  [[nodiscard]]
599  inline auto Accumulate(const std::vector<T> &vector, const T start_value)
600  {
601  return std::accumulate(vector.begin(), vector.end(), start_value);
602  }
603 
605  template <typename T>
606  [[nodiscard]]
607  inline auto Sum(const std::vector<T> &vector)
608  {
609  return Accumulate(vector, static_cast<T>(0));
610  }
611 
613  template <typename T>
614  [[nodiscard]]
615  inline auto Average(const std::vector<T> &vector)
616  {
617  return vector.empty()
618  ? static_cast<T>(0)
619  : Sum(vector) * (1.0f / vector.size());
620  }
621 
629  template <typename T, typename F>
630  [[nodiscard]]
631  inline auto Accumulate(const std::vector<T> &vector, const T initial_value, const F &function)
632  {
633  return std::accumulate(vector.begin(), vector.end(), initial_value, [&function](const T total, const T value)->T { return function(value) + total; });
634  }
635 
637  template <typename T, typename F>
638  [[nodiscard]]
639  inline auto Sum(const std::vector<T> &vector, const F &function)
640  {
641  return Accumulate(vector, static_cast<T>(0), function);
642  }
643 
645  template <typename T, typename F>
646  [[nodiscard]]
647  inline float Average(const std::vector<T> &vector, const F &function)
648  {
649  return vector.empty()
650  ? 0.0f
651  : Sum(vector, function) / static_cast<float>(vector.size());
652  }
653 
655  template <typename T>
656  [[nodiscard]]
657  inline auto Median(const std::vector<T> &vector)
658  {
659  if (vector.empty())
660  {
661  return static_cast<T>(0);
662  }
663 
664  auto vec = vector;
665  const size_t n = vec.size() / 2;
666  std::nth_element(vec.begin(), vec.begin() + n, vec.end());
667  return vec[n];
668  }
669 
670 
672  template <typename T, typename S>
673  void InsertBack(T &target, const S &source)
674  {
675  target.insert(target.end(), source.begin(), source.end());
676  }
677 
679  template <typename T, typename S>
680  void InsertFront(T &target, const S &source)
681  {
682  target.insert(target.begin(), source.begin(), source.end());
683  }
684 
686  template <typename T, typename S>
687  void InsertTo(T &target, const S &source)
688  {
689  target.insert(source.begin(), source.end());
690  }
691 
692 
694  template <typename Type>
695  void EmplaceBackUnique(const Type &element, std::vector<Type> &vector)
696  {
697  if (!Contains(element, vector))
698  {
699  vector.emplace_back(element);
700  }
701  }
702 
703 
705  inline std::string &TrimBegin(std::string &s, const std::string &t = WHITESPACES)
706  {
707  s.erase(0, s.find_first_not_of(t));
708  return s;
709  }
710 
712  inline std::string &TrimEnd(std::string &s, const std::string &t = WHITESPACES)
713  {
714  s.erase(s.find_last_not_of(t) + 1);
715  return s;
716  }
717 
719  inline std::string &Trim(std::string &s, const std::string &t = WHITESPACES)
720  {
721  TrimBegin(s, t);
722  TrimEnd(s, t);
723  return s;
724  }
725 
727  inline std::string EndWithASingleSlash(const std::string &s)
728  {
729  auto result = s;
730  TrimEnd(result, "/\\");
731  return result + "/";
732  }
733 
735  inline std::vector<std::string> Parse(const std::string &text, const std::string &separators = WHITESPACES)
736  {
737  std::vector<std::string> result;
738  result.reserve(text.length());
739  size_t last_end_pos = 0;
740  while (true)
741  {
742  auto start_pos = text.find_first_not_of(separators, last_end_pos);
743  if (start_pos == std::string::npos)
744  {
745  break;
746  }
747  last_end_pos = text.find_first_of(separators, start_pos);
748  auto length = last_end_pos - start_pos;
749  result.emplace_back(text.substr(start_pos, length));
750  }
751  result.shrink_to_fit();
752  return result;
753  }
754 
755 
756 
758  template <typename T>
759  inline void Read(T &val, std::istream &str)
760  {
761  str.read(reinterpret_cast<char *>(&val), sizeof(T));
762  }
763 
765  template <typename T>
766  inline T Read(std::istream &str)
767  {
768  T val;
769  str.read(reinterpret_cast<char *>(&val), sizeof(T));
770  return val;
771  }
772 
774  template <typename T>
775  inline void Write(const T val, std::ostream &str)
776  {
777  str.write(reinterpret_cast<const char *>(&val), sizeof(T));
778  }
779 
780  inline void Read(std::string &, std::istream &)
781  {
782  throw std::invalid_argument("Function std_ext::Read() cannot be used for reading strings. Use std_ext::ReadString() instead.");
783  }
784 
785  inline void Write(const std::string &, std::ostream &)
786  {
787  throw std::invalid_argument("Function std_ext::Write() cannot be used for writing strings. Use std_ext::WriteString() instead.");
788  }
789 
791  inline void ReadString(std::string &text, std::istream &str)
792  {
793  unsigned int len = 0u;
794  str.read((char *)&len, sizeof(unsigned int));
795  char *temp = new char[len + 1];
796  str.read(temp, len);
797  temp[len] = '\0';
798  text = temp;
799  delete[] temp;
800  }
801 
803  inline std::string ReadString(std::istream &str)
804  {
805  std::string val;
806  ReadString(val, str);
807  return val;
808  }
809 
811  inline void WriteString(const std::string &text, std::ostream &str)
812  {
813  auto len = unsigned(text.size());
814  str.write((char *)&len, sizeof(unsigned));
815  str.write(text.c_str(), text.size());
816  }
817 
818 
819 
821  template<class C, class T>
822  [[nodiscard]]
823  auto Find(C &container, const T &value)
824  {
825  return std::find(container.begin(), container.end(), value);
826  }
827 
829  template<class C, class F>
830  [[nodiscard]]
831  auto Transformed(const C &container, F Function)
832  {
833  std::vector<decltype(Function(std::declval<typename C::value_type>()))> result;
834  std::transform(container.begin(), container.end(), std::back_inserter(result), Function);
835  return result;
836  }
837 
839  template<class C, class P>
840  [[nodiscard]]
841  auto AllOf(const C &container, const P &Predicate)->bool
842  {
843  return std::all_of(container.begin(), container.end(), Predicate);
844  }
845 
847  template<class C, class P>
848  [[nodiscard]]
849  auto AnyOf(const C &container, const P &Predicate)->bool
850  {
851  return std::any_of(container.begin(), container.end(), Predicate);
852  }
853 
855  template<class C, class P>
856  [[nodiscard]]
857  auto AnyOfW(C &container, const P &Predicate)->bool
858  {
859  return std::any_of(container.begin(), container.end(), Predicate);
860  }
861 
863  template<class C, class P>
864  [[nodiscard]]
865  auto NoneOf(const C &container, const P &Predicate)->bool
866  {
867  return std::none_of(container.begin(), container.end(), Predicate);
868  }
869 
871  template<class C, class T>
872  [[nodiscard]]
873  auto Count(const C &container, const T &val)
874  {
875  return std::count(container.begin(), container.end(), val);
876  }
877 
879  template<class C, class P>
880  [[nodiscard]]
881  auto CountIf(const C &container, const P &Predicate)
882  {
883  return std::count_if(container.begin(), container.end(), Predicate);
884  }
885 
887  template<class C, class P>
888  [[nodiscard]]
889  auto FindIf(C &container, const P &Predicate)
890  {
891  return std::find_if(container.begin(), container.end(), Predicate);
892  }
893 
895  template<class C, class P>
896  [[nodiscard]]
897  auto RevFindIf(C &container, const P &Predicate)
898  {
899  return std::find_if(container.rbegin(), container.rend(), Predicate);
900  }
901 
907  template<class C, class P>
908  [[nodiscard]]
909  auto FindIfPtr(C &container, const P &Predicate)
910  {
911  const auto it = std::find_if(container.begin(), container.end(), Predicate);
912  return it == container.end()
913  ? nullptr
914  : *it;
915  }
916 
918  template<class C, class P>
919  auto EraseIf(C &container, const P &Predicate)
920  {
921  auto rem = std::remove_if(container.begin(), container.end(), Predicate);
922  container.erase(rem, container.end());
923  }
924 
926  template<class C, class P>
927  [[nodiscard]]
928  auto SelectIf(const C &container, const P &Predicate)
929  {
930  C result;
931  std::copy_if(container.begin(), container.end(), std::back_inserter(result), Predicate);
932  return result;
933  }
934 
940  template<class C, class P>
941  [[nodiscard]]
942  std::pair<C, C> Partition(const C &container, const P &Predicate)
943  {
944  C result_true;
945  C result_false;
946  std::partition_copy(container.begin(), container.end(), std::back_inserter(result_true), std::back_inserter(result_false), Predicate);
947  return std::make_pair(result_true, result_false);
948  }
949 
954  template<class T, class P>
955  [[nodiscard]]
956  std::vector<std::pair<size_t, size_t>> FindIntervals(const std::vector<T> &vec, const P &Predicate)
957  {
958  std::vector<std::pair<size_t, size_t>> intervals;
959  std::optional<size_t> interval_start = std::nullopt;
960 
961  for (size_t i = 0; i < vec.size(); i++)
962  {
963  if (Predicate(vec[i]))
964  {
965  if (!interval_start)
966  {
967  interval_start = i;
968  }
969  }
970  else if (interval_start)
971  {
972  intervals.emplace_back(std::make_pair(*interval_start, i - 1));
973  interval_start = std::nullopt;
974  }
975  }
976 
977  return intervals;
978  }
979 
980  template<typename T>
981  std::vector<T> Joined(const std::vector<std::vector<T>> &vector_of_vectors)
982  {
983  std::vector<T> result;
984  for (const auto &vec : vector_of_vectors)
985  {
986  InsertBack(result, vec);
987  }
988  return result;
989  }
990 
991  template<typename T, typename F>
992  std::vector<T> JoinTransformed(const std::vector<std::vector<T>> &vector_of_vectors, const F &Function)
993  {
994  std::vector<T> result;
995  for (const auto &vec : vector_of_vectors)
996  {
997  InsertBack(result, Transformed(vec, Function));
998  }
999  return result;
1000  }
1001 
1003  template<typename F, typename O>
1004  inline auto Bind1(F method, O object)
1005  {
1006  return std::bind(method, object, std::placeholders::_1);
1007  }
1008 
1009 
1011  inline bool IsASCII(const std::string &s)
1012  {
1013  return AllOf(s, [](unsigned char c)
1014  {
1015  return isprint(c);
1016  });
1017  }
1018 }
1019 
1020 #endif /* !UTILS_EXT_STD_H */
std::string Lowercase(const std::string &text)
Converts text into all lowercase.
Definition: ExtSTD.h:311
void InsertBack(T &target, const S &source)
Insert the source vector to the back of target vector.
Definition: ExtSTD.h:673
std::string ToStringWithThousandSeparators(const T &input)
Converts a numeric value to string using thousand separators.
Definition: ExtSTD.h:270
auto AnyOf(const C &container, const P &Predicate) -> bool
Simplified wrapper for std::any_of.
Definition: ExtSTD.h:849
std::optional< size_t > GetIndex(const Type element, const std::vector< Type > &vector)
If an element exists in the vector, returns its index.
Definition: ExtSTD.h:375
auto EraseIf(C &container, const P &Predicate)
Simplified wrapper for std::remove_if and std::erase.
Definition: ExtSTD.h:919
size_t CountOccurrences(const std::string &text, const std::string &key_to_search)
Counts how many of specified key_to_search substrings does text contain.
Definition: ExtSTD.h:80
auto MiddleValue(const std::vector< T > &vector, const F &Function)
Finds a middle (average of min, max) value transformed by a function in a vector. ...
Definition: ExtSTD.h:589
auto Find(C &container, const T &value)
Simplified wrapper for std::find.
Definition: ExtSTD.h:823
auto Average(const std::vector< T > &vector)
Compute average element of a vector. In case of empty vector, return 0.
Definition: ExtSTD.h:615
auto Median(const std::vector< T > &vector)
Compute median value of a vector. In case of empty vector, return 0.
Definition: ExtSTD.h:657
std::string & TrimBegin(std::string &s, const std::string &t=WHITESPACES)
Trims characters from beginning of string (left).
Definition: ExtSTD.h:705
std::string ToStringWithPrecision(const T value, const int n=6)
Converts type to string with specified number of decimal digits.
Definition: ExtSTD.h:215
void MakeUppercase(char *text)
Converts text into all uppercase.
Definition: ExtSTD.h:279
void Foreach(const T &container, const F &Function)
Execute a function for every element in the container.
Definition: ExtSTD.h:485
auto FindIf(C &container, const P &Predicate)
Simplified wrapper for std::find_if.
Definition: ExtSTD.h:889
auto AllOf(const C &container, const P &Predicate) -> bool
Simplified wrapper for std::all_of.
Definition: ExtSTD.h:841
std::string ToStdString(const std::wstring &text)
Convert std::wstring to std::string.
Definition: ExtSTD.h:74
std::vector< Type > RemoveDuplicates(std::vector< Type > &vector)
Returns vector similar to input, but without duplicate elements.
Definition: ExtSTD.h:430
std::string & Trim(std::string &s, const std::string &t=WHITESPACES)
Trims from both ends of string (left & right).
Definition: ExtSTD.h:719
std::string ReplacePatternWithNumber(const std::string &base_string, const std::string &pattern, const int number)
Replaces a patter in specified string with a number.
Definition: ExtSTD.h:342
auto FindIfPtr(C &container, const P &Predicate)
Simplified wrapper for std::find_if that expects a vector of pointers as input and returns a pointer...
Definition: ExtSTD.h:909
void InsertTo(T &target, const S &source)
Insert the source container to the target container.
Definition: ExtSTD.h:687
bool Contains(const std::string &substring, const std::string &text)
Checks if the text contains substring.
Definition: ExtSTD.h:327
void ReadString(std::string &text, std::istream &str)
Reads a string from the binary stream.
Definition: ExtSTD.h:791
std::wstring ToWString(const std::string &text)
Convert single-byte std::string to std::wstring.
Definition: ExtSTD.h:68
bool IsASCII(const std::string &s)
Check std::string for non-ASCII characters.
Definition: ExtSTD.h:1011
std::vector< std::string > Parse(const std::string &text, const std::string &separators=WHITESPACES)
Parses the input text to sub strings by a given list of separators.
Definition: ExtSTD.h:735
std::string ToString(const std::vector< T > &container, const std::string &separator=", ")
Converts all values in the vector to a string and joins them using specified separator.
Definition: ExtSTD.h:233
auto MaxValue(const std::vector< T > &vector, const F &Function)
Finds a maximum value transformed by a function in a vector.
Definition: ExtSTD.h:573
auto MinValue(const std::vector< T > &vector, const F &Function)
Finds a minimal value transformed by a function in a vector.
Definition: ExtSTD.h:581
auto SelectIf(const C &container, const P &Predicate)
Copies all elements fulfilling a given condition into a new vector. Uses std::copy_if.
Definition: ExtSTD.h:928
void Read(T &val, std::istream &str)
Reads a value from the binary stream, number of bytes red is determined by a template type...
Definition: ExtSTD.h:759
Extensions to the standard libraries.
Definition: AtomicQueue.h:11
std::vector< std::pair< size_t, size_t > > FindIntervals(const std::vector< T > &vec, const P &Predicate)
Finds all intervals on the vector where a given condition holds.
Definition: ExtSTD.h:956
auto Accumulate(const std::vector< T > &vector, const T start_value)
Wrapper for std::accumulate.
Definition: ExtSTD.h:599
auto Bind1(F method, O object)
Creates binding for an object method with a single argument.
Definition: ExtSTD.h:1004
auto MinIt(const std::vector< T > &vector)
Finds a minimal value in a vector, return an iterator pointing to it.
Definition: ExtSTD.h:501
void SortAndRemoveDuplicates(std::vector< Type > &vec)
Sorts the array and removes duplicate elements.
Definition: ExtSTD.h:422
T Max(const std::vector< T > &vector)
Finds a maximum value in a vector.
Definition: ExtSTD.h:509
bool ContainsCommonElement(const std::vector< Type > &elements1, const std::set< Type > &elements2)
Check if there is at least a single common element in two vectors.
Definition: ExtSTD.h:415
auto Transformed(const C &container, F Function)
Simplified wrapper for std::transform.
Definition: ExtSTD.h:831
auto RevFindIf(C &container, const P &Predicate)
Simplified wrapper for reversed std::find_if.
Definition: ExtSTD.h:897
std::vector< IndicesType > CollectIndices(const std::vector< Type > &vector, const Type value)
Collect all indices of elements in a vector with a specified value.
Definition: ExtSTD.h:470
auto MaxIt(const std::vector< T > &vector)
Finds a maximum value in a vector, return an iterator pointing to it.
Definition: ExtSTD.h:493
std::string & TrimEnd(std::string &s, const std::string &t=WHITESPACES)
Trims characters from end of string (right).
Definition: ExtSTD.h:712
auto CountIf(const C &container, const P &Predicate)
Simplified wrapper for std::count_if.
Definition: ExtSTD.h:881
std::string BoolToString(const bool value)
Converts bool to string ("true" or "false").
Definition: ExtSTD.h:160
std::string ToStringAutoPrecision(const double value, int max_digits=6)
Converts double to string with decimal digits until max_digits are reached.
Definition: ExtSTD.h:173
std::string Uppercase(const std::string &text)
Converts text into all uppercase.
Definition: ExtSTD.h:291
bool Erase(const Type &element, std::vector< Type > &vector)
Removes an element from the vector. Returns success.
Definition: ExtSTD.h:446
std::string Replace(const std::string &text, const std::string &key_to_search, const std::string &replace_text)
Replaces all occurrences of key_to_search with replace_text in specified text.
Definition: ExtSTD.h:117
void WriteString(const std::string &text, std::ostream &str)
Writes a value to the binary stream, number of bytes written is determined by a template type...
Definition: ExtSTD.h:811
void Write(const T val, std::ostream &str)
Writes a value to the binary stream, bytes written are determined by a template type.
Definition: ExtSTD.h:775
void EmplaceBackUnique(const Type &element, std::vector< Type > &vector)
Emplace back an element if it is unique to the vector.
Definition: ExtSTD.h:695
T Min(const std::vector< T > &vector)
Finds a minimal value in a vector.
Definition: ExtSTD.h:517
auto Sum(const std::vector< T > &vector)
Sum all elements in a vector.
Definition: ExtSTD.h:607
std::string ToStringWithZeroes(const T value, const int n=6)
Converts int to string with zeros at the beginning to match specified number of digits.
Definition: ExtSTD.h:224
bool EndsWith(const std::string &ending, const std::string &text)
Checks if the text ends with a string.
Definition: ExtSTD.h:319
std::string EndWithASingleSlash(const std::string &s)
Trim the end of a string to removes forward and backward slashes (/, ) and add one single forward sla...
Definition: ExtSTD.h:727
std::pair< C, C > Partition(const C &container, const P &Predicate)
Partitions all elements based on a given condition.
Definition: ExtSTD.h:942
auto NoneOf(const C &container, const P &Predicate) -> bool
Simplified wrapper for std::none_of.
Definition: ExtSTD.h:865
void MakeLowercase(char *text)
Converts text into all lowercase.
Definition: ExtSTD.h:299
auto Count(const C &container, const T &val)
Simplified wrapper for std::count.
Definition: ExtSTD.h:873
auto AnyOfW(C &container, const P &Predicate) -> bool
Simplified wrapper for std::any_of (writable).
Definition: ExtSTD.h:857
void InsertFront(T &target, const S &source)
Insert the source vector to the front of target vector.
Definition: ExtSTD.h:680