Skip to content

Commit

Permalink
Clean up enumerate useage
Browse files Browse the repository at this point in the history
  • Loading branch information
gsjaardema committed Sep 28, 2023
1 parent 49a1bf3 commit 8399813
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
9 changes: 5 additions & 4 deletions packages/seacas/libraries/aprepro_lib/apr_builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "apr_builtin.h"
#include "apr_symrec.h"
#include "enumerate.h"

#include <cctype>
#include <cerrno>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/seacas/libraries/aprepro_lib/apr_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string> // for string
#include <array>
#include <string> // for string

namespace SEAMS {
init arith_0_fncts[] = {
{"seconds", do_time, "seconds()", "Seconds since epoch (useful for srand())."},
{nullptr, nullptr, nullptr, nullptr}};
std::array<init, 1> 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|."},
Expand Down
34 changes: 34 additions & 0 deletions packages/seacas/libraries/aprepro_lib/enumerate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// https://www.reedbeta.com/blog/python-like-enumerate-in-cpp17/
// std::vector<Thing> things;
// ...
// for (auto [i, thing] : enumerate(things))
// {
// .. `i` gets the index and `thing` gets the Thing in each iteration
// }

#include <tuple>

template <typename T, typename TIter = decltype(std::begin(std::declval<T>())),
typename = decltype(std::end(std::declval<T>()))>
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<T>(iterable)};
}
2 changes: 1 addition & 1 deletion packages/seacas/libraries/ioss/src/Ioss_DatabaseIO.C
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <Ioss_BoundingBox.h>
#include <Ioss_CodeTypes.h>
#include <Ioss_ElementTopology.h>
#include <Ioss_Enumberate.h>
#include <Ioss_Enumerate.h>
#include <Ioss_FileInfo.h>
#include <Ioss_ParallelUtils.h>
#include <Ioss_Sort.h>
Expand Down

0 comments on commit 8399813

Please sign in to comment.