Skip to content

Commit

Permalink
Move opt_to_str to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov-Dmitriy-Ivanovich committed Nov 29, 2023
1 parent 01e2661 commit c83beef
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 65 deletions.
69 changes: 69 additions & 0 deletions src/python_bindings/opt_to_str.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once
#include "opt_to_str.h"
namespace {
using ConvFunction = std::function<std::string(boost::any)>;

template <typename T>
std::pair<std::type_index, ConvFunction> normal_conv_pair{
std::type_index(typeid(T)),
[](boost::any value) { return std::to_string(boost::any_cast<T>(value)); }
};

template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<bool>{
std::type_index(typeid(bool)),
[](boost::any value) { return boost::any_cast<bool>(value) ? "True" : "False"; }
};

template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<config::IndicesType>{
std::type_index(typeid(config::IndicesType)),
[](boost::any value) {
auto opt_value = (boost::any_cast<config::IndicesType>(value));
if (opt_value.size() == 0) {
return std::string("[]");
}
std::string str_value = "[";
for (int i = 0; i < opt_value.size() - 1; i++) {
str_value += std::to_string(opt_value.at(i)) + ", ";
}
str_value += std::to_string(opt_value.back()) + "]";
return str_value;
}
};
template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<algos::metric::MetricAlgo>{
std::type_index(typeid(algos::metric::MetricAlgo)),
[](boost::any value){
auto opt_value = boost::any_cast<algos::metric::MetricAlgo>(value);
return opt_value._to_string();
}
};
template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<algos::metric::Metric>{
std::type_index(typeid(algos::metric::Metric)),
[](boost::any value){
auto opt_value = boost::any_cast<algos::metric::Metric>(value);
return opt_value._to_string();
}
};
const std::unordered_map<std::type_index, ConvFunction> converters{
normal_conv_pair<int>,
normal_conv_pair<double>,
normal_conv_pair<long double>,
normal_conv_pair<unsigned int>,
normal_conv_pair<bool>,
normal_conv_pair<config::ThreadNumType>,
normal_conv_pair<config::MaxLhsType>,
normal_conv_pair<config::ErrorType>,
normal_conv_pair<config::IndicesType>,
normal_conv_pair<algos::metric::MetricAlgo>,
normal_conv_pair<algos::metric::Metric>
};
} // namespace

namespace python_bindings{
std::string opt_to_str(std::type_index type, boost::any val) {
return converters.at(type)(val);
}
} // namespace python_bindings
24 changes: 24 additions & 0 deletions src/python_bindings/opt_to_str.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <memory>
#include <type_traits>
#include <unordered_map>
#include <vector>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "algorithms/algorithm.h"
#include "algorithms/metric/enums.h"
#include "config/equal_nulls/type.h"
#include "config/error/type.h"
#include "config/indices/type.h"
#include "config/max_lhs/type.h"
#include "config/tabular_data/input_table_type.h"
#include "config/thread_number/type.h"
#include "config/names.h"
#include "model/types/types.h"

namespace python_bindings{
std::string opt_to_str(std::type_index type, boost::any val);
} // namespace python_bindings
68 changes: 3 additions & 65 deletions src/python_bindings/py_algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,10 @@
#include "config/max_lhs/type.h"
#include "config/tabular_data/input_table_type.h"
#include "config/thread_number/type.h"
#include "config/names.h"
#include "model/types/types.h"
namespace {
using ConvFunction = std::function<std::string(boost::any)>;
#include "opt_to_str.h"

template <typename T>
std::pair<std::type_index, ConvFunction> normal_conv_pair{
std::type_index(typeid(T)),
[](boost::any value) { return std::to_string(boost::any_cast<T>(value)); }
};

template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<bool>{
std::type_index(typeid(bool)),
[](boost::any value) { return boost::any_cast<bool>(value) ? "True" : "False"; }
};

template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<config::IndicesType>{
std::type_index(typeid(config::IndicesType)),
[](boost::any value) {
auto opt_value = (boost::any_cast<config::IndicesType>(value));
if (opt_value.size() == 0) {
return std::string("[]");
}
std::string str_value = "[";
for (int i = 0; i < opt_value.size() - 1; i++) {
str_value += std::to_string(opt_value.at(i)) + ", ";
}
str_value += std::to_string(opt_value.back()) + "]";
return str_value;
}
};
template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<algos::metric::MetricAlgo>{
std::type_index(typeid(algos::metric::MetricAlgo)),
[](boost::any value){
auto opt_value = boost::any_cast<algos::metric::MetricAlgo>(value);
return opt_value._to_string();
}
};
template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<algos::metric::Metric>{
std::type_index(typeid(algos::metric::Metric)),
[](boost::any value){
auto opt_value = boost::any_cast<algos::metric::Metric>(value);
return opt_value._to_string();
}
};
const std::unordered_map<std::type_index, ConvFunction> converters{
normal_conv_pair<int>,
normal_conv_pair<double>,
normal_conv_pair<long double>,
normal_conv_pair<unsigned int>,
normal_conv_pair<bool>,
normal_conv_pair<config::ThreadNumType>,
normal_conv_pair<config::MaxLhsType>,
normal_conv_pair<config::ErrorType>,
normal_conv_pair<config::IndicesType>,
normal_conv_pair<algos::metric::MetricAlgo>,
normal_conv_pair<algos::metric::Metric>
};

std::string opt_to_str(std::type_index type, boost::any val) {
return converters.at(type)(val);
}
} // namespace
namespace python_bindings {

class PyAlgorithmBase {
Expand Down Expand Up @@ -123,7 +61,7 @@ class PyAlgorithmBase {
auto opt_ = algorithm_->GetOptValues();
std::unordered_map<std::string, std::string> res;
for (const auto& [name,value] : opt_) {
if (name == "table" || name == "input_format") {
if (name == config::names::kTable || name == config::names::kInputFormat) {
continue;
}
res[std::string(name)] = opt_to_str(value->type, value->value);
Expand Down

0 comments on commit c83beef

Please sign in to comment.