text:
/*!
 * @file     mppi_new.cpp
 * @author   Giuseppe Rizzi
 * @date     01.07.2020
 * @version  1.0
 * @brief    description
 */

#include "mppi/controller/mppi.h"

#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <vector>

#include <Eigen/Core>

#include "mppi/controller/rollout.h"
#include "mppi/cost/cost_base.h"
#include "mppi/dynamics/dynamics_base.h"
#include "mppi/experts/expert.h"
#include "mppi/filters/savgol_filter.h"
#include "mppi/solver_config.h"
#include "mppi/tree/tree_manager.h"
#include "mppi/utils/logging.h"

namespace mppi {

PathIntegral::PathIntegral(dynamics_ptr dynamics, cost_ptr cost,
                           const SolverConfig& config, sampler_ptr sampler,
                           renderer_ptr renderer)
    : cost_(std::move(cost)),
      dynamics_(std::move(dynamics)),
      config_(config),
      sampler_(std::move(sampler)),
      renderer_(std::move(renderer)),
      expert_(config_, dynamics_),
      tree_manager_(dynamics_, cost_, sampler_, config_, &expert_) {
  init_data();
  init_filter();
  if (!config_.use_tree_search) {
    init_threading();
  }

  if (config_.use_tree_search) {
    init_tree_manager_dynamics();
  }

  if (config_.display_update_freq) {
    start_time_ = std::chrono::high_resolution_clock::now();
  }
}

void PathIntegral::init_data() {
  steps_ = std::floor(config_.horizon / config_.step_size);
  nx_ = dynamics_->get_state_dimension();
  nu_ = dynamics_->get_input_dimension();

  opt_roll_ = Rollout(steps_, nu_, nx_);
  opt_roll_cache_ = Rollout(steps_, nu_, nx_);

  rollouts_.resize(config_.rollouts, Rollout(steps_, nu_, nx_));
  rollouts_cost_ = Eigen::ArrayXd::Zero(config_.rollouts);

  omega = Eigen::ArrayXd::Zero(config_.rollouts);
  cached_rollouts_ = std::ceil(config_.caching_factor * config_.rollouts);

  momentum_.resize(steps_, input_t::Zero(nu_));

  if (sampler_ == nullptr) {
    log_info("No sampler provided, using gaussian sampler");
    if (config_.input_variance.size() != nu_)
      throw std::runtime_error(
          "The input variance size is different from the input size");

    sampler_ = std::make_shared<mppi::GaussianSampler>(nu_);
    sampler_->set_covariance(config_.input_variance);
  }

  if (config_.logging) {
    data_.config = config_;
  }

  if (config_.bound_input) {
    if (config_.u_min.size() != nu_) {
      std::stringstream error;
      error << "Bounding input and min constraint size " << config_.u_min.size()
            << " != " << nu_;
      throw std::runtime_error(error.str());
    }
    if (config_.u_max.size() != nu_) {
      std::stringstream error;
      error << "Bounding input and max constraint size " << config_.u_max.size()
            << " != " << nu_;
      throw std::runtime_error(error.str());
    }
  }
  step_count_ = 0;
  observation_set_ = false;
  reference_set_ = false;
}

void PathIntegral::init_filter() {
  switch (config_.filter_type) {
    case InputFilterType::NONE: {
      break;
    }
    case InputFilterType::SAVITZKY_GOLEY: {
      filter_ = SavGolFilter(steps_, nu_, config_.filters_window,
                             config_.filters_order);
      break;
    }
    default: {
      throw std::runtime_error("Unrecognized filter type.");
    }
  }
}

void PathIntegral::init_threading() {
  if (config_.threads > 1) {
    std::cout << "Using multithreading. Number of threads: " << config_.threads
              << std::endl;
    pool_ = std::make_unique<ThreadPool>(config_.threads);
    futures_.resize(config_.threads);

    for (size_t i = 0; i < config_.threads; i++) {
      dynamics_v_.push_back(dynamics_->create());
      cost_v_.push_back(cost_->create());
    }
  }
}

void PathIntegral::init_tree_manager_dynamics() {
  for (size_t i = 0; i < config_.rollouts; i++) {
    tree_dynamics_v_.push_back(dynamics_->create());
  }
}

void PathIntegral::update_policy() {
  if (!observation_set_) {
    log_warning_throttle(1.0,
                         "Observation has never been set. Dropping update");
  } else if (!reference_set_) {
    log_warning_throttle(1.0, "Reference has never been set. Dropping update");
  } else {
    copy_observation();

    for (size_t i = 0; i < config_.substeps; i++) {
      prepare_rollouts();
      update_reference();

      if (config_.use_tree_search) {
        timer_.reset();
        update_experts();
        timer_.add_interval("update_experts");
        sample_trajectories_via_tree();
        timer_.add_interval("sample_trajectories_via_tree");
        overwrite_rollouts();
        timer_.add_interval("overwrite_rollouts");
        timer_.print_intervals(100);
      } else {
        sample_trajectories();
      }

      optimize();
      filter_input();

      // TODO move this away. This goes year since there might be filtering
      // happening before optimal rollout
      dynamics_->reset(x0_internal_);
      for (size_t t = 0; t < steps_; t++) {
        opt_roll_.xx[t] = dynamics_->step(opt_roll_.uu[t], config_.step_size);
      }
      stage_cost_ = cost_->get_stage_cost(x0_internal_, t0_internal_);
    }
    swap_policies();

    if (renderer_) renderer_->render(rollouts_);
    if (config_.logging) {
      // data_.rollouts = rollouts_;
      data_.step_count = step_count_;
      data_.stage_cost = stage_cost_;
      data_.rollouts_cost = rollouts_cost_;
      data_.weights = omega;
      data_.optimization_time = 0.0;
      data_.reset_time = t0_internal_;
      data_.optimal_rollout = opt_roll_;
      step_count_++;
    }
  }
}

void PathIntegral::time_it() {
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::microseconds>(stop - start_time_)
          .count();
  auto timing_string =
      "Time since optimization start: " + std::to_string(duration) + "[μs], " +
      std::to_string(duration / 1000000.0) + "[s], " +
      std::to_string(1 / (duration / 1000000.0)) + "[Hz]";
  log_info(timing_string);
  start_time_ = std::chrono::high_resolution_clock::now();
}

void PathIntegral::set_observation(const observation_t& x, const double t) {
  {
    std::unique_lock<std::shared_mutex> lock(state_mutex_);
    x0_ = x;
    reset_time_ = t;
  }

  // initialization of rollouts data
  if (first_step_) {
    copy_observation();
    initialize_rollouts();
    first_step_ = false;
  }

  observation_set_ = true;
}

void PathIntegral::copy_observation() {
  std::shared_lock<std::shared_mutex> lock(state_mutex_);
  x0_internal_ = x0_;
  t0_internal_ = reset_time_;
}

void PathIntegral::initialize_rollouts() {
  std::shared_lock<std::shared_mutex> lock_state(state_mutex_);
  opt_roll_.clear();
  std::fill(opt_roll_.uu.begin(), opt_roll_.uu.end(),
            dynamics_->get_zero_input(x0_internal_));

  std::shared_lock<std::shared_mutex> lock(rollout_cache_mutex_);
  std::fill(opt_roll_cache_.xx.begin(), opt_roll_cache_.xx.end(), x0_internal_);
  std::fill(opt_roll_cache_.uu.begin(), opt_roll_cache_.uu.end(),
            dynamics_->get_zero_input(x0_internal_));
  for (size_t i = 0; i < steps_; i++) {
    opt_roll_cache_.tt[i] = t0_internal_ + config_.step_size * i;
  }
}

void PathIntegral::prepare_rollouts() {
  // find trim index
  size_t offset;
  {
    std::shared_lock<std::shared_mutex> lock(rollout_cache_mutex_);
    auto lower = std::lower_bound(opt_roll_cache_.tt.begin(),
                                  opt_roll_cache_.tt.end(), t0_internal_);
    if (lower == opt_roll_cache_.tt.end()) {
      std::stringstream warning;
      warning << "Resetting to time " << t0_internal_
              << ", greater than the last available time: "
              << opt_roll_cache_.tt.back();
      log_warning(warning.str());
    }
    offset = std::distance(opt_roll_cache_.tt.begin(), lower);
  }

  // sort rollouts for easier caching
  std::sort(rollouts_.begin(), rollouts_.end());

  // shift and trim so they restart from current time
  for (auto& roll : rollouts_) {
    shift_back(roll.uu, dynamics_->get_zero_input(roll.xx.back()), offset);
    roll.clear_cost();
    roll.clear_observation();
  }

  // shift and trim the previously optimized trajectory
  shift_back(opt_roll_.uu, dynamics_->get_zero_input(opt_roll_cache_.xx.back()),
             offset);
  shift_back(momentum_, momentum_.back(), offset);
}

void PathIntegral::set_reference_trajectory(mppi::reference_trajectory_t& ref) {
  if (ref.rr.size() != ref.tt.size()) {
    std::stringstream error;
    error << "The reference trajectory state and time dimensions do not match: "
          << ref.rr.size() << " != " << ref.tt.size();
    throw std::runtime_error(error.str());
  }
  std::unique_lock<std::shared_mutex> lock(reference_mutex_);
  rr_tt_ref_ = ref;
  reference_set_ = true;
}

void PathIntegral::update_reference() {
  std::shared_lock<std::shared_mutex> lock(reference_mutex_);
  cost_->set_reference_trajectory(rr_tt_ref_);

  if (config_.threads > 1) {
    for (auto& cost : cost_v_) cost->set_reference_trajectory(rr_tt_ref_);
  }

  if (config_.use_tree_search) {
    tree_manager_.set_reference_trajectory(rr_tt_ref_);
  }
}

void PathIntegral::sample_noise(input_t& noise) { sampler_->get_sample(noise); }

void PathIntegral::sample_trajectories_batch(dynamics_ptr& dynamics,
                                             cost_ptr& cost,
                                             const size_t start_idx,
                                             const size_t end_idx) {
  observation_t x;
  for (size_t k = start_idx; k < end_idx; k++) {
    dynamics->reset(x0_internal_);
    x = x0_internal_;
    for (size_t t = 0; t < steps_; t++) {
      // cached rollout (recompute noise)
      if (k < cached_rollouts_) {
        rollouts_[k].nn[t] = rollouts_[k].uu[t] - opt_roll_.uu[t];
      }
      // noise free trajectory
      else if (k == cached_rollouts_) {
        rollouts_[k].nn[t].setZero();
        rollouts_[k].uu[t] = opt_roll_.uu[t];
      }
      // perturbed trajectory
      else {
        sample_noise(rollouts_[k].nn[t]);
        rollouts_[k].uu[t] = opt_roll_.uu[t] + rollouts_[k].nn[t];
      }

      // impose input constraints
      bound_input(rollouts_[k].uu[t]);

      // compute input-state stage cost
      double cost_temp = 0;
      cost_temp = std::pow(config_.discount_factor, t) *
                  cost->get_stage_cost(x, t0_internal_ + t * config_.step_size);

      if (std::isnan(cost_temp)) {
        throw std::runtime_error("Something went wrong ... dynamics diverged?");
      }

      // store data
      rollouts_[k].xx[t] = x;
      rollouts_[k].cc(t) = cost_temp;
      rollouts_[k].total_cost +=
          cost_temp -  // TODO(giuseppe) this should actually be a plus
          config_.lambda * opt_roll_.uu[t].transpose() * sampler_->sigma_inv() *
              rollouts_[k].nn[t] +
          config_.lambda * opt_roll_.uu[t].transpose() * sampler_->sigma_inv() *
              opt_roll_.uu[t];

      // integrate dynamics
      x = dynamics->step(rollouts_[k].uu[t], config_.step_size);
    }
    rollouts_cost_[k] = rollouts_[k].total_cost;
  }
}

void PathIntegral::sample_trajectories() {
  if (config_.threads == 1) {
    sample_trajectories_batch(dynamics_, cost_, 0, config_.rollouts);
  } else {
    for (size_t i = 0; i < config_.threads; i++) {
      futures_[i] = pool_->enqueue(
          std::bind(&PathIntegral::sample_trajectories_batch, this,
                    std::placeholders::_1, std::placeholders::_2,
                    std::placeholders::_3, std::placeholders::_4),
          dynamics_v_[i], cost_v_[i],
          (size_t)i * config_.rollouts / config_.threads,
          (size_t)(i + 1) * config_.rollouts / config_.threads);
    }

    for (size_t i = 0; i < config_.threads; i++) futures_[i].get();
  }
}

void PathIntegral::sample_trajectories_via_tree() {
  tree_manager_.build_new_tree(x0_internal_, t0_internal_, opt_roll_);
}

void PathIntegral::overwrite_rollouts() {
  rollouts_ = tree_manager_.get_rollouts();
  rollouts_cost_ = tree_manager_.get_rollouts_cost();
}

void PathIntegral::compute_exponential_cost() {
  min_cost_ = rollouts_cost_.minCoeff();
  max_cost_ = rollouts_cost_.maxCoeff();

  if (config_.debug_print) print_cost_histogram();

  exponential_cost_ = Eigen::exp(-config_.h * (rollouts_cost_ - min_cost_) /
                                 (max_cost_ - min_cost_));
  if (config_.filtering) {
    for (size_t k = 0; k < config_.rollouts; k++) {
      if (rollouts_cost_(k) >
          (max_cost_ + (1 - config_.cost_ratio) * min_cost_))
        exponential_cost_(k) = 0.0;
    }
  }
}

void PathIntegral::optimize() {
  compute_exponential_cost();
  omega = exponential_cost_ / exponential_cost_.sum();

  // rollouts averaging
  for (size_t t = 0; t < steps_; t++) {
    momentum_[t] = config_.beta * momentum_[t];
    for (size_t k = 0; k < config_.rollouts; k++) {
      momentum_[t] += omega[k] * rollouts_[k].nn[t];
    }
  }

  for (size_t t = 0; t < steps_; t++) {
    opt_roll_.tt[t] = t0_internal_ + t * config_.step_size;
    opt_roll_.uu[t] += config_.alpha * momentum_[t];
  }

  // TODO(giuseppe) exploration covariance mean weighted noise covariance. Ok?
  if (config_.adaptive_sampling) {
    input_t delta = input_t::Zero(nu_);
    // TODO(giuseppe) remove the hardcoded baseline variance
    Eigen::MatrixXd new_covariance =
        Eigen::MatrixXd::Identity(nu_, nu_) * 0.001;
    for (size_t k = 0; k < config_.rollouts; k++) {
      for (size_t i = 0; i < steps_; i++) {
        delta = rollouts_[k].uu[i] - opt_roll_.uu[i];
        new_covariance += omega[k] * (delta * delta.transpose()) / steps_;
      }
    }
    // I could filter the covariance update using a first order
    // new_covariance = alpha * new_covariance + (1-alpha) old_covariance
    sampler_->set_covariance(new_covariance);
  }
}

void PathIntegral::filter_input() {
  if (config_.filter_type) {
    filter_.reset(t0_internal_);

    for (size_t i = 0; i < opt_roll_.uu.size(); i++) {
      filter_.add_measurement(opt_roll_.uu[i], opt_roll_.tt[i]);
    }

    for (size_t i = 0; i < opt_roll_.uu.size(); i++) {
      filter_.apply(opt_roll_.uu[i], opt_roll_.tt[i]);
    }
  }
}

void PathIntegral::get_input(const observation_t& x, input_t& u,
                             const double t) {
  static double coeff;
  static size_t idx;
  {
    std::shared_lock<std::shared_mutex> lock(rollout_cache_mutex_);
    if (t < opt_roll_cache_.tt.front()) {
      std::stringstream warning;
      warning << "Queried time " << t << " smaller than first available time "
              << opt_roll_cache_.tt.front();
      log_warning_throttle(1.0, warning.str());
      u = dynamics_->get_zero_input(x);
    }

    auto lower = std::lower_bound(opt_roll_cache_.tt.begin(),
                                  opt_roll_cache_.tt.end(), t);
    if (lower == opt_roll_cache_.tt.end()) {
      std::stringstream warning;
      warning << "Queried time " << t << " larger than last available time "
              << opt_roll_cache_.tt.back();
      log_warning_throttle(1.0, warning.str());
      u = opt_roll_cache_.uu.back();
      return;
    }

    idx = std::distance(opt_roll_cache_.tt.begin(), lower);
    // first
    if (idx == 0) {
      u = opt_roll_cache_.uu.front();
    }
    // last
    else if (idx > opt_roll_cache_.steps_) {
      u = opt_roll_cache_.uu.back();
    }
    // interpolate
    else {
      coeff = (t - *(lower - 1)) / (*lower - *(lower - 1));
      u = (1 - coeff) * opt_roll_cache_.uu[idx - 1] +
          coeff * opt_roll_cache_.uu[idx];
    }
  }
}

void PathIntegral::get_diagonal_variance(Eigen::VectorXd& var) const {
  var = sampler_->sigma().diagonal();
}

void PathIntegral::get_input_state(const observation_t& x, observation_t& x_nom,
                                   input_t& u_nom, const double t) {
  {
    std::shared_lock<std::shared_mutex> lock(rollout_cache_mutex_);
    if (t < opt_roll_cache_.tt.front()) {
      std::stringstream warning;
      warning << "Queried time " << t << " smaller than first available time "
              << opt_roll_cache_.tt.front();
      log_warning_throttle(1.0, warning.str());
      x_nom = opt_roll_cache_.xx.front();
      u_nom = dynamics_->get_zero_input(x);
      return;
    }

    auto lower = std::lower_bound(opt_roll_cache_.tt.begin(),
                                  opt_roll_cache_.tt.end(), t);
    if (lower == opt_roll_cache_.tt.end()) {
      std::stringstream warning;
      warning << "Queried time " << t << " larger than last available time "
              << opt_roll_cache_.tt.back();
      log_warning_throttle(1.0, warning.str());
      x_nom = opt_roll_cache_.xx.back();
      u_nom = dynamics_->get_zero_input(x);
      return;
    }

    size_t idx = std::distance(opt_roll_cache_.tt.begin(), lower);
    // first
    if (idx == 0) {
      x_nom = opt_roll_cache_.xx.front();
      u_nom = opt_roll_cache_.uu.front();
    }
    // last
    else if (idx > opt_roll_cache_.steps_) {
      x_nom = opt_roll_cache_.xx.back();
      u_nom = opt_roll_cache_.uu.back();
    }
    // interpolate
    else {
      double coeff = (t - *(lower - 1)) / (*lower - *(lower - 1));
      u_nom = (1 - coeff) * opt_roll_cache_.uu[idx - 1] +
              coeff * opt_roll_cache_.uu[idx];
      x_nom = opt_roll_cache_.xx[idx];  // TODO offer a way to also do
                                        // interpolation of the state
    }
  }
}

bool PathIntegral::get_optimal_rollout(observation_array_t& xx,
                                       input_array_t& uu) {
  std::shared_lock<std::shared_mutex> lock(rollout_cache_mutex_);
  auto lower = std::lower_bound(opt_roll_cache_.tt.begin(),
                                opt_roll_cache_.tt.end(), reset_time_);
  if (lower == opt_roll_cache_.tt.end()) return false;
  size_t offset = std::distance(opt_roll_cache_.tt.begin(), lower);

  // fill with portion of vector starting from current time
  xx = observation_array_t(opt_roll_cache_.xx.begin() + offset,
                           opt_roll_cache_.xx.end());
  uu = input_array_t(opt_roll_cache_.uu.begin() + offset,
                     opt_roll_cache_.uu.end());
  return true;
}

void PathIntegral::update_experts() { expert_.update_expert(1, opt_roll_.uu); };

void PathIntegral::swap_policies() {
  std::unique_lock<std::shared_mutex> lock(rollout_cache_mutex_);
  opt_roll_cache_ = opt_roll_;
}

PathIntegral::input_array_t PathIntegral::offline_control(
    const DynamicsBase::observation_t& x, const int subit, const double t) {
  set_observation(x, t);
  for (size_t i = 0; i < subit; i++) update_policy();
}

template <typename T>
void PathIntegral::shift_back(std::vector<T>& v_out, const T& fill,
                              const int offset) {
  std::rotate(v_out.begin(), v_out.begin() + offset, v_out.end());
  std::fill(v_out.end() - offset, v_out.end(), fill);
}

// explicit instantiation
template void PathIntegral::shift_back<Eigen::VectorXd>(
    std::vector<Eigen::VectorXd>& v_out, const Eigen::VectorXd& fill,
    const int offset);

void PathIntegral::print_cost_histogram() const {
  constexpr int nbins = 10;
  double delta = (max_cost_ - min_cost_) / nbins;

  std::cout << "Rollouts cost histogram" << std::endl;
  std::cout << "Max: " << max_cost_ << ", Min: " << min_cost_
            << ", delta: " << delta << std::endl;
  std::map<int, int> hist{};
  for (size_t k = 0; k < config_.rollouts; k++) {
    ++hist[std::round((rollouts_cost_(k) - min_cost_) / delta)];
  }
  for (auto p : hist) {
    std::cout << std::setw(2) << p.first << ' ' << std::string(p.second, '*')
              << '\n';
  }
  std::cout << std::endl << std::endl;
}

void PathIntegral::bound_input(input_t& u) {
  if (config_.bound_input)
    u = u.cwiseMax(config_.u_min).cwiseMin(config_.u_max);
}

}  // namespace mppi


