Skip to content

Commit f50f6c9

Browse files
committed
Add helper function for lower index in array
1 parent 0a58e4a commit f50f6c9

2 files changed

Lines changed: 41 additions & 16 deletions

File tree

include/hydroc/helper.h

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,48 @@
33

44
#pragma once
55

6-
#include <string>
7-
#include <iostream>
6+
#include <Eigen/Dense> // Need for the container function
87
#include <fstream>
9-
#include <Eigen/Dense> // Need for the container function
10-
8+
#include <iostream>
9+
#include <string>
1110

1211
#ifndef M_PI
1312
#define M_PI 3.14159265358979323846
1413
#endif
14+
15+
/**@brief Returns last index of vector element below value.
16+
*
17+
* @param value Input value
18+
* @param ticks Array of ticks from which to find lower-bound index (assuming ascending order)
19+
*
20+
*/
21+
size_t get_lower_index(double value, const std::vector<double>& ticks);
22+
1523
/**@brief Base namespace for HydroChrono library
16-
*
17-
*/
24+
*
25+
*/
1826
namespace hydroc {
1927

2028
/**@brief Set initial environment
21-
*
29+
*
2230
* Use command line argument or env variables to set s
2331
* ome static data at initialization.
24-
*
32+
*
2533
* Set the main data directory ..
26-
*
34+
*
2735
* @param argc number of argument (same as for main function)
28-
* @param argv arguments of main function
36+
* @param argv arguments of main function
2937
* @return 1 on error 0 else
30-
*/
38+
*/
3139
int SetInitialEnvironment(int argc, char* argv[]) noexcept;
3240

3341
/**@brief Get base name of data directory
34-
*
42+
*
3543
* @return the string containing the path in standard format
36-
*/
44+
*/
3745
std::string getDataDir() noexcept;
3846
template <typename T>
39-
void WriteDataToFile(const std::vector<T>& data, const std::string& filename){
47+
void WriteDataToFile(const std::vector<T>& data, const std::string& filename) {
4048
std::ofstream outFile(filename);
4149
if (outFile.is_open()) {
4250
for (const auto& item : data) {
@@ -66,7 +74,8 @@ void WriteContainerToFile(const Container& container, const std::string& file_na
6674
* @param file_name file to write container to
6775
*/
6876
template <>
69-
void inline WriteContainerToFile<std::vector<double>>(const std::vector<double>& container, const std::string& file_name) {
77+
void inline WriteContainerToFile<std::vector<double>>(const std::vector<double>& container,
78+
const std::string& file_name) {
7079
std::ofstream output_file(file_name);
7180

7281
if (!output_file) {
@@ -103,6 +112,6 @@ void inline WriteContainerToFile<Eigen::VectorXd>(const Eigen::VectorXd& contain
103112
output_file.close();
104113
};
105114

106-
} // end namespace hydroc
115+
} // end namespace hydroc
107116

108117
#endif

src/helper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
#include <memory>
66
#include <vector>
77

8+
size_t get_lower_index(double value, const std::vector<double>& ticks) {
9+
auto it = std::upper_bound(ticks.begin(), ticks.end(), value);
10+
// get nearest-below index
11+
size_t idx = it - ticks.begin() - 1;
12+
// remove one if equal to value
13+
if (ticks[idx] == value) {
14+
idx -= 1;
15+
}
16+
if (idx <= 0 || idx >= ticks.size() - 1) {
17+
throw std::runtime_error("Could not find index for value " + std::to_string(value) + " in array with bounds (" +
18+
std::to_string(ticks.front()) + ", " + std::to_string(ticks.back()) + ").");
19+
}
20+
// return index
21+
return idx;
22+
}
23+
824
using std::filesystem::path;
925

1026
static path DATADIR{};

0 commit comments

Comments
 (0)