TDMS
Time Domain Maxwell Solver
Toggle main menu visibility
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
/**
2
* @file utils.h
3
* @brief Useful miscellaneous utility functions
4
*/
5
#pragma once
6
7
#include <algorithm>
8
#include <iterator>
9
#include <string>
10
#include <vector>
11
12
/**
13
* @brief Throws a runtime error if a file is not found.
14
*
15
* @param filename The name of the file to check.
16
* @param mode The mode to try and open with.
17
*/
18
void
assert_can_open_file
(
const
char
*filename,
const
char
*mode);
19
20
/**
21
* @brief Check two strings are equal
22
*
23
* @param a The first string
24
* @param b The second string
25
* @return true if the strings are the same
26
* @return false otherwise
27
*/
28
bool
are_equal
(
const
char
*a,
const
char
*b);
29
30
/**
31
* @brief A collection of utility functions that we will be applying to
32
* std::vectors multiple times throughout the codebase.
33
*/
34
namespace
tdms_vector_utils
{
35
/** @brief Return the maximum value stored in the vector. */
36
template
<
typename
T>
37
T
max
(
const
std::vector<T> &v) {
38
return
*std::max_element(v.begin(), v.end());
39
}
40
41
/** @brief Return true if the vector has non-zero size. */
42
template
<
typename
T>
43
bool
has_elements
(
const
std::vector<T> &v) {
44
return
v.size() != 0;
45
}
46
47
/**
48
* Get the index of a particular integer in this vector. If it does not exist
49
* then return -1. Returns the first occurrence.
50
* @param v Vector to search through for the value
51
* @param value value to find
52
* @return index of the value in the vector, or -1 (if not found)
53
*/
54
template
<
typename
T>
55
int
index
(
const
std::vector<T> &v,
const
T &value) {
56
auto
found_index = std::find(v.begin(), v.end(), value);
57
// Return -1 if we didn't find the value
58
if
(found_index == v.end()) {
59
return
-1;
60
}
else
{
61
// Otherwise return the index of the occurrence of the value
62
return
std::distance(v.begin(), found_index);
63
}
64
}
65
66
/**
67
* @brief Static_casts an array of doubles to ints. ONLY INTENDED FOR CASES
68
* WHERE WE KNOW THAT THE VALUES STORED AS DOUBLES ARE INTS.
69
* @details Usage cases are restricted to when data is read from MATLAB .mat
70
* files, which by default save all values as floats, however we are expecting
71
* to receive integer values (cell indices, sizes, etc).
72
* @param ints_that_are_doubles Vector of doubles that actually contain integer
73
* values
74
* @return std::vector<int> Converted values
75
*/
76
std::vector<int>
77
to_vector_int
(
const
std::vector<double> &ints_that_are_doubles);
78
79
}
// namespace tdms_vector_utils
tdms_vector_utils
A collection of utility functions that we will be applying to std::vectors multiple times throughout ...
Definition
utils.h:34
tdms_vector_utils::has_elements
bool has_elements(const std::vector< T > &v)
Return true if the vector has non-zero size.
Definition
utils.h:43
tdms_vector_utils::max
T max(const std::vector< T > &v)
Return the maximum value stored in the vector.
Definition
utils.h:37
tdms_vector_utils::to_vector_int
std::vector< int > to_vector_int(const std::vector< double > &ints_that_are_doubles)
Static_casts an array of doubles to ints. ONLY INTENDED FOR CASES WHERE WE KNOW THAT THE VALUES STORE...
tdms_vector_utils::index
int index(const std::vector< T > &v, const T &value)
Definition
utils.h:55
are_equal
bool are_equal(const char *a, const char *b)
Check two strings are equal.
Definition
utils.cpp:21
assert_can_open_file
void assert_can_open_file(const char *filename, const char *mode)
Throws a runtime error if a file is not found.
Definition
utils.cpp:10
tdms
include
utils.h
Generated by
1.17.0