text:
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    A Host implementation of Bag of Visual Words
% by: Alireza Ahmadi and Robin Steffens
% Alireza.Ahmadi@uni-bonn.de
% Robin.Steffens@gmx.net
% University of Bonn- MSc Robotics & Geodetic Engineering
% alirezaahmadi.xyz
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <boost/filesystem.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <include/Kmeans_Flann.h>
#include <include/utils_html.h>
#include <include/utils_parser.h>
#include <include/sifts.h>
#include <include/utils.h>

using namespace boost::filesystem;
using namespace cv;
using namespace std;

vector<string> imageNames;
vector<string> imageNamesList;
vector<double> timeStamps;
vector<gps_info> gpsPositions;
std::vector<Product> prod;
Kmeans_Flann KmeansFLANN;
vector<HistImage> Img_Hist;
uint imageCount;
vector<HistImage> inp_Hist;
vector<gps_info> gps_image;
std::string datasetDirectory;

#define LOG_EN

void processDataset(void);
void LoadDataset(int argc, char **argv);
void runEvaluation(int argc, char **argv);
void runClassifier(int argc, char **argv);

int main(int argc, char **argv) {

  if (argc >= 2) {

    imageCount = 0;

    // loading images, timstamps and GPS positions
    LoadDataset(argc, argv);

    float time_diff = 1000;
    float time_diff_old = 1000;
    size_t start_gps = 1;

    for (size_t i = 1; i < timeStamps.size(); i++){
      for (size_t j = start_gps; start_gps < gpsPositions.size(); j++){
        time_diff = abs(timeStamps[i] - gpsPositions[j].time);
        if (time_diff > time_diff_old){
          gps_info tmp_gps = {gpsPositions[j-1].time, gpsPositions[j-1].latitude, gpsPositions[j-1].longitude};
          gps_image.push_back(tmp_gps);
          start_gps = j-1;
          i+=9;
          time_diff_old = 1000;
          break;
        }else{
          time_diff_old = time_diff;
        }
      }
    }

    // if user didn't enter number of images to process set it to maximum
    if ((argc == 4 && stoi(argv[3]) == 0) || argc == 2) {
      imageCount = imageNames.size();
    } else if (argc >= 4 && stoi(argv[3]) != 0) {
      imageCount = stoi(argv[3]);
    } else if (argc == 2 && stoi(argv[1]) == 1) {
      // dataset ....
    } else {
      cout << "Inputs don't match !!!" << endl;
      cout << "your input: " << argc << " " << stoi(argv[1]) << " "
           << stoi(argv[2]) << " " << stoi(argv[3]) << endl;
      return 0;
    }

    #undef ClusterNum_K
    #define ClusterNum_K stoi(argv[2])

    inp_Hist = {HistImage{"Empty", 0, 0.0, 0.0, 0.0, 0.0, {0.0}}};
    HistImage tmp_histogram{"Empty", 0, 0.0, 0.0, 0.0, 0.0, {0.0}};
    for (uint i = 0; i < imageCount; ++i) {
      Img_Hist.push_back(tmp_histogram);
    }
    // Getting the Dataset, extracting features and saving them to binray files.
    if (1 == stoi(argv[1])) {
      cout << "dataset ...." << endl;
      processDataset();
    } else if (2 == stoi(argv[1])) {
      cout << "Classifier ...." << endl;
      runClassifier(argc, argv);
    } else if (3 == stoi(argv[1])) {
      cout << "Evaluation ...." << endl;
      runEvaluation(argc, argv);
    }
  } else {
    cout << "Required inputs: Mode {1:dataset_run, 2:Kmeans, 3:hist&TFIDF} , K "
            "= ? , Number of Images to read: ?"
         << endl;
  }
  return 0;
}

