diff --git a/packages/seacas/libraries/aprepro_lib/apr_builtin.cc b/packages/seacas/libraries/aprepro_lib/apr_builtin.cc index b53c594e7d..f02ea1523c 100644 --- a/packages/seacas/libraries/aprepro_lib/apr_builtin.cc +++ b/packages/seacas/libraries/aprepro_lib/apr_builtin.cc @@ -6,6 +6,7 @@ #include "apr_builtin.h" #include "apr_symrec.h" +#include "enumerate.h" #include #include @@ -84,8 +85,8 @@ namespace SEAMS { extern SEAMS::Aprepro *aprepro; -#define d2r(x) ((x)*PI / 180.) -#define r2d(x) ((x)*180. / PI) +#define d2r(x) ((x) * PI / 180.) +#define r2d(x) ((x) * 180. / PI) #ifndef max #define max(x, y) (x) > (y) ? (x) : (y) @@ -656,8 +657,8 @@ namespace SEAMS { { const auto &tokens = get_tokenized_strings(string, delm); std::string sword{word}; - for (size_t i = 0; i < tokens.size(); i++) { - if (tokens[i] == sword) { + for (auto [i, token] : enumerate(tokens)) { + if (token == sword) { return i + 1; } } diff --git a/packages/seacas/libraries/aprepro_lib/apr_init.cc b/packages/seacas/libraries/aprepro_lib/apr_init.cc index f2e742c829..d844d86994 100644 --- a/packages/seacas/libraries/aprepro_lib/apr_init.cc +++ b/packages/seacas/libraries/aprepro_lib/apr_init.cc @@ -14,12 +14,12 @@ #include "apr_tokenize.h" #include "aprepro.h" // for symrec, Aprepro, etc #include "init_structs.h" // for array_a_init, array_c_init, etc -#include // for string +#include +#include // for string namespace SEAMS { - init arith_0_fncts[] = { - {"seconds", do_time, "seconds()", "Seconds since epoch (useful for srand())."}, - {nullptr, nullptr, nullptr, nullptr}}; + std::array arith_0_fncts = { + {"seconds", do_time, "seconds()", "Seconds since epoch (useful for srand())."}}; init_d arith_fncts[] = { {"abs", do_fabs, "abs(x)", "Absolute value of x. |x|."}, diff --git a/packages/seacas/libraries/aprepro_lib/enumerate.h b/packages/seacas/libraries/aprepro_lib/enumerate.h new file mode 100644 index 0000000000..c523adea8f --- /dev/null +++ b/packages/seacas/libraries/aprepro_lib/enumerate.h @@ -0,0 +1,34 @@ +// https://www.reedbeta.com/blog/python-like-enumerate-in-cpp17/ +// std::vector things; +// ... +// for (auto [i, thing] : enumerate(things)) +// { +// .. `i` gets the index and `thing` gets the Thing in each iteration +// } + +#include + +template ())), + typename = decltype(std::end(std::declval()))> +constexpr auto enumerate(T &&iterable) +{ + struct iterator + { + size_t i; + TIter iter; + bool operator!=(const iterator &other) const { return iter != other.iter; } + void operator++() + { + ++i; + ++iter; + } + auto operator*() const { return std::tie(i, *iter); } + }; + struct iterable_wrapper + { + T iterable; + auto begin() { return iterator{0, std::begin(iterable)}; } + auto end() { return iterator{0, std::end(iterable)}; } + }; + return iterable_wrapper{std::forward(iterable)}; +} diff --git a/packages/seacas/libraries/ioss/src/Ioss_DatabaseIO.C b/packages/seacas/libraries/ioss/src/Ioss_DatabaseIO.C index 0ea82a473a..5606d5bf66 100644 --- a/packages/seacas/libraries/ioss/src/Ioss_DatabaseIO.C +++ b/packages/seacas/libraries/ioss/src/Ioss_DatabaseIO.C @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include