22using namespace std::chrono;
38inline size_t maxIndex(
const std::vector<T> &data) {
41 std::vector<size_t> idx(data.size());
42 std::iota(idx.begin(), idx.end(), 0);
44 std::stable_sort(idx.begin(), idx.end(),
45 [&data](
size_t i1,
size_t i2) {return data[i1] > data[i2];});
56inline size_t minIndex(
const std::vector<T> &data) {
59 std::vector<size_t> idx(data.size());
60 std::iota(idx.begin(), idx.end(), 0);
62 std::stable_sort(idx.begin(), idx.end(),
63 [&data](
size_t i1,
size_t i2) {return data[i1] < data[i2];});
74inline T
max(
const std::vector<T> &data) {
84inline T
min(
const std::vector<T> &data) {
118 size_t data_start,
size_t data_end) {
120 if (data.size() == 0) {
121 std::cerr <<
"Error: maxIndex() is called with data of size " << data.size()
126 if (data_end == 0 or data_end > data.size()) {
127 std::cerr <<
"Error: maxIndex() data_end = " << data_end
128 <<
" is not valid for the data of size " << data.size()
133 if (data_start > data.size() - 1) {
134 std::cerr <<
"Error: maxIndex() data_start = " << data_start
135 <<
" is not valid for the data of size " << data.size()
141 std::vector<size_t> idx(data_end - data_start);
142 std::iota(idx.begin(), idx.end() , 0);
144 std::stable_sort(idx.begin(), idx.end(),
145 [&data, &data_start](
size_t i1,
size_t i2)
146 {return data[i1 + data_start] > data[i2 + data_start];});
148 return idx[0] + data_start;
157 std::vector<double> length_data(data.size());
158 for (
size_t i = 0; i < data.size(); i++)
159 length_data[i] = data[i].length();
170 std::vector<double> length_data(data.size());
171 for (
size_t i = 0; i < data.size(); i++)
172 length_data[i] = data[i].length();
182inline double maxLength(
const std::vector<util::Point> &data) {
183 std::vector<double> length_data(data.size());
184 for (
size_t i = 0; i < data.size(); i++)
185 length_data[i] = data[i].length();
195inline double minLength(
const std::vector<util::Point> &data) {
196 std::vector<double> length_data(data.size());
197 for (
size_t i = 0; i < data.size(); i++)
198 length_data[i] = data[i].length();
209 std::vector<double> length_data(data.size());
210 for (
size_t i = 0; i < data.size(); i++)
211 length_data[i] = data[i].length();
214 return {length_data[i], i};
223 std::vector<double> length_data(data.size());
224 for (
size_t i = 0; i < data.size(); i++)
225 length_data[i] = data[i].length();
228 return {length_data[i], i};
238inline T
add(
const std::vector<T> &data) {
239 return std::reduce(data.begin(), data.end());
249inline bool isFree(
const int &i,
const unsigned int &dof) {
250 return !(i >> dof & 1UL);
254inline bool isFree(
const uint8_t &i,
const unsigned int &dof) {
255 return !(i >> dof & 1UL);
265inline bool isInList(
const T &i,
const std::vector<T> &list) {
266 for (
const auto &j : list)
279inline bool isTagInList(
const std::string &tag,
const std::vector<std::string> &tags) {
289inline void addToList(
const T &i, std::vector<T> &list) {
290 for (
const auto &j : list)
294 list.emplace_back(i);
304inline float timeDiff(std::chrono::steady_clock::time_point begin,
305 std::chrono::steady_clock::time_point end, std::string unit =
"microseconds") {
306 if (unit ==
"microseconds")
307 return std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
308 else if (unit ==
"milliseconds")
309 return std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
310 else if (unit ==
"seconds")
311 return std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();
313 std::cerr <<
"Unit = " << unit <<
" not valid.\n";
324template <
typename T_out>
325inline T_out
getKeyData(std::string key, std::map<std::string, T_out> &data_map,
bool issue_err =
false) {
328 if (data_map.find(key) == data_map.end()) {
329 std::cerr <<
"Error: key = " << key <<
" does not exist in data map.\n";
334 return data_map[key];
343template <
typename T_out>
344inline void appendKeyData(std::string key, T_out data, std::map<std::string, T_out> &data_map,
bool issue_err =
false) {
346 if (data_map.find(key) == data_map.end()) {
348 std::cerr <<
"Error: key = " << key <<
" does not exist in data map.\n";
352 data_map[key] = data;
356 data_map[key] = data_map[key] + data;
365template <
typename T_out>
366inline void setKeyData(std::string key, T_out data, std::map<std::string, T_out> &data_map,
bool issue_err =
false) {
368 if (data_map.find(key) == data_map.end()) {
370 std::cerr <<
"Error: key = " << key <<
" does not exist in data map.\n";
375 data_map[key] = data;
std::pair< double, size_t > maxLengthAndMaxLengthIndex(const std::vector< util::Point > &data)
Returns the maximum length of point and index from list of points.
void appendKeyData(std::string key, T_out data, std::map< std::string, T_out > &data_map, bool issue_err=false)
Append value to data associated with key.
T_out getKeyData(std::string key, std::map< std::string, T_out > &data_map, bool issue_err=false)
Get data for a key.
size_t maxLengthIndex(const std::vector< util::Point > &data)
Returns the index that has maximum length of point from list of points.
std::pair< size_t, T > minAndMinIndex(const std::vector< T > &data)
Returns the minimum and index of minimum from list of data.
double minLength(const std::vector< util::Point > &data)
Returns the minimum length of point from list of points.
T max(const std::vector< T > &data)
Returns the maximum from list of data.
size_t minIndex(const std::vector< T > &data)
Returns the index corresponding to minimum from list of data.
T min(const std::vector< T > &data)
Returns the minimim from list of data.
bool isInList(const T &i, const std::vector< T > &list)
Find if data is in the list.
size_t minLengthIndex(const std::vector< util::Point > &data)
Returns the index that has minimum length of point from list of points.
T add(const std::vector< T > &data)
Returns the sum of data.
float timeDiff(std::chrono::steady_clock::time_point begin, std::chrono::steady_clock::time_point end, std::string unit="microseconds")
Returns difference between two times.
bool isTagInList(const std::string &tag, const std::vector< std::string > &tags)
Returns true if tag is found in the list of tags.
std::pair< size_t, T > maxAndMaxIndex(const std::vector< T > &data)
Returns the maximum and index of maximum from list of data.
bool isFree(const int &i, const unsigned int &dof)
Returns true if degree of freedom is free.
void addToList(const T &i, std::vector< T > &list)
Add element to the list.
std::pair< double, size_t > minLengthAndMinLengthIndex(const std::vector< util::Point > &data)
Returns the minimum length of point and index from list of points.
void setKeyData(std::string key, T_out data, std::map< std::string, T_out > &data_map, bool issue_err=false)
Set value to data associated with key.
size_t maxIndex(const std::vector< T > &data)
Returns the index corresponding to maximum from list of data.
double maxLength(const std::vector< util::Point > &data)
Returns the maximum length of point from list of points.
Collection of methods useful in simulation.