void processDataset(void){
  Mat descriptorImage;
      Product tmp{"Empty", 0, 0, false, {0}};
      for (size_t i = 0; i < imageNamesList.size(); ++i) {
        cout << datasetDirectory + imageNames[i] << endl;
        Mat descriptors =
            computeSifts(datasetDirectory + imageNames[i], descriptorImage);

        const string file_name = "../sifts/" + imageNamesList[i] + ".bin";
        cerr << file_name << endl;
        // products get loaded
        for (int row = 0; row < descriptors.rows; row++) {
          for (int col = 0; col < descriptors.cols; col++) {
            tmp.sift[col] = descriptors.at<float>(row, col);
          }
          strcpy(tmp.name, imageNamesList[i].c_str());
          tmp.siftNum = row;
          tmp.isCentroid = false;
          prod.push_back(tmp);
        }
        writeBinaryProduct(file_name, prod);
        prod.clear();
      }
}

void runClassifier(int argc, char **argv){

      std::vector<uint> Img_SiftSize;

      for (size_t i = 0; i < imageCount; ++i) {
        const string file_name = "../sifts/" + imageNamesList[i] + ".bin";
        std::vector<Product> tmp_prod = readBinaryProduct(file_name);
        prod.insert(prod.end(), tmp_prod.begin(), tmp_prod.end());
        Img_SiftSize.push_back(tmp_prod.size());
      }

      cout << "Total Number of Sifts: " << prod.size() << " : "
           << Img_SiftSize.size() << endl;

      vector<Product> Centroids =
          KmeansFLANN.kmeans(prod, (uint16_t)ClusterNum_K, Img_Hist, Img_SiftSize);

      writeBinaryProduct("../sifts/Final_Centroids.bin", Centroids);
      writeBinaryProduct("../sifts/Updated_Sifts.bin", prod);
      writeBinaryHistImage("../sifts/Img_Hist.bin", Img_Hist);
}

void runEvaluation(int argc, char **argv){
  vector<Product> Centroids =
      readBinaryProduct("../sifts/Final_Centroids.bin"); // output of kmeans...
  cout << "Number of Final Centroids: " << Centroids.size() << " -> "
        << endl;
  vector<Product> Sifts =
      readBinaryProduct("../sifts/Updated_Sifts.bin"); // we dont use this anymore ...
  cout << "Number of Updated Sifts: " << Sifts.size() << " -> " << endl;
  vector<HistImage> ImgHist =
      readBinaryHistImage("../sifts/Img_Hist.bin"); // hist struct of each image
  cout << "Number of histograms: " << ImgHist.size() << " -> " << endl;

  vector<vector<uint>> hist = hist_features(ImgHist, (uint)ClusterNum_K);
  vector<vector<float>> tfIdf_hist;
  uint Comp_reference = 0;
  vector<string> InpNum = getimageNames("../img"); // takeing the name of input image from /img folder
  // string input_image = "imageCompressedCam0_0000680";
  if (argc == 4) {
    // Build SIFT for Input Image
    vector<Product> prod_input;
    Mat descriptorImage_Input;
    Product tmp_input{"Empty", 0, 0, false, {0}};
    Mat descriptor_input =
        computeSifts("../img/" + InpNum[0], descriptorImage_Input);

    cout << "Input: " << InpNum[0] << endl;

    // making a product type for input image and filling in
    for (int row = 0; row < descriptor_input.rows; row++) {
      for (int col = 0; col < descriptor_input.cols; col++) {
        tmp_input.sift[col] = descriptor_input.at<float>(row, col);
      }
      strcpy(tmp_input.name, InpNum[0].c_str());
      tmp_input.siftNum = row;
      tmp_input.isCentroid = false;
      prod_input.push_back(tmp_input);
    }

    cv::Mat centroid_Mat;
    for (size_t i = 0; i < Centroids.size(); i++) {     // Prod to Mat format ... Centriods
      cv::Mat centroid_vec(1, 128, cv::DataType<float>::type,
                            Centroids[i].sift);
      centroid_Mat.push_back(centroid_vec);
    }

    // Loop for Kmeans-Flann
    cv::flann::KDTreeIndexParams indexParams(4);
    cv::flann::Index kdtree(centroid_Mat, indexParams);
    // Store Indizes from k nearest sifts
    cv::Mat nearestVectorIdx(1, ClusterNum_K, cv::DataType<int>::type);
    cv::Mat nearestVectorDist(1, ClusterNum_K, cv::DataType<float>::type);

    for (size_t i = 0; i < prod_input.size(); i++) {    // Prod to Mat format ... Samples
      cv::Mat sift_vec(1, 128, cv::DataType<float>::type,
                        prod_input[i].sift);

      kdtree.knnSearch(sift_vec, nearestVectorIdx, nearestVectorDist, 1);

      // 1 nearest rows of prod_input get the number of cluster
      uint closestIdx = nearestVectorIdx.at<uint>(0, 0);
      prod_input[i].ClusterID = closestIdx;
      inp_Hist[0].hist[nearestVectorIdx.at<uint>(0, 0)]++;
    }
    vector<vector<uint>> inp_hist =
        hist_features(inp_Hist, (uint)ClusterNum_K);

    hist.push_back(inp_hist[0]);

    tfIdf_hist = tfIdf(hist);

    Comp_reference = tfIdf_hist.size() - 1;
  } else if (argc == 5) {
    Comp_reference = stoi(argv[4]);
    InpNum[0] = imageNames[stoi(argv[4])];

    cout << "Input: " << InpNum[0] << " --> " + datasetDirectory << endl;
    tfIdf_hist = tfIdf(hist);
  }

  vector<float> compare_hist =
      cosine_comparison(tfIdf_hist, tfIdf_hist[Comp_reference]);

  for (size_t i = 0; i < ImgHist.size(); i++) {
    // cout << i << ": " << compare_hist[i] << endl;
    // cerr << imageNamesList.size() << "  ImName --> " << imageNamesList[i].c_str() << endl;
    strcpy(ImgHist[i].name, imageNamesList[i].c_str());
    ImgHist[i].time = gps_image[i].time;
    ImgHist[i].latitude = gps_image[i].latitude;
    ImgHist[i].longitude = gps_image[i].longitude;
    ImgHist[i].CosCom = compare_hist[i];
  }
  std::sort(ImgHist.begin(), ImgHist.end(), ImgHist_sorter);

  // for (size_t i = 0; i < ImgHist.size(); i++) {
  //   cout << i << ": " << ImgHist[i].CosCom << " -> " << compare_hist[i]
  //        << endl;
  // }

  writeBinaryHistImage("../sifts/results.bin", ImgHist);
  html_print(InpNum[0], 10, argc);
}

void LoadDataset(int argc, char **argv){
  assert(argc == 3);
  datasetDirectory = argv[2];
  // getting Dataset - loading Image names
  imageNames = getimageNames(datasetDirectory);
  #ifdef LOG_EN
    for (size_t i = 0; i < imageNames.size(); i++) {
      cout << imageNames[i] << endl;
    }
  #endif

  // subtracting .png from image names
  imageNamesList = modifyimageNames(imageNames);
  #ifdef LOG_EN
    for (size_t i = 0; i < imageNamesList.size(); i++) {
      cout << imageNamesList[i] << endl;
    }
    freopen("imageNames.txt", "w", stdout);
  #endif

  // extracting timestapms from image list
  timeStamps = readimageNamesList(datasetDirectory + "/image-imageNamesList.txt");
  #ifdef LOG_EN
    for (size_t i = 0; i < 10; i++) {
      cout << std::setprecision(20) << timeStamps[i] << endl;
    }
  #endif

  // reading GPS positions
  gpsPositions = readGPSPositions(datasetDirectory + "/gps_info.txt");
  #ifdef LOG_EN
    for (auto i = 0; i != 10; i++) {
      std::cout << gpsPositions[i].time << " ";
      std::cout << gpsPositions[i].latitude << " ";
      std::cout << gpsPositions[i].longitude << " " << endl;
    }
  #endif
}

ext: cpp

max_stars_repo_name: alirezaahmadi/Bag-of-Visual-Words-

max_issues_repo_name: alirezaahmadi/Bag-of-Visual-Words-

max_forks_repo_name: alirezaahmadi/Bag-of-Visual-Words-


text:
// Copyright (c) Steinwurf ApS 2017.
// All Rights Reserved
//
// Distributed under the "BSD License". See the accompanying LICENSE.rst file.

#pragma once

#include <cstdint>
#include <cassert>
#include <memory>
#include <bnb/stream_reader.hpp>
#include <boost/optional.hpp>

#include "helper.hpp"

namespace mts
{
class adaptation_field
{
public:

    static boost::optional<adaptation_field> parse(
        const uint8_t* data, uint64_t size, std::error_code& error)
    {
        bnb::stream_reader<endian::big_endian> reader(data, size, error);
        return parse(reader);
    }

