ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Helper.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cmath>
11 #include <cstdlib>
12 #include <functional>
13 #include <memory>
14 #include <random>
15 #include <stdexcept>
16 #include <string>
17 #include <tuple>
18 #include <vector>
19 
20 #include "CVCoreLib.h"
21 
22 namespace cloudViewer {
23 namespace utility {
24 
32 
33 template <typename TT>
34 struct hash_tuple {
35  size_t operator()(TT const& tt) const { return std::hash<TT>()(tt); }
36 };
37 
38 namespace {
39 
40 template <class T>
41 inline void hash_combine(std::size_t& hash_seed, T const& v) {
42  hash_seed ^= std::hash<T>()(v) + 0x9e3779b9 + (hash_seed << 6) +
43  (hash_seed >> 2);
44 }
45 
46 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
47 struct HashValueImpl {
48  static void apply(size_t& seed, Tuple const& tuple) {
49  HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
50  hash_combine(seed, std::get<Index>(tuple));
51  }
52 };
53 
54 template <class Tuple>
55 struct HashValueImpl<Tuple, 0> {
56  static void apply(size_t& seed, Tuple const& tuple) {
57  hash_combine(seed, std::get<0>(tuple));
58  }
59 };
60 
61 } // unnamed namespace
62 
63 template <typename... TT>
64 struct hash_tuple<std::tuple<TT...>> {
65  size_t operator()(std::tuple<TT...> const& tt) const {
66  size_t hash_seed = 0;
67  HashValueImpl<std::tuple<TT...>>::apply(hash_seed, tt);
68  return hash_seed;
69  }
70 };
71 
72 template <typename T>
73 struct hash_eigen {
74  std::size_t operator()(T const& matrix) const {
75  size_t hash_seed = 0;
76  for (int i = 0; i < (int)matrix.size(); i++) {
77  auto elem = *(matrix.data() + i);
78  hash_seed ^= std::hash<typename T::Scalar>()(elem) + 0x9e3779b9 +
79  (hash_seed << 6) + (hash_seed >> 2);
80  }
81  return hash_seed;
82  }
83 };
84 
85 // Hash function for enum class for C++ standard less than C++14
86 // https://stackoverflow.com/a/24847480/1255535
88  template <typename T>
89  std::size_t operator()(T t) const {
90  return static_cast<std::size_t>(t);
91  }
92 };
93 
94 // Format string by replacing embedded format specifiers with their respective
95 // values, see `printf` for more details. This is a modified implementation
96 // of Google's BSD-licensed StringPrintf function.
97 std::string CV_CORE_LIB_API StringPrintf(const char* format, ...);
98 
99 // Replace all occurrences of `old_str` with `new_str` in the given string.
100 std::string CV_CORE_LIB_API StringReplace(const std::string& str,
101  const std::string& old_str,
102  const std::string& new_str);
103 
107 bool CV_CORE_LIB_API StringContains(const std::string& src,
108  const std::string& dst);
109 
110 std::string CV_CORE_LIB_API StringReplaceFirst(const std::string& str,
111  const std::string& old_str,
112  const std::string& new_str);
113 
114 std::string CV_CORE_LIB_API StringReplaceLast(const std::string& str,
115  const std::string& old_str,
116  const std::string& new_str);
117 
118 // Check whether a string starts with a certain prefix.
119 bool CV_CORE_LIB_API StringStartsWith(const std::string& str,
120  const std::string& prefix);
121 
122 // Check whether a string ends with a certain postfix.
123 bool CV_CORE_LIB_API StringEndsWith(const std::string& str,
124  const std::string& postfix);
125 
126 std::string CV_CORE_LIB_API JoinStrings(const std::vector<std::string>& strs,
127  const std::string& delimiter = ", ");
128 
131 std::vector<std::string> CV_CORE_LIB_API
132 StringSplit(const std::string& str,
133  const std::string& delimiters = " ",
134  bool trim_empty_str = true);
135 
138 void CV_CORE_LIB_API SplitString(std::vector<std::string>& tokens,
139  const std::string& str,
140  const std::string& delimiters = " ",
141  bool trim_empty_str = true);
142 
145 std::vector<std::string> CV_CORE_LIB_API
146 SplitString(const std::string& str,
147  const std::string& delimiters = " ",
148  bool trim_empty_str = true);
149 
153 size_t CV_CORE_LIB_API WordLength(const std::string& doc,
154  size_t start_pos,
155  const std::string& valid_chars = "_");
156 
157 CV_CORE_LIB_API std::string& LeftStripString(
158  std::string& str, const std::string& chars = "\t\n\v\f\r ");
159 
160 CV_CORE_LIB_API std::string& RightStripString(
161  std::string& str, const std::string& chars = "\t\n\v\f\r ");
162 
165 CV_CORE_LIB_API std::string& StripString(
166  std::string& str, const std::string& chars = "\t\n\v\f\r ");
167 
169 std::string CV_CORE_LIB_API ToLower(const std::string& s);
170 
172 std::string CV_CORE_LIB_API ToUpper(const std::string& s);
173 
175 template <typename... Args>
176 inline std::string FormatString(const std::string& format, Args... args) {
177  int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) +
178  1; // Extra space for '\0'
179  if (size_s <= 0) {
180  throw std::runtime_error("Error during formatting.");
181  }
182  auto size = static_cast<size_t>(size_s);
183  auto buf = std::make_unique<char[]>(size);
184  std::snprintf(buf.get(), size, format.c_str(), args...);
185  return std::string(buf.get(),
186  buf.get() + size - 1); // We don't want the '\0' inside
187 };
188 
190 template <typename... Args>
191 inline std::string FastFormatString(const std::string& format, Args... args) {
192 #ifdef _WIN32
193  return FormatString(format, args...);
194 #else
195  char* buffer = nullptr;
196  int size_s = asprintf(&buffer, format.c_str(), args...);
197  if (size_s == -1) {
198  throw std::runtime_error("Error during formatting.");
199  }
200  auto ret = std::string(buffer,
201  buffer + size_s); // no + 1 since we ignore the \0
202  std::free(buffer); // asprintf calls malloc
203  return ret;
204 #endif // _WIN32
205 };
206 
207 void CV_CORE_LIB_API Sleep(int milliseconds);
208 
210 inline int CV_CORE_LIB_API DivUp(int x, int y) {
211  div_t tmp = std::div(x, y);
212  return tmp.quot + (tmp.rem != 0 ? 1 : 0);
213 }
214 
220 public:
222  const int min,
223  const int max,
224  std::mt19937::result_type seed = std::random_device{}())
225  : distribution_(min, max), generator_(seed) {}
226  int operator()() { return distribution_(generator_); }
227 
228 protected:
229  std::uniform_int_distribution<int> distribution_;
230  std::mt19937 generator_;
231 };
232 
235 
236 } // namespace utility
237 } // namespace cloudViewer
#define CV_CORE_LIB_API
Definition: CVCoreLibWin.h:15
filament::Texture::InternalFormat format
int size
Draw pseudo-random integers bounded by min and max (inclusive) from a uniform distribution.
Definition: Helper.h:219
std::uniform_int_distribution< int > distribution_
Definition: Helper.h:229
UniformRandIntGenerator(const int min, const int max, std::mt19937::result_type seed=std::random_device{}())
Definition: Helper.h:221
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars="_")
Definition: Helper.cpp:257
std::string FormatString(const std::string &format, Args... args)
Format string.
Definition: Helper.h:176
std::string StringPrintf(const char *format,...)
Definition: Helper.cpp:110
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
Definition: Helper.cpp:197
std::string & StripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
Definition: Helper.cpp:238
bool StringEndsWith(const std::string &str, const std::string &postfix)
Definition: Helper.cpp:163
std::string ToLower(const std::string &s)
Convert string to the lower case.
Definition: Helper.cpp:242
std::string JoinStrings(const std::vector< std::string > &strs, const std::string &delimiter=", ")
Definition: Helper.cpp:168
int DivUp(int x, int y)
Computes the quotient of x/y with rounding up.
Definition: Helper.h:210
std::string & RightStripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
Definition: Helper.cpp:233
std::string StringReplaceLast(const std::string &str, const std::string &old_str, const std::string &new_str)
Definition: Helper.cpp:144
bool StringContains(const std::string &src, const std::string &dst)
Definition: Helper.cpp:154
void Sleep(int milliseconds)
Definition: Helper.cpp:278
std::string ToUpper(const std::string &s)
Convert string to the upper case.
Definition: Helper.cpp:249
std::string GetCurrentTimeStamp()
Returns current time stamp.
Definition: Helper.cpp:286
std::string FastFormatString(const std::string &format, Args... args)
Format string fast (Unix / BSD Only)
Definition: Helper.h:191
std::string & LeftStripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
Definition: Helper.cpp:228
std::vector< std::string > StringSplit(const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
Definition: Helper.cpp:180
std::string StringReplace(const std::string &str, const std::string &old_str, const std::string &new_str)
Definition: Helper.cpp:119
std::string StringReplaceFirst(const std::string &str, const std::string &old_str, const std::string &new_str)
Definition: Helper.cpp:134
bool StringStartsWith(const std::string &str, const std::string &prefix)
Definition: Helper.cpp:158
Generic file read and write utility for python interface.
Definition: Eigen.h:85
std::size_t operator()(T const &matrix) const
Definition: Helper.h:74
std::size_t operator()(T t) const
Definition: Helper.h:89
size_t operator()(std::tuple< TT... > const &tt) const
Definition: Helper.h:65
size_t operator()(TT const &tt) const
Definition: Helper.h:35