samedi 5 décembre 2020

How to efficiently calculate a function on a vector of variants?

I have a vector of variants that can store either uint64_t and double types, and I want to calculate the median over them.

At any given time, the vector is composed entirely of uint64_t's or double's

The solution I have so far as outlined below works but it seems redundant in terms of code, and I want to know if there is a better way to achieve this result.

typedef std::variant<uint64_t, double> data_variant;

data_variant calculate_median(const std::vector<data_variant>& data) {
    auto index = data.back().index();

    // uint64_t
    if (index == 0) {
      std::vector<uint64_t> vec;
      for (auto& i: data) {
        vec.emplace_back(std::get<uint64_t>(i));
      }

      const auto median_it = vec.begin() + vec.size() / 2;
      std::nth_element(vec.begin(), median_it , vec.end());
      return *median_it;
    // double
    } else if (index == 1) {
      std::vector<double> vec;
      for (auto& i: data) {
        vec.emplace_back(std::get<double>(i));
      }

      const auto median_it = vec.begin() + vec.size() / 2;
      std::nth_element(vec.begin(), median_it , vec.end());
      return *median_it;
    }
  }

Aucun commentaire:

Enregistrer un commentaire