|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// See the LICENSE file for license and copyright information |
| 4 | +// SPDX-License-Identifier: NCSA AND BSD-3-Clause |
| 5 | +// |
| 6 | +//===----------------------------------------------------------------------===// |
| 7 | +/// |
| 8 | +/// @file |
| 9 | +/// Declare and implement the IteratorAdaptor |
| 10 | +/// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +#ifndef GRTESTUTILS_ITERATOR_ADAPTOR_HPP |
| 13 | +#define GRTESTUTILS_ITERATOR_ADAPTOR_HPP |
| 14 | + |
| 15 | +#include <iterator> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +#include "grackle.h" |
| 19 | +#include "preset.hpp" |
| 20 | + |
| 21 | +namespace grtest { |
| 22 | + |
| 23 | +/// the standard value-type that an IteratorAdaptor instantiation refers to |
| 24 | +struct NameIdPair { |
| 25 | + std::string name; |
| 26 | + long long id; |
| 27 | +}; |
| 28 | + |
| 29 | +/// implements a C++ style InputIterator by adapting a simple Plugin type |
| 30 | +/// that wraps a set of Grackle functions |
| 31 | +/// |
| 32 | +/// This is useful for making use of C++ standard library algorithms and |
| 33 | +/// (arguably more importantly) making use of range-based for-loops |
| 34 | +template <class Plugin> |
| 35 | +class IteratorAdaptor { |
| 36 | + unsigned long long counter_; |
| 37 | + unsigned long long n_rates_; |
| 38 | + Plugin plugin_; |
| 39 | + NameIdPair current_pair_; |
| 40 | + |
| 41 | + /// Updates current_pair_ and returns `*this` |
| 42 | + IteratorAdaptor& update_pair_and_ret_(unsigned long long current_count) { |
| 43 | + if (current_count < this->n_rates_) { |
| 44 | + this->current_pair_ = this->plugin_(current_count); |
| 45 | + } |
| 46 | + return *this; |
| 47 | + } |
| 48 | + |
| 49 | +public: |
| 50 | + using iterator_category = std::input_iterator_tag; |
| 51 | + using value_type = NameIdPair; |
| 52 | + using difference_type = std::ptrdiff_t; |
| 53 | + using pointer = const NameIdPair*; |
| 54 | + using reference = const NameIdPair; |
| 55 | + |
| 56 | + /// construct a new instance |
| 57 | + IteratorAdaptor(unsigned long long counter, unsigned long long n_rates, |
| 58 | + Plugin plugin) |
| 59 | + : counter_(counter), n_rates_(n_rates), plugin_(plugin) { |
| 60 | + update_pair_and_ret_(counter); |
| 61 | + } |
| 62 | + |
| 63 | + /// implements the equality operation |
| 64 | + bool operator==(const IteratorAdaptor& other) const { |
| 65 | + return (counter_ == other.counter_) && (plugin_ == other.plugin_); |
| 66 | + } |
| 67 | + |
| 68 | + /// implements the inequality operation |
| 69 | + bool operator!=(const IteratorAdaptor& other) const { |
| 70 | + return !(*this == other); |
| 71 | + } |
| 72 | + |
| 73 | + /// implements the dereference operation |
| 74 | + reference operator*() const { return current_pair_; } |
| 75 | + |
| 76 | + /// implements the prefix increment operation |
| 77 | + /// |
| 78 | + /// This effectively implements `++x`, which increments the value of `x` |
| 79 | + /// before determining the returned value. In other words, `++x` returns the |
| 80 | + /// value of `x` from **after** after the increment |
| 81 | + IteratorAdaptor& operator++() { return update_pair_and_ret_(++counter_); } |
| 82 | + |
| 83 | + /// implements the prefix increment operation |
| 84 | + /// |
| 85 | + /// This effectively implements `x++`, which increments the value of `x` |
| 86 | + /// after determining the returned value. In other words, `x++` returns the |
| 87 | + /// value of `x` from **before** the increment |
| 88 | + IteratorAdaptor operator++(int) { |
| 89 | + IteratorAdaptor ret = *this; |
| 90 | + ++(*this); |
| 91 | + return ret; |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +// Now lets use this machinery to implement logic iterating over the names |
| 96 | +// accessible through the ratequery api |
| 97 | + |
| 98 | +struct RateQueryPlugin { |
| 99 | + chemistry_data_storage* my_rates; |
| 100 | + |
| 101 | + NameIdPair operator()(unsigned long long i) const { |
| 102 | + grunstable_rateid_type tmp; |
| 103 | + const char* name = grunstable_ith_rate(i, &tmp); |
| 104 | + return NameIdPair{name, tmp}; |
| 105 | + } |
| 106 | + |
| 107 | + bool operator==(const RateQueryPlugin& other) const { |
| 108 | + return my_rates == other.my_rates; |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +// will be implemented (in a much more robust manner) in the near future |
| 113 | +inline unsigned long long grunstable_ratequery_nrates( |
| 114 | + const chemistry_data_storage* my_rates) { |
| 115 | + // current implementation is stupid! (in future, will use my_rates) |
| 116 | + unsigned long long i = 0; |
| 117 | + while (nullptr != grunstable_ith_rate(i, nullptr)) { |
| 118 | + i++; |
| 119 | + } |
| 120 | + return i; |
| 121 | +} |
| 122 | + |
| 123 | +/// used for creating the iterator and within range-based for-loops |
| 124 | +class RateQueryRange { |
| 125 | + RateQueryPlugin plugin_; |
| 126 | + using iterator = IteratorAdaptor<RateQueryPlugin>; |
| 127 | + long long n_rates_; |
| 128 | + |
| 129 | +public: |
| 130 | + explicit RateQueryRange(grtest::GrackleCtxPack& pack) |
| 131 | + : plugin_(RateQueryPlugin{pack.my_rates()}), |
| 132 | + n_rates_(grunstable_ratequery_nrates(pack.my_rates())) {} |
| 133 | + |
| 134 | + iterator begin() { return iterator(0, n_rates_, plugin_); } |
| 135 | + iterator end() { return iterator(n_rates_, n_rates_, plugin_); } |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace grtest |
| 139 | + |
| 140 | +#endif // GRTESTUTILS_ITERATOR_ADAPTOR_HPP |
0 commit comments