    static boost::optional<adaptation_field> parse(
        bnb::stream_reader<endian::big_endian>& reader)
    {
        mts::adaptation_field field;

        reader.read_bytes<1>(field.m_length);
        if (field.m_length == 0)
            return field;
        auto adaptation_field = reader.skip(field.m_length);

        adaptation_field
        .read_bits<bitter::u8, bitter::msb0, 1, 1, 1, 1, 1, 1, 1, 1>()
        .get<0>(field.m_discontinuity_indicator)
        .get<1>(field.m_random_access_indicator)
        .get<2>(field.m_elementary_stream_priority_indicator)
        .get<3>(field.m_pcr_flag)
        .get<4>(field.m_opcr_flag)
        .get<5>(field.m_splicing_point_flag)
        .get<6>(field.m_transport_private_data_flag)
        .get<7>(field.m_adaptation_field_extension_flag);

        if (field.m_pcr_flag)
        {
            uint64_t pcr_base = 0;
            uint16_t pcr_extension = 0;
            adaptation_field.read_bits<bitter::u48, bitter::msb0, 33, 6, 9>()
            .get<0>(pcr_base)
            .get<2>(pcr_extension);
            field.m_program_clock_reference = pcr_base * 300 + pcr_extension;
        }

        if (field.m_opcr_flag)
        {
            uint64_t opcr_base = 0;
            uint16_t opcr_extension = 0;

            adaptation_field.read_bits<bitter::u48, bitter::msb0, 33, 6, 9>()
            .get<0>(opcr_base)
            .get<2>(opcr_extension);
            field.m_original_program_clock_reference =
                opcr_base * 300 + opcr_extension;
        }

        if (field.m_splicing_point_flag)
        {
            adaptation_field.read_bytes<1>(field.m_splice_countdown);
        }
        if (field.m_transport_private_data_flag)
        {
            adaptation_field.read_bytes<1>(
                field.m_transport_private_data_length);

            auto transport_private_data = adaptation_field.skip(
                field.m_transport_private_data_length);

            field.m_transport_private_data = transport_private_data.data();
        }
        if (field.m_adaptation_field_extension_flag)
        {
            uint8_t adaptation_field_extension_length = 0;
            adaptation_field.read_bytes<1>(
                adaptation_field_extension_length);

            auto adaptation_field_extension = adaptation_field.skip(
                adaptation_field_extension_length);

            adaptation_field_extension
            .read_bits<bitter::u8, bitter::msb0, 1, 1, 1, 5>()
            .get<0>(field.m_ltw_flag)
            .get<1>(field.m_piecewise_rate_flag)
            .get<2>(field.m_seamless_splice_flag);

            if (field.m_ltw_flag)
            {
                adaptation_field_extension
                .read_bits<bitter::u16, bitter::msb0, 1, 15>()
                .get<0>(field.m_ltw_valid_flag)
                .get<1>(field.m_ltw_offset);
            }

            if (field.m_piecewise_rate_flag)
            {
                adaptation_field_extension
                .read_bits<bitter::u24, bitter::msb0, 2, 22>()
                .get<1>(field.m_piecewise_rate);
            }

            if (field.m_seamless_splice_flag)
            {

                uint8_t dts_next_au_32_30 = 0;
                uint16_t dts_next_au_29_15 = 0;
                uint16_t dts_next_au_14_0 = 0;
                adaptation_field_extension
                .read_bits<bitter::u40, bitter::msb0, 4, 3, 1, 15, 1, 15, 1>()
                .get<0>(field.m_splice_type)
                .get<1>(dts_next_au_32_30)
                .get<3>(dts_next_au_29_15)
                .get<5>(dts_next_au_14_0);
                field.m_dts_next_au = helper::read_timestamp(
                    dts_next_au_32_30,
                    dts_next_au_29_15,
                    dts_next_au_14_0);
            }
        }

        if (reader.error())
        {
            return boost::none;
        }

        return field;
    }

public:

    uint8_t length() const
    {
        return m_length;
    }

    bool discontinuity_indicator() const
    {
        assert(m_length != 0);
        return m_discontinuity_indicator;
    }

    bool random_access_indicator() const
    {
        assert(m_length != 0);
        return m_random_access_indicator;
    }

    bool elementary_stream_priority_indicator() const
    {
        assert(m_length != 0);
        return m_elementary_stream_priority_indicator;
    }

    bool pcr_flag() const
    {
        assert(m_length != 0);
        return m_pcr_flag;
    }

    bool opcr_flag() const
    {
        assert(m_length != 0);
        return m_opcr_flag;
    }

    bool splicing_point_flag() const
    {
        assert(m_length != 0);
        return m_splicing_point_flag;
    }

    bool transport_private_data_flag() const
    {
        assert(m_length != 0);
        return m_transport_private_data_flag;
    }

    bool adaptation_field_extension_flag() const
    {
        assert(m_length != 0);
        return m_adaptation_field_extension_flag;
    }

    uint64_t program_clock_reference() const
    {
        assert(pcr_flag());
        return m_program_clock_reference;
    }

    uint64_t original_program_clock_reference() const
    {
        assert(opcr_flag());
        return m_original_program_clock_reference;
    }

    uint8_t splice_countdown() const
    {
        assert(splicing_point_flag());
        return m_splice_countdown;
    }

    const uint8_t* transport_private_data() const
    {
        assert(transport_private_data_flag());
        return m_transport_private_data;
    }

    uint8_t transport_private_data_length() const
    {
        assert(transport_private_data_flag());
        return m_transport_private_data_length;
    }

    bool ltw_flag() const
    {
        assert(adaptation_field_extension_flag());
        return m_ltw_flag;
    }

    bool piecewise_rate_flag() const
    {
        assert(adaptation_field_extension_flag());
        return m_piecewise_rate_flag;
    }

    bool seamless_splice_flag() const
    {
        assert(adaptation_field_extension_flag());
        return m_seamless_splice_flag;
    }

    bool ltw_valid_flag() const
    {
        assert(adaptation_field_extension_flag());
        assert(ltw_flag());
        return m_ltw_valid_flag;
    }

    uint16_t ltw_offset() const
    {
        assert(adaptation_field_extension_flag());
        assert(ltw_flag());
        return m_ltw_offset;
    }

    uint32_t piecewise_rate() const
    {
        assert(adaptation_field_extension_flag());
        assert(piecewise_rate_flag());
        return m_piecewise_rate;
    }

    uint8_t splice_type() const
    {
        assert(adaptation_field_extension_flag());
        assert(seamless_splice_flag());
        return m_splice_type;
    }

    uint64_t dts_next_au() const
    {
        assert(adaptation_field_extension_flag());
        assert(seamless_splice_flag());
        return m_dts_next_au;
    }

private:

    uint8_t m_length = 0;
    bool m_discontinuity_indicator = false;
    bool m_random_access_indicator = false;
    bool m_elementary_stream_priority_indicator = false;
    bool m_pcr_flag = false;
    bool m_opcr_flag = false;
    bool m_splicing_point_flag = false;
    bool m_transport_private_data_flag = false;
    bool m_adaptation_field_extension_flag = false;

    uint64_t m_program_clock_reference = 0;

    uint64_t m_original_program_clock_reference = 0;

    uint8_t m_splice_countdown = 0;

    uint8_t m_transport_private_data_length = 0;
    const uint8_t* m_transport_private_data = nullptr;

    bool m_ltw_flag = false;
    bool m_piecewise_rate_flag = false;
    bool m_seamless_splice_flag = false;

    bool m_ltw_valid_flag = false;
    uint16_t m_ltw_offset = 0;

    uint32_t m_piecewise_rate = 0;

    uint8_t m_splice_type = 0;
    uint64_t m_dts_next_au = 0;
};
}

ext: hpp

max_stars_repo_name: steinwurf/mts

max_issues_repo_name: steinwurf/mts

max_forks_repo_name: steinwurf/mts


text:
//
//  KeyValueFile.hpp
//
//  Created by Ignac Banic on 11/01/20.
//  Copyright © 2020 Ignac Banic. All rights reserved.
//

#pragma once

#include <string>
#include <map>
#include <vector>
#include <filesystem>
#include <boost/iostreams/device/mapped_file.hpp>
#include <optional>
#include <string_view>
#include <climits>
#include <search/CompressSize.hpp>
#include <fstream>
#include <atomic>
#include <mutex>
#include <shared_mutex>
#include <tuple>
#include <search/Types.hpp>
#include <cstring>

// change that file supports both platforms:
// BOOST_BIG_ENDIAN   BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4()

namespace fs =  std::filesystem; //boost::filesystem;

namespace Search {

	class KeyValueFile {
	public:
		static const uint64_t Version = 1;

	private:
		boost::iostreams::mapped_file file_;
		fs::path path_;
		bool locked_;
		std::byte* buffer_;

		struct ImportingData {
			std::atomic<uint64_t> numItems;
			std::atomic<uint64_t> wasted;
			std::vector<std::pair<size_t, size_t>> dataRange;
			std::mutex mutex_;
		};
		ImportingData* importing_;

		class Item {
		private:
			std::byte* data_;
			uint64_t offset_;

		public:
			Item() : data_(nullptr), offset_(0) {}
			Item(std::byte* data, uint64_t offset)
				: data_(data), offset_(offset)
			{ }
			bool valid() const { return offset_ != 0; }
			uint64_t offset() const { return offset_; }
			uint64_t nextOffset() const {
				//return *reinterpret_cast<const uint64_t*>(itemData());
				uint64_t n; std::memcpy(&n, itemData(), sizeof(n)); return n;
			}
			void setNextOffset(uint64_t nextOffset) {
				std::memcpy(itemData(), &nextOffset, sizeof(uint64_t));
			}
			std::string_view key() const
			{
				const std::byte* ptr = itemData() + sizeof(uint64_t);
				auto sKey = readSize(ptr);
				/*auto sValue =*/ readSize(ptr);
				return {(const char*)ptr, sKey};
			}
			BytesView value() const {
				const std::byte* ptr = itemData() + sizeof(uint64_t);
				auto sKey = readSize(ptr);
				auto sValue = readSize(ptr);
				ptr += sKey;
				return BytesView(ptr, sValue);
			}
			void setValue(BytesView txt) {
				const std::byte* ptr = itemData() + sizeof(uint64_t);
				auto sKey = readSize(ptr);
				const std::byte* ptrValSize = ptr;
				auto sValue = readSize(ptr);
				ptr += sKey;

				std::memcpy(const_cast<std::byte*>(ptr), txt.data(), txt.size());
				auto numBytes = numBytesSize(sValue);
				std::byte* ptr2 = const_cast<std::byte*>(ptrValSize);
				writeSize(ptr2, txt.size(), numBytes);
			}
			size_t calcSize() const {
				const std::byte* ptr = itemData() + sizeof(uint64_t);
				auto sKey = readSize(ptr);
				auto sValue = readSize(ptr);
				return ptr - itemData() + sKey + sValue;
			}
			static size_t calcSize(std::string_view key, BytesView value) {
				return sizeof(uint64_t) + numBytesSize(key.size()) + numBytesSize(value.size()) + key.size() + value.size();
			}
			static void write(std::byte*& buffer, uint64_t nextOffset, std::string_view key, BytesView value) {
				std::memcpy(buffer, &nextOffset, sizeof(uint64_t));
				buffer += sizeof(uint64_t);
				writeSize(buffer, key.size());
				writeSize(buffer, value.size());
				std::memcpy(buffer, key.data(), key.size());
				buffer += key.size();
				std::memcpy(buffer, value.data(), value.size());
				buffer += value.size();
			}
			Item next() const {
				return Item(data_, nextOffset());
			}

		private:
			std::byte* itemData() const {
				return data_ + offset_;
			}
		};

	public:
		KeyValueFile(const fs::path& path);
		~KeyValueFile();
		KeyValueFile(const KeyValueFile&) = delete;
		KeyValueFile& operator=(const KeyValueFile&) = delete;
		KeyValueFile(KeyValueFile&&) = default;
		KeyValueFile& operator=(KeyValueFile&&) = default;

		void set(const std::string& key, const Bytes& value);
		void set(std::string_view key, BytesView value);
		//void setBatch(const std::vector<std::pair<Bytes, Bytes>>& arr);
		//void setBatch(const std::vector<std::pair<BytesView, BytesView>>& arr);
		void set(const KeyValueFile& db2);
		BytesView get(const std::string& key) const;
		BytesView get(std::string_view key) const;
		BytesView getWithBucket(uint64_t bucket, std::string_view key) const;
		void remove(std::string_view key);
		void remove(const std::string& key);
		void optimize();
		void lockTableForNumItems(uint64_t n);
		void unlockTable();
		std::vector<std::pair<std::string_view, BytesView>> allDocuments() const;

		const fs::path& path() const { return path_; }
		static void createFile(const fs::path& path, uint64_t tabSize=101, uint64_t contentSize=1000);
		size_t fileSize() const;
		static bool isFileVersionOk(const fs::path& pth);

		static void bulkWrite(std::ofstream& out, std::string_view key, BytesView value);
	private:
		void bulkInsertEnlarge(size_t nthThread, size_t numThreads);
	public:
		void bulkInsert(uint64_t bucket, std::string_view key, BytesView value, size_t nthThread, size_t numThreads);
		void bulkRemove(uint64_t bucket, std::string_view key, BytesView value, size_t nthThread, size_t numThreads);
		static std::tuple<uint64_t, std::string_view, BytesView> bulkRead(const std::byte*& dt);
		static bool bulkIsInThread(uint64_t bucket, size_t nthThread, size_t numThreads, uint64_t numBuckets);
		void bulkStart(size_t numThreads);
		void bulkStop();
		//Bytes getWithBucketImport(uint64_t bucket, std::string_view key) const;

		static uint64_t calcHash(std::string_view key);
		static uint64_t calcBucketFromHash(uint64_t hash, uint64_t numBuckets);
		uint64_t calcBucket(std::string_view key) const;
		//std::vector<uint64_t> calcBuckets(const std::vector<std::pair<std::string_view, BytesView>>& arr) const;
		uint64_t numBuckets() const;
		void ensureOptimalWaste();
		uint64_t numItems() const;
		uint64_t wasted() const;
		void clear();

	private:
		void setInternal(uint64_t bucket, std::string_view key, BytesView value);
		void removeInternal(uint64_t bucket, std::string_view key);
		Item firstItem(uint64_t bucket) const;
		std::pair<uint64_t, Item> findInternal(uint64_t bucket, std::string_view key) const;

		std::byte* data() const;
		uint64_t tableOffset(uint64_t bucket) const;
		void setTableOffset(uint64_t bucket, uint64_t offset) const;

		uint64_t nextDataOffset() const;
		void setNextDataOffset(uint64_t off);

		void setNumItems(uint64_t num);
		void setWasted(uint64_t w);

		void ensureFreeSpace(size_t additional);
		void ensureTableSize(int64_t additional);
		fs::path tmpFilePath() const;
		inline static size_t findTabSizePrime(size_t minNum);
		inline static size_t findTabSizePrimeDouble(size_t minNum);
		void openFile();
		static uint64_t availableMemory();
		void changeTable(uint64_t numBuckets, uint64_t bodySize);
	};
}

max_stars_repo_name: ibanic/search

max_issues_repo_name: ibanic/search

max_forks_repo_name: ibanic/search


text:
/// Memory utilities.
///
/// @file

#include <type_traits>

#include <boost/core/noncopyable.hpp>

//==========================================================================
namespace medianizer {
//==========================================================================

/// Generic RAII wrapper around raw C pointers.
/// Like std::unique_ptr but not limited to a memory allocation.
template<typename _T, auto _Deleter>
class CPtr final : boost::noncopyable
{
public:
  CPtr() noexcept = default;

  CPtr(_T* const ptr) noexcept
    : m_ptr{ptr}
  {}

  CPtr(CPtr&& other) noexcept
  {
    del();
    m_ptr = other.m_ptr;
    other.m_ptr = nullptr;
  }

  CPtr& operator=(CPtr&& other) noexcept
  {
    del();
    m_ptr = other.m_ptr;
    other.m_ptr = nullptr;
  }

  ~CPtr()
  {
    del();
  }

  auto& operator*() const noexcept
  {
    return *m_ptr;
  }

  auto operator->() const noexcept
  {
    return m_ptr;
  }

  auto get() const noexcept
  {
    return m_ptr;
  }

private:
  void del()
  {
    if (m_ptr != nullptr)
    {
      _Deleter(m_ptr);
      m_ptr = nullptr;
    }
  }

  _T* m_ptr{};
};

//==========================================================================

/// AV* objects RAII wrapper.
template<typename _T, auto _Deleter>
class AVPtr final
{
  public:
    AVPtr() noexcept = default;

    AVPtr(_T* const ptr) noexcept
      : m_ptr{ptr}
    {}

  auto& operator*() const noexcept
  {
    return *m_ptr;
  }

  auto operator->() const noexcept
  {
    return m_ptr.get();
  }

  auto get() const noexcept
  {
    return m_ptr.get();
  }

  private:
    static void deleter(_T* ptr)
    {
      _Deleter(&ptr);
    }

    CPtr<_T, deleter> m_ptr{};
};

//==========================================================================
} // namespace medianizer

max_stars_repo_name: dsiroky/pex

max_issues_repo_name: dsiroky/pex

max_forks_repo_name: dsiroky/pex


text:
/*!
@file
Defines `boost::hana::experimental::types`.

@copyright Louis Dionne 2013-2016
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
 */

#ifndef BOOST_HANA_EXPERIMENTAL_TYPES_HPP
#define BOOST_HANA_EXPERIMENTAL_TYPES_HPP

#include <boost/hana/bool.hpp>
#include <boost/hana/concept/metafunction.hpp>
#include <boost/hana/config.hpp>
#include <boost/hana/detail/any_of.hpp>
#include <boost/hana/fwd/at.hpp>
#include <boost/hana/fwd/contains.hpp>
#include <boost/hana/fwd/core/tag_of.hpp>
#include <boost/hana/fwd/equal.hpp>
#include <boost/hana/fwd/is_empty.hpp>
#include <boost/hana/fwd/transform.hpp>
#include <boost/hana/fwd/unpack.hpp>
#include <boost/hana/type.hpp>

#include <cstddef>
#include <type_traits>
#include <utility>

BOOST_HANA_NAMESPACE_BEGIN
    namespace experimental {
        //! @ingroup group-experimental
        //! Container optimized for holding types.
        //!
        //! It is often useful to manipulate a sequence that contains types
        //! only, without any associated runtime value. This container allows
        //! storing and manipulating pure types in a much more compile-time
        //! efficient manner than using `hana::tuple`, which must assume that
        //! its contents might have runtime values.
        template <typename ...T>
        struct types;

        struct types_tag;

        //////////////////////////////////////////////////////////////////////

        template <typename ...T>
        struct types { };
    } // end namespace experimental

    template <typename ...T>
    struct tag_of<experimental::types<T...>> {
        using type = experimental::types_tag;
    };

    // Foldable
    template <>
    struct unpack_impl<hana::experimental::types_tag> {
        template <typename ...T, typename F, typename = typename std::enable_if<
            !hana::Metafunction<F>::value
        >::type>
        static constexpr decltype(auto) apply(hana::experimental::types<T...> const&, F&& f) {
            return static_cast<F&&>(f)(hana::type<T>{}...);
        }

        template <typename ...T, typename F, typename = typename std::enable_if<
            hana::Metafunction<F>::value
        >::type>
        static constexpr hana::type<typename F::template apply<T...>::type>
        apply(hana::experimental::types<T...> const&, F const&) { return {}; }
    };

    // Functor
    template <>
    struct transform_impl<hana::experimental::types_tag> {
        template <typename ...T, typename F, typename = typename std::enable_if<
            !hana::Metafunction<F>::value
        >::type>
        static constexpr auto apply(hana::experimental::types<T...> const&, F&& f)
            -> hana::experimental::types<typename decltype(+f(hana::type<T>{}))::type...>
        { return {}; }

        template <typename ...T, typename F, typename = typename std::enable_if<
            hana::Metafunction<F>::value
        >::type>
        static constexpr hana::experimental::types<typename F::template apply<T>::type...>
        apply(hana::experimental::types<T...> const&, F const&) { return {}; }
    };

    namespace types_detail {
        template <std::size_t I, typename T>
        struct elt { using type = T; };

        template <typename Indices, typename ...T>
        struct indexer;

        template <std::size_t ...I, typename ...T>
        struct indexer<std::index_sequence<I...>, T...>
            : elt<I, T>...
        { };

        template <std::size_t I, typename T>
        elt<I, T> get_elt(elt<I, T> const&);
    }

    // Iterable
    template <>
    struct at_impl<hana::experimental::types_tag> {
        template <typename ...T, typename N>
        static constexpr auto
        apply(hana::experimental::types<T...> const&, N const&) {
            using Indexer = types_detail::indexer<std::make_index_sequence<sizeof...(T)>, T...>;
            using Nth = typename decltype(types_detail::get_elt<N::value>(Indexer{}))::type;
            return hana::type<Nth>{};
        }
    };

    template <>
    struct is_empty_impl<hana::experimental::types_tag> {
        template <typename ...T>
        static constexpr hana::bool_<sizeof...(T) == 0>
        apply(hana::experimental::types<T...> const&)
        { return {}; }
    };

    template <>
    struct drop_front_impl<hana::experimental::types_tag> {
        template <std::size_t n, typename Indexer, std::size_t ...i>
        static hana::experimental::types<
            typename decltype(types_detail::get_elt<i + n>(Indexer{}))::type...
        > helper(std::index_sequence<i...>);

        template <typename ...T, typename N>
        static constexpr auto
        apply(hana::experimental::types<T...> const&, N const&) {
            constexpr std::size_t n = N::value > sizeof...(T) ? sizeof...(T) : N::value;
            using Indices = std::make_index_sequence<sizeof...(T) - n>;
            using Indexer = types_detail::indexer<std::make_index_sequence<sizeof...(T)>, T...>;
            return decltype(helper<n, Indexer>(Indices{})){};
        }
    };

    // Searchable
    template <>
    struct contains_impl<hana::experimental::types_tag> {
        template <typename U>
        struct is_same_as {
            template <typename T>
            struct apply {
                static constexpr bool value = std::is_same<U, T>::value;
            };
        };

        template <typename ...T, typename U>
        static constexpr auto apply(hana::experimental::types<T...> const&, U const&)
            -> hana::bool_<
                detail::any_of<is_same_as<typename U::type>::template apply, T...>::value
            >
        { return {}; }

        static constexpr hana::false_ apply(...) { return {}; }
    };

    // Comparable
    template <>
    struct equal_impl<hana::experimental::types_tag, hana::experimental::types_tag> {
        template <typename Types>
        static constexpr hana::true_ apply(Types const&, Types const&)
        { return {}; }

        template <typename Ts, typename Us>
        static constexpr hana::false_ apply(Ts const&, Us const&)
        { return {}; }
    };
BOOST_HANA_NAMESPACE_END

#endif // !BOOST_HANA_EXPERIMENTAL_TYPES_HPP

max_stars_repo_name: EuroLine/nheqminer

max_issues_repo_name: EuroLine/nheqminer

max_forks_repo_name: EuroLine/nheqminer


text:
#pragma once

#include <string>
#ifdef WIN32
// <filesystem> not available on macOS < 10.15
#include <filesystem>
#endif

#ifdef __APPLE__
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
#else
#include <filesystem>
namespace fs = ::std::filesystem;
#endif

namespace fdn
{

typedef fs::path PathType;
PathType getConfigurationPath();

}

max_stars_repo_name: GregBakker/adobe-cc-codec-foundation

max_issues_repo_name: codec-foundation/adobe-cc

max_forks_repo_name: codec-foundation/adobe-cc


text:
// I am 31, which is very young for my age.
// That is enough to realize I’m a pencil that has learned
// how to draw the Internet. I explain squiggles
// diagramming exactly how I feel and you are drawn to read
// in ways you cannot yet. Slow goes the drag
// of creation, how what’s within comes to be without,
// which is the rhythmic erection of essence.
//   - Amy King, Wings of Desire

#ifndef IO_CORE_HPP
#define IO_CORE_HPP

#include <boost/asio.hpp>
#include <boost/asio/ts/net.hpp>
#include <botan/cipher_mode.h>
#include <cstdint>
#include <memory>
#include <string>
#include <zlib.h>

#include "event_core.hpp"
#include "minecraft_protocol.hpp"
#include "plugin_base.hpp"
#include "plugin_loader.hpp"

// I have no idea why SWIG hates namespace aliasing, but what it can't see
// won't hurt it.
#ifndef SWIG
#ifdef BOOST_ASIO_TS_NET_HPP
namespace net = boost::asio;
namespace sys = boost::system;
#endif
namespace ip = net::ip;
#endif

namespace rkr {

struct ConnectData {
  std::string host;
  std::uint16_t port;
};

class IOCore : public PluginBase {
public:
  std::uint8_t shared_secret[16];

  IOCore(rkr::PluginLoader& ploader, net::io_context& ctx,
      bool ownership = false);
  void encode_packet(const mcd::Packet& packet);
  void connect(const std::string& host, const std::string& service);

private:
  EventCore* ev;
  mcd::packet_state state {mcd::HANDSHAKING};
  bool compressed {false};
  bool encrypted {false};
  bool ongoing_write {false};
  ip::tcp::socket sock;
  ip::tcp::resolver rslv;
  boost::asio::steady_timer tick_timer;

  std::deque<boost::asio::streambuf> write_bufs;
  std::deque<boost::asio::const_buffer> out_bufs;

  boost::asio::mutable_buffer in_buf;
  boost::asio::streambuf read_buf;
  std::istream read_is {&read_buf};

  ev_id_type connect_event;
  ev_id_type kill_event;
  ev_id_type tick_event;

  std::unique_ptr<Botan::Cipher_Mode> encryptor {
      Botan::Cipher_Mode::create("AES-128/CFB/8", Botan::ENCRYPTION)};
  std::unique_ptr<Botan::Cipher_Mode> decryptor {
      Botan::Cipher_Mode::create("AES-128/CFB/8", Botan::DECRYPTION)};

  z_stream inflator {};
  z_stream deflator {};
  std::size_t threshold;

  std::array<std::array<std::vector<ev_id_type>, mcd::DIRECTION_MAX>,
      mcd::STATE_MAX>
      packet_event_ids;

  void tick();
  void read_packet();
  void write_packet(const boost::asio::streambuf& header,
      const boost::asio::streambuf& body);
  void read_header();
  void read_body(std::size_t len);
  void connect_handler(const sys::error_code& ec, const ip::tcp::endpoint& ep);
  void write_handler(const sys::error_code& ec, std::size_t len);
  void header_handler(const sys::error_code& ec, std::size_t len);
  void body_handler(
      const sys::error_code& ec, std::size_t len, int32_t body_len);
  void encryption_begin_handler(const void* data);
  void enable_encryption();
  void enable_compression(const void* data);
  void transition_state(const void* data);
  void login_success();
};

} // namespace rkr

#endif


text:
// Copyright (C) 2020 T. Zachary Laine
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Warning! This file is autogenerated.
#ifndef BOOST_TEXT_DATA_CS_HPP
#define BOOST_TEXT_DATA_CS_HPP

#include <boost/text/string_view.hpp>

namespace boost { namespace text { namespace data { namespace cs {

inline string_view standard_collation_tailoring()
{
    return string_view((char const *)
u8R"(
&C<č<<<Č
&H<ch<<<cH<<<Ch<<<CH
&R<ř<<<Ř
&S<š<<<Š
&Z<ž<<<Ž
  )");
}

}}}}

#endif

max_stars_repo_name: PyABI/PyABI

max_issues_repo_name: PyABI/PyABI

max_forks_repo_name: PyABI/PyABI


text:
#include "Logger.hpp"
#include <sstream>
#include <boost/filesystem.hpp>
#include <base-logging/Logging.hpp>

namespace templ {
namespace utils {

Logger::Logger(const base::Time& time, const std::string& baseDirectory, bool useSessions)
    : mTime(time)
    , mBaseDirectory(baseDirectory)
    , mUseSessions(useSessions)
    , mSessionId(0)
{
}

std::string Logger::filename(const std::string& filename) const
{
    std::stringstream ss;
    ss << mBaseDirectory << "/" << mTime.toString(base::Time::Seconds, "%Y%m%d_%H%M%S") << "-templ/";
    if(mUseSessions)
    {
        ss << "/" << mSessionId;
    }
    if(boost::filesystem::create_directories( boost::filesystem::path(ss.str())) )
    {
        LOG_DEBUG_S << "Created directory: '" << ss.str() << "'";
    }

    ss << "/" << filename;
    return ss.str();
}

std::string Logger::getBasePath() const
{
    std::stringstream ss;
    ss << mBaseDirectory << "/" << mTime.toString(base::Time::Seconds, "%Y%m%d_%H%M%S") << "-templ/";
    return ss.str();
}

} // end namespace utils
} // end namespace templ

max_stars_repo_name: tomcreutz/planning-templ

max_issues_repo_name: tomcreutz/planning-templ

max_forks_repo_name: tomcreutz/planning-templ


text: #include <boost/hana/intersection.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#define BOOST_TEST_MODULE "CutoffDiscretizationTest"
#include <boost/test/included/unit_test.hpp>
#include "CutoffDiscretization.hpp"

BOOST_TEST_DONT_PRINT_LOG_VALUE(CutoffIterator);

struct CutoffDiscretizationFixture
{
	CutoffDiscretizationFixture()
	{
		std::vector<float> values({ 5.0, 4.0, 3.0, 2.0, 1.0 });
		c = new CutoffDiscretization(values);
	}

	~CutoffDiscretizationFixture()
	{
		delete c;
	}

	CutoffDiscretization *c;
};

BOOST_FIXTURE_TEST_SUITE(CutoffDiscretizationTest, CutoffDiscretizationFixture);

BOOST_AUTO_TEST_CASE(iterate)
{
	CutoffIterator i = c->begin();
	BOOST_CHECK_EQUAL(*i, 5.0f);

	++i;
	BOOST_CHECK_EQUAL(*i, 4.0f);

	i = c->last();
	BOOST_CHECK_EQUAL(*i, 1.0f);

	++i;
	BOOST_CHECK_EQUAL(i, c->end());
}

BOOST_AUTO_TEST_CASE(find)
{
	CutoffIterator i = c->find(4.0f);
	BOOST_CHECK_EQUAL(*i, 4.0f);

	i = c->find(0.1f);
	BOOST_CHECK_EQUAL(i, c->end());
}

BOOST_AUTO_TEST_SUITE_END();

max_stars_repo_name: Sourin-chatterjee/SpinParser

max_issues_repo_name: Sourin-chatterjee/SpinParser

max_forks_repo_name: Sourin-chatterjee/SpinParser


text:
//          Copyright Gavin Band 2008 - 2012.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <limits>
#include <Eigen/Dense>
#include "metro/mean_and_variance.hpp"

namespace metro {
	OnlineElementwiseMeanAndVariance::Storage OnlineElementwiseMeanAndVariance::get_mean() const {
		// Return m_mean, but with NaN for counts that are zero.
		return m_mean.array() + (( m_nonmissingness.array() / m_nonmissingness.array() ) - 1 ) ;
	}

	OnlineElementwiseMeanAndVariance::Storage OnlineElementwiseMeanAndVariance::get_variance() const {
		Storage result = m_sum_of_squares_of_differences.array() / ( m_nonmissingness.array() - 1 ) ;
		// Set result to NaN when there is 0 or 1 observation.
		result.array() *= m_nonmissingness.array() / m_nonmissingness.array() ;
		result.array() *= ( m_nonmissingness.array() - 1 ) / ( m_nonmissingness.array() - 1 ) ;
		return result ;
	}

	double OnlineElementwiseMeanAndVariance::get_count( int row, int column ) const {
		return m_nonmissingness(row, column) ;
	}

	double OnlineElementwiseMeanAndVariance::get_mean( int row, int column ) const {
		// Return m_mean, but with NaN for counts that are zero.
		return m_mean(row, column) + (( m_nonmissingness(row, column) / m_nonmissingness(row, column) ) - 1 ) ;
	}

	double OnlineElementwiseMeanAndVariance::get_variance( int row, int column ) const {
		// The result is NaN unless there are at least 2 observations
		double result = std::numeric_limits< double >::quiet_NaN() ;
		if( m_nonmissingness(row, column) > 1 ) {
			result = m_sum_of_squares_of_differences(row, column) / ( m_nonmissingness(row, column) - 1 ) ;
		}
		return result ;
	}
}

max_stars_repo_name: gavinband/bingwa

max_issues_repo_name: gavinband/bingwa

max_forks_repo_name: gavinband/qctool


text:
#ifndef STAN_MATH_TORSTEN_PKMODEL_EXTRACTVECTOR_HPP
#define STAN_MATH_TORSTEN_PKMODEL_EXTRACTVECTOR_HPP

#include <Eigen/Dense>
#include <iostream>
#include <string>
#include <vector>

namespace torsten {

/**
 * Extracts the nth column or row from a matrix and
 * returns it as an std::vector. Indexing starts at 0, meaning the
 * first row or column is found for n = 0.
 *
 * @param[in]: matrix from which the row or column will be extracted
 * @param[in]: n the row or column number
 * @param[in]: str a string that specifies whether the function extracts
 *             a column or a row.
 * @return: output dvector
 */
template <typename T>
std::vector<T> ExtractVector(Eigen::Matrix<T, Eigen::Dynamic, 1>
  matrix, int n, std::string str) {
  using std::vector;
  using std::string;
  using Eigen::Matrix;
  using Eigen::Dynamic;

  assert((str == "row") || (str == "col"));
  int length;
  vector<T> ExtVec;

  if (str == "col") {  // extract a column.
    length = matrix.rows();
    assert(n < matrix.cols());
    ExtVec.resize(length);
    for (int i = 0; i < length; i++) ExtVec[i] = matrix(i, n);
  } else {  // extract a row
    length = matrix.cols();
    assert(n < matrix.rows());
    ExtVec.resize(length);
    for (int i = 0; i < length; i++) ExtVec[i] = matrix(n, i);
  }
  return ExtVec;
}

}
#endif

max_stars_repo_name: yizhang-cae/torsten

max_issues_repo_name: yizhang-cae/torsten

max_forks_repo_name: yizhang-cae/torsten


text:
/*
 *  solutionset.cpp
 *
 *   Created on: 1-oct-2015
 *       Author: M. El-Kebir
 */

#include "solutionset.h"
#include "rootedcladisticancestrygraph.h"
#include "perfectphylotree.h"
#include <boost/unordered_set.hpp>

namespace gm {

SolutionSet::SolutionSet()
  : _sol()

{
}

void SolutionSet::initSummary()
{
  _occArc.clear();
  _nodes.clear();

  // populate occArc and nodes
  int nodeIdx = 0;
  for (int solIdx = 0; solIdx < solutionCount(); ++solIdx)
  {
    const Solution& sol = _sol[solIdx];

    PerfectPhyloTree T(sol.A(), sol.S());
    T.setLabels(sol.inferredF());

    for (Digraph::ArcIt a_cidj(T.T()); a_cidj != lemon::INVALID; ++a_cidj)
    {
      Digraph::Node v_ci = T.T().source(a_cidj);
      Digraph::Node v_dj = T.T().target(a_cidj);

      // assuming that labels are ids!
      const std::string& label_ci = T.label(v_ci);
      const std::string& label_dj = T.label(v_dj);

      if (_occArc[label_ci].find(label_dj) == _occArc[label_ci].end())
      {
        _occArc[label_ci][label_dj] = 0;
      }
      ++_occArc[label_ci][label_dj];

      if (_nodes.find(label_ci) == _nodes.end())
      {
        _nodes[label_ci] = nodeIdx++;
      }
      if (_nodes.find(label_dj) == _nodes.end())
      {
        _nodes[label_dj] = nodeIdx++;
      }
    }
  }
}

void SolutionSet::assignDistancesByOccurenceCounts()
{
  initSummary();

  int solCount = _sol.size();
  for (int idx = 0; idx < solCount; ++idx)
  {
    Solution& sol = _sol[idx];

    int distance = 0;
    PerfectPhyloTree T(sol.A(), sol.S());
    T.setLabels(sol.inferredF());
    for (Digraph::ArcIt a_cidj(T.T()); a_cidj != lemon::INVALID; ++a_cidj)
    {
      Digraph::Node v_ci = T.T().source(a_cidj);
      Digraph::Node v_dj = T.T().target(a_cidj);

      // assuming that labels are ids!
      const std::string& label_ci = T.label(v_ci);
      const std::string& label_dj = T.label(v_dj);

      distance -= _occArc[label_ci][label_dj];
    }

    sol.distance() = distance;
  }
}

void SolutionSet::sort()
{
  std::sort(_sol.begin(), _sol.end(), Compare());
}

void SolutionSet::add(const SolutionSet& sols)
{
  int n = sols.solutionCount();
  for (int i = 0; i < n; ++i)
  {
    add(sols.solution(i));
  }
}

void SolutionSet::add(const Solution& sol)
{
  _sol.push_back(sol);
}

int SolutionSet::unique()
{
  typedef std::pair<IntPair, IntPair> IntPairPair;
  typedef std::set<IntPairPair> ArcSet;
  typedef std::set<ArcSet> ArcSetSet;

  int res = 0;
  ArcSetSet AS;
  for (SolutionVectorNonConstIt it = _sol.begin(); it != _sol.end();)
  {
    const Solution& sol = *it;
    PerfectPhyloTree T(sol.A(), sol.S());
    const Digraph& TT = T.T();

    ArcSet arcs;
    for (Digraph::ArcIt a_cidj(TT); a_cidj != lemon::INVALID; ++a_cidj)
    {
      Digraph::Node v_ci = TT.source(a_cidj);
      Digraph::Node v_dj = TT.target(a_cidj);

      const IntPair& ci = T.nodeToCharState(v_ci);
      const IntPair& dj = T.nodeToCharState(v_dj);

      arcs.insert(std::make_pair(ci, dj));
    }

    if (AS.find(arcs) == AS.end())
    {
      AS.insert(arcs);
      ++it;
    }
    else
    {
      it = _sol.erase(it);
      ++res;
    }
  }
  return res;
}

std::ostream& operator<<(std::ostream& out, const SolutionSet& sols)
{
  int solCount = sols.solutionCount();
  out << solCount << " # solutions" << std::endl << std::endl;

  for (int idx = 0; idx < solCount; ++idx)
  {
    out << sols.solution(idx);
  }

  return out;
}

std::istream& operator>>(std::istream& in, SolutionSet& sols)
{
  int solCount = -1;

  std::string line;
  getline(in, line);
  std::stringstream ss(line);
  ss >> solCount;

  getline(in, line);

  if (solCount <= 0)
  {
    throw std::runtime_error(getLineNumber() + "Error: solCount should be nonnegative");
  }

  sols._sol.clear();
  for (int idx = 0; idx < solCount; ++idx)
  {
    Solution sol;
    in >> sol;
    sols.add(sol);

    getline(in, line);
  }

  return in;
}

} // namespace gm

max_stars_repo_name: elkebir-group/OncoSim

max_issues_repo_name: elkebir-group/OncoSim

max_forks_repo_name: elkebir-group/OncoSim


text:
/*
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * Copyright (c) 2020 Andrey Semashev
 */
/*!
 * \file   atomic/detail/wait_caps_dragonfly_umtx.hpp
 *
 * This header defines waiting/notifying operations capabilities macros.
 */

#ifndef BOOST_ATOMIC_DETAIL_WAIT_CAPS_DRAGONFLY_UMTX_HPP_INCLUDED_
#define BOOST_ATOMIC_DETAIL_WAIT_CAPS_DRAGONFLY_UMTX_HPP_INCLUDED_

#include <boost/atomic/detail/config.hpp>
#include <boost/atomic/detail/capabilities.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

// DragonFly BSD umtx_sleep/umtx_wakeup use physical address to the atomic object as a key, which means it should support address-free operations.
// https://man.dragonflybsd.org/?command=umtx&section=2

#define BOOST_ATOMIC_HAS_NATIVE_INT32_WAIT_NOTIFY BOOST_ATOMIC_INT32_LOCK_FREE
#define BOOST_ATOMIC_HAS_NATIVE_INT32_IPC_WAIT_NOTIFY BOOST_ATOMIC_INT32_LOCK_FREE

#endif // BOOST_ATOMIC_DETAIL_WAIT_CAPS_DRAGONFLY_UMTX_HPP_INCLUDED_

max_stars_repo_name: kindlychung/mediasoup-sfu-cpp

max_issues_repo_name: kindlychung/mediasoup-sfu-cpp

max_forks_repo_name: kindlychung/mediasoup-sfu-cpp


text:
// boost/math/distributions/arcsine.hpp

// Copyright John Maddock 2014.
// Copyright Paul A. Bristow 2014.

// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

// http://en.wikipedia.org/wiki/arcsine_distribution

// The arcsine Distribution is a continuous probability distribution.
// http://en.wikipedia.org/wiki/Arcsine_distribution
// http://www.wolframalpha.com/input/?i=ArcSinDistribution

// Standard arcsine distribution is a special case of beta distribution with both a & b = one half,
// and 0 <= x <= 1.

// It is generalized to include any bounded support a <= x <= b from 0 <= x <= 1
// by Wolfram and Wikipedia,
// but using location and scale parameters by
// Virtual Laboratories in Probability and Statistics http://www.math.uah.edu/stat/index.html
// http://www.math.uah.edu/stat/special/Arcsine.html
// The end-point version is simpler and more obvious, so we implement that.
// TODO Perhaps provide location and scale functions?

#ifndef BOOST_MATH_DIST_ARCSINE_HPP
#define BOOST_MATH_DIST_ARCSINE_HPP

#include <boost/math/distributions/fwd.hpp>
#include <boost/math/distributions/complement.hpp> // complements.
#include <boost/math/distributions/detail/common_error_handling.hpp> // error checks.
#include <boost/math/constants/constants.hpp>

#include <boost/math/special_functions/fpclassify.hpp> // isnan.

#if defined (BOOST_MSVC)
#  pragma warning(push)
#  pragma warning(disable: 4702) // Unreachable code,
// in domain_error_imp in error_handling.
#endif

#include <utility>
#include <exception>  // For std::domain_error.

namespace boost
{
  namespace math
  {
    namespace arcsine_detail
    {
      // Common error checking routines for arcsine distribution functions:
      // Duplicating for x_min and x_max provides specific error messages.
      template <class RealType, class Policy>
      inline bool check_x_min(const char* function, const RealType& x, RealType* result, const Policy& pol)
      {
        if (!(boost::math::isfinite)(x))
        {
          *result = policies::raise_domain_error<RealType>(
            function,
            "x_min argument is %1%, but must be finite !", x, pol);
          return false;
        }
        return true;
      } // bool check_x_min

      template <class RealType, class Policy>
      inline bool check_x_max(const char* function, const RealType& x, RealType* result, const Policy& pol)
      {
        if (!(boost::math::isfinite)(x))
        {
          *result = policies::raise_domain_error<RealType>(
            function,
            "x_max argument is %1%, but must be finite !", x, pol);
          return false;
        }
        return true;
      } // bool check_x_max

      template <class RealType, class Policy>
      inline bool check_x_minmax(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
      { // Check x_min < x_max
        if (x_min >= x_max)
        {
          std::string msg = "x_max argument is %1%, but must be > x_min = " + lexical_cast<std::string>(x_min) + "!";
          *result = policies::raise_domain_error<RealType>(
            function,
            msg.c_str(), x_max, pol);
           // "x_max argument is %1%, but must be > x_min !", x_max, pol);
            //  "x_max argument is %1%, but must be > x_min %2!", x_max, x_min, pol); would be better.
          // But would require replication of all helpers functions in /policies/error_handling.hpp for two values,
          // as well as two value versions of raise_error, raise_domain_error and do_format ...
          // so use slightly hacky lexical_cast to string instead.
          return false;
        }
        return true;
      } // bool check_x_minmax

      template <class RealType, class Policy>
      inline bool check_prob(const char* function, const RealType& p, RealType* result, const Policy& pol)
      {
        if ((p < 0) || (p > 1) || !(boost::math::isfinite)(p))
        {
          *result = policies::raise_domain_error<RealType>(
            function,
            "Probability argument is %1%, but must be >= 0 and <= 1 !", p, pol);
          return false;
        }
        return true;
      } // bool check_prob

      template <class RealType, class Policy>
      inline bool check_x(const char* function, const RealType& x_min, const RealType& x_max, const RealType& x, RealType* result, const Policy& pol)
      { // Check x finite and x_min < x < x_max.
        if (!(boost::math::isfinite)(x))
        {
          *result = policies::raise_domain_error<RealType>(
            function,
            "x argument is %1%, but must be finite !", x, pol);
          return false;
        }
        if ((x < x_min) || (x > x_max))
        {
          // std::cout << x_min << ' ' << x << x_max << std::endl;
          *result = policies::raise_domain_error<RealType>(
            function,
            "x argument is %1%, but must be x_min < x < x_max !", x, pol);
          // For example:
          // Error in function boost::math::pdf(arcsine_distribution<double> const&, double) : x argument is -1.01, but must be x_min < x < x_max !
          // TODO Perhaps show values of x_min and x_max?
          return false;
        }
        return true;
      } // bool check_x

      template <class RealType, class Policy>
      inline bool check_dist(const char* function, const RealType& x_min, const RealType& x_max, RealType* result, const Policy& pol)
      { // Check both x_min and x_max finite, and x_min  < x_max.
        return check_x_min(function, x_min, result, pol)
            && check_x_max(function, x_max, result, pol)
            && check_x_minmax(function, x_min, x_max, result, pol);
      } // bool check_dist

      template <class RealType, class Policy>
      inline bool check_dist_and_x(const char* function, const RealType& x_min, const RealType& x_max, RealType x, RealType* result, const Policy& pol)
      {
        return check_dist(function, x_min, x_max, result, pol)
          && arcsine_detail::check_x(function, x_min, x_max, x, result, pol);
      } // bool check_dist_and_x

      template <class RealType, class Policy>
      inline bool check_dist_and_prob(const char* function, const RealType& x_min, const RealType& x_max, RealType p, RealType* result, const Policy& pol)
      {
        return check_dist(function, x_min, x_max, result, pol)
          && check_prob(function, p, result, pol);
      } // bool check_dist_and_prob

    } // namespace arcsine_detail

    template <class RealType = double, class Policy = policies::policy<> >
    class arcsine_distribution
    {
    public:
      typedef RealType value_type;
      typedef Policy policy_type;

      arcsine_distribution(RealType x_min = 0, RealType x_max = 1) : m_x_min(x_min), m_x_max(x_max)
      { // Default beta (alpha = beta = 0.5) is standard arcsine with x_min = 0, x_max = 1.
        // Generalized to allow x_min and x_max to be specified.
        RealType result;
        arcsine_detail::check_dist(
          "boost::math::arcsine_distribution<%1%>::arcsine_distribution",
          m_x_min,
          m_x_max,
          &result, Policy());
      } // arcsine_distribution constructor.
      // Accessor functions:
      RealType x_min() const
      {
        return m_x_min;
      }
      RealType x_max() const
      {
        return m_x_max;
      }

    private:
      RealType m_x_min; // Two x min and x max parameters of the arcsine distribution.
      RealType m_x_max;
    }; // template <class RealType, class Policy> class arcsine_distribution

    // Convenient typedef to construct double version.
    typedef arcsine_distribution<double> arcsine;

    template <class RealType, class Policy>
    inline const std::pair<RealType, RealType> range(const arcsine_distribution<RealType, Policy>&  dist)
    { // Range of permissible values for random variable x.
      using boost::math::tools::max_value;
      return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
    }

    template <class RealType, class Policy>
    inline const std::pair<RealType, RealType> support(const arcsine_distribution<RealType, Policy>&  dist)
    { // Range of supported values for random variable x.
      // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
      return std::pair<RealType, RealType>(static_cast<RealType>(dist.x_min()), static_cast<RealType>(dist.x_max()));
    }

    template <class RealType, class Policy>
    inline RealType mean(const arcsine_distribution<RealType, Policy>& dist)
    { // Mean of arcsine distribution .
      RealType result;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();

      if (false == arcsine_detail::check_dist(
        "boost::math::mean(arcsine_distribution<%1%> const&, %1% )",
        x_min,
        x_max,
        &result, Policy())
        )
      {
        return result;
      }
      return  (x_min + x_max) / 2;
    } // mean

    template <class RealType, class Policy>
    inline RealType variance(const arcsine_distribution<RealType, Policy>& dist)
    { // Variance of standard arcsine distribution = (1-0)/8 = 0.125.
      RealType result;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();
      if (false == arcsine_detail::check_dist(
        "boost::math::variance(arcsine_distribution<%1%> const&, %1% )",
        x_min,
        x_max,
        &result, Policy())
        )
      {
        return result;
      }
      return  (x_max - x_min) * (x_max - x_min) / 8;
    } // variance

    template <class RealType, class Policy>
    inline RealType mode(const arcsine_distribution<RealType, Policy>& /* dist */)
    { //There are always [*two] values for the mode, at ['x_min] and at ['x_max], default 0 and 1,
      // so instead we raise the exception domain_error.
      return policies::raise_domain_error<RealType>(
        "boost::math::mode(arcsine_distribution<%1%>&)",
        "The arcsine distribution has two modes at x_min and x_max: "
        "so the return value is %1%.",
        std::numeric_limits<RealType>::quiet_NaN(), Policy());
    } // mode

    template <class RealType, class Policy>
    inline RealType median(const arcsine_distribution<RealType, Policy>& dist)
    { // Median of arcsine distribution (a + b) / 2 == mean.
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();
      RealType result;
      if (false == arcsine_detail::check_dist(
        "boost::math::median(arcsine_distribution<%1%> const&, %1% )",
        x_min,
        x_max,
        &result, Policy())
        )
      {
        return result;
      }
      return  (x_min + x_max) / 2;
    }

    template <class RealType, class Policy>
    inline RealType skewness(const arcsine_distribution<RealType, Policy>& dist)
    {
      RealType result;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();

      if (false == arcsine_detail::check_dist(
        "boost::math::skewness(arcsine_distribution<%1%> const&, %1% )",
        x_min,
        x_max,
        &result, Policy())
        )
      {
        return result;
      }
      return 0;
    } // skewness

    template <class RealType, class Policy>
    inline RealType kurtosis_excess(const arcsine_distribution<RealType, Policy>& dist)
    {
      RealType result;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();

      if (false == arcsine_detail::check_dist(
        "boost::math::kurtosis_excess(arcsine_distribution<%1%> const&, %1% )",
        x_min,
        x_max,
        &result, Policy())
        )
      {
        return result;
      }
      result = -3;
      return  result / 2;
    } // kurtosis_excess

    template <class RealType, class Policy>
    inline RealType kurtosis(const arcsine_distribution<RealType, Policy>& dist)
    {
      RealType result;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();

      if (false == arcsine_detail::check_dist(
        "boost::math::kurtosis(arcsine_distribution<%1%> const&, %1% )",
        x_min,
        x_max,
        &result, Policy())
        )
      {
        return result;
      }

      return 3 + kurtosis_excess(dist);
    } // kurtosis

    template <class RealType, class Policy>
    inline RealType pdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& xx)
    { // Probability Density/Mass Function arcsine.
      BOOST_FPU_EXCEPTION_GUARD
      BOOST_MATH_STD_USING // For ADL of std functions.

      static const char* function = "boost::math::pdf(arcsine_distribution<%1%> const&, %1%)";

      RealType lo = dist.x_min();
      RealType hi = dist.x_max();
      RealType x = xx;

      // Argument checks:
      RealType result = 0;
      if (false == arcsine_detail::check_dist_and_x(
        function,
        lo, hi, x,
        &result, Policy()))
      {
        return result;
      }
      using boost::math::constants::pi;
      result = static_cast<RealType>(1) / (pi<RealType>() * sqrt((x - lo) * (hi - x)));
      return result;
    } // pdf

    template <class RealType, class Policy>
    inline RealType cdf(const arcsine_distribution<RealType, Policy>& dist, const RealType& x)
    { // Cumulative Distribution Function arcsine.
      BOOST_MATH_STD_USING // For ADL of std functions.

      static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";

      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();

      // Argument checks:
      RealType result = 0;
      if (false == arcsine_detail::check_dist_and_x(
        function,
        x_min, x_max, x,
        &result, Policy()))
      {
        return result;
      }
      // Special cases:
      if (x == x_min)
      {
        return 0;
      }
      else if (x == x_max)
      {
        return 1;
      }
      using boost::math::constants::pi;
      result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
      return result;
    } // arcsine cdf

    template <class RealType, class Policy>
    inline RealType cdf(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
    { // Complemented Cumulative Distribution Function arcsine.
      BOOST_MATH_STD_USING // For ADL of std functions.
      static const char* function = "boost::math::cdf(arcsine_distribution<%1%> const&, %1%)";

      RealType x = c.param;
      arcsine_distribution<RealType, Policy> const& dist = c.dist;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();

      // Argument checks:
      RealType result = 0;
      if (false == arcsine_detail::check_dist_and_x(
        function,
        x_min, x_max, x,
        &result, Policy()))
      {
        return result;
      }
      if (x == x_min)
      {
        return 0;
      }
      else if (x == x_max)
      {
        return 1;
      }
      using boost::math::constants::pi;
      // Naive version x = 1 - x;
      // result = static_cast<RealType>(2) * asin(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
      // is less accurate, so use acos instead of asin for complement.
      result = static_cast<RealType>(2) * acos(sqrt((x - x_min) / (x_max - x_min))) / pi<RealType>();
      return result;
    } // arcsine ccdf

    template <class RealType, class Policy>
    inline RealType quantile(const arcsine_distribution<RealType, Policy>& dist, const RealType& p)
    {
      // Quantile or Percent Point arcsine function or
      // Inverse Cumulative probability distribution function CDF.
      // Return x (0 <= x <= 1),
      // for a given probability p (0 <= p <= 1).
      // These functions take a probability as an argument
      // and return a value such that the probability that a random variable x
      // will be less than or equal to that value
      // is whatever probability you supplied as an argument.
      BOOST_MATH_STD_USING // For ADL of std functions.

      using boost::math::constants::half_pi;

      static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";

      RealType result = 0; // of argument checks:
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();
      if (false == arcsine_detail::check_dist_and_prob(
        function,
        x_min, x_max, p,
        &result, Policy()))
      {
        return result;
      }
      // Special cases:
      if (p == 0)
      {
        return 0;
      }
      if (p == 1)
      {
        return 1;
      }

      RealType sin2hpip = sin(half_pi<RealType>() * p);
      RealType sin2hpip2 = sin2hpip * sin2hpip;
      result = -x_min * sin2hpip2 + x_min + x_max * sin2hpip2;

      return result;
    } // quantile

    template <class RealType, class Policy>
    inline RealType quantile(const complemented2_type<arcsine_distribution<RealType, Policy>, RealType>& c)
    {
      // Complement Quantile or Percent Point arcsine function.
      // Return the number of expected x for a given
      // complement of the probability q.
      BOOST_MATH_STD_USING // For ADL of std functions.

      using boost::math::constants::half_pi;
      static const char* function = "boost::math::quantile(arcsine_distribution<%1%> const&, %1%)";

      // Error checks:
      RealType q = c.param;
      const arcsine_distribution<RealType, Policy>& dist = c.dist;
      RealType result = 0;
      RealType x_min = dist.x_min();
      RealType x_max = dist.x_max();
      if (false == arcsine_detail::check_dist_and_prob(
        function,
        x_min,
        x_max,
        q,
        &result, Policy()))
      {
        return result;
      }
      // Special cases:
      if (q == 1)
      {
        return 0;
      }
      if (q == 0)
      {
        return 1;
      }
      // Naive RealType p = 1 - q; result = sin(half_pi<RealType>() * p); loses accuracy, so use a cos alternative instead.
      //result = cos(half_pi<RealType>() * q); // for arcsine(0,1)
      //result = result * result;
      // For generalized arcsine:
      RealType cos2hpip = cos(half_pi<RealType>() * q);
      RealType cos2hpip2 = cos2hpip * cos2hpip;
      result = -x_min * cos2hpip2 + x_min + x_max * cos2hpip2;

      return result;
    } // Quantile Complement

  } // namespace math
} // namespace boost

// This include must be at the end, *after* the accessors
// for this distribution have been defined, in order to
// keep compilers that support two-phase lookup happy.
#include <boost/math/distributions/detail/derived_accessors.hpp>

#if defined (BOOST_MSVC)
# pragma warning(pop)
#endif

#endif // BOOST_MATH_DIST_ARCSINE_HPP


text:
/***************************************************
/ Synchronous Server Program
/ @author Cody Martin
/ Using Boost 1.69.0 C++ Library.
/ Server processes and outputs all data from Client
/**************************************************/

#include <boost/asio.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;
using namespace boost::asio;
using namespace boost::asio::ip;

/***************************************************************************
/ Reads Client's messages through streambuf and socket until newline char
/ Typecasts data to character string and returns data
/ @param socket: Provides reference to interface communication with Client
/**************************************************************************/
string getData(tcp::socket &socket)
{
    	boost::asio::streambuf dataBuffer;
   	read_until(socket, dataBuffer, "\n");
    	string data = buffer_cast<const char*>(dataBuffer.data());

    	return data;
}

/********************************************************************
/ Function template to write data of a generic type to a given file
/ @param fileName: output text file name
/ @param data: generic data message to be written to file
/*******************************************************************/
template <class T>
void writeFile(char fileName[], T data)
{
	ofstream ofFile;

	//fstream::app activates append mode on file
	ofFile.open(fileName, fstream::app);
	if(ofFile.is_open())
	{
		ofFile << data << endl;
	}
	else cout << "Failed to open file";
	ofFile.close();
}

/***********************************************************
/ Checks to see if file exists, then deletes existing file
/ @param fileName: output text file name
/**********************************************************/
void deleteFile(char fileName[])
{
	ifstream iFile(fileName);
	if(iFile)
	{
		remove(fileName);
	}
}

/******************************************************************
/ Basic parser that extracts the numbers of a data message string
/ and parses them from a string to an integer.
/ Outputs parsed data to terminal and a text file.
/ @param fileName: output text file name
/ @param rawData: string data to be parsed into integer
/*****************************************************************/
void parseToFile(char fileName[], string rawData)
{
    	int parsedData;
	ofstream ofFile;

    	istringstream issData(rawData);
   	string singleObject;

	//uses istringstream and getline to input into singleObject
	//delimited by a space
	cout << "Parsed Data (string -> int):\n";
    	while(getline(issData, singleObject, ' '))
    	{
        	try
        	{
            		parsedData = stoi(singleObject, nullptr, 10);
            		cout << parsedData << endl;

			writeFile(fileName, parsedData);
        	}
        	catch(...)
        	{
            		cout << "Invalid input: " << singleObject << endl;
        	}
    	}
}

/*
/ Main program that sets up socket connection for Server
/ and receives data from Client to be processed
/ Desc:	Sets up io_service to use Boost functions
/	Sets up acceptor to listen for a connection from Client on port 1234
/	Sets up socket connection for server
/	Server receives values from Client for id, data, and password
/	Terminal output is carried about by terminalOutput variable
/*/
int main()
{
    	io_service io_service;
    	tcp::acceptor serverAcceptor( io_service, tcp::endpoint(tcp::v4(), 1234));
    	tcp::socket serverSocket(io_service);

    	serverAcceptor.accept(serverSocket);

    	string terminalOut, id, data, password;

	//File needs to be deleted at the beginning of each run of the program
	//to delete data from previous program run.
	char outFileName[] = "userMessages.txt";
	deleteFile(outFileName);

	//For each string variable that is read from Client, there is a newline
	//character that needs to be removed before processing data.
    	id = getData(serverSocket);
	id.pop_back();				//deletes "\n" from data
    	terminalOut = "ID Message:\n" + id;
    	cout << terminalOut << endl;
	writeFile(outFileName, id);

	password = getData(serverSocket);
	password.pop_back();
	terminalOut = "Password Message:\n" + password;
	cout << terminalOut << endl;
	writeFile(outFileName, password);

	//Previous iteration of file should be deleted before processing
	char parsedFile[] = "parsedData.txt";
	deleteFile(parsedFile);

	//Infinite loop to read data messages from Client and parses data
	//"exit" is used to leave the loop
	while(true)
	{
		data = getData(serverSocket);
		data.pop_back();
		terminalOut = "Data Message:\n" + data;
		cout << terminalOut << endl;

		if(data == "exit")
			break;
		else
		{
			writeFile(outFileName, data);
			parseToFile(parsedFile, data);
		}
	}

    	return 0;
}

max_stars_repo_name: ExtinctStar/boostnetworking

max_issues_repo_name: ExtinctStar/boostnetworking

max_forks_repo_name: ExtinctStar/boostnetworking