Skip to content

Commit

Permalink
removed static from string-enum map
Browse files Browse the repository at this point in the history
- added macro to add enums
- cleanup error messages
  • Loading branch information
gf712 committed Feb 21, 2019
1 parent 0ff60d7 commit 4fd2301
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
15 changes: 5 additions & 10 deletions src/interfaces/swig/SGBase.i
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,14 @@ namespace shogun
std::vector<std::string> param_options(const std::string& name) const {
std::vector<std::string> result;

auto enum_to_string_map = $self->get_enum_to_string_map();
auto param_to_enum_map = $self->get_string_to_enum_map();

if (enum_to_string_map.find($self->get_name()) == enum_to_string_map.end())
SG_SERROR("There are no parameter options for the class '%s'", $self->get_name());
if (param_to_enum_map.find(name) == param_to_enum_map.end())
SG_SERROR("There are no options for the parameter '%s'", name.c_str());

std::unordered_map<std::string, std::unordered_map<std::string, machine_int_t>> class_to_param = enum_to_string_map[$self->get_name()];
auto string_to_enum_map = param_to_enum_map[name];

if (class_to_param.find(name) == class_to_param.end())
SG_SERROR("There is no enum mapping for the parameter '%s'", name.c_str());

std::unordered_map<std::string, machine_int_t> string_to_enum = class_to_param[name];

for (auto const& each: string_to_enum)
for (auto const& each: string_to_enum_map)
result.push_back(each.first);

return result;
Expand Down
4 changes: 1 addition & 3 deletions src/shogun/base/SGObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,4 @@ void CSGObject::push_back(CDynamicObjectArray* array, CSGObject* value)
{
ASSERT(array);
array->push_back(value);
}

stringToEnumMapType CSGObject::m_enum_to_string_map;
}
37 changes: 22 additions & 15 deletions src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include <shogun/lib/parameter_observers/ObservedValue.h>
#include <shogun/lib/tag.h>

#include <utility>
#include <vector>
#include <map>
#include <unordered_map>
#include <utility>
#include <vector>

/** \namespace shogun
* @brief all of classes and functions are contained in the shogun namespace
Expand All @@ -50,7 +50,7 @@ struct TParameter;
template <class T> class DynArray;
template <class T> class SGStringList;

using stringToEnumMapType = std::unordered_map<std::string, std::unordered_map<std::string, std::unordered_map<std::string, machine_int_t>>>;
using stringToEnumMapType = std::unordered_map<std::string, std::unordered_map<std::string, machine_int_t>>;

/*******************************************************************************
* define reference counter macros
Expand Down Expand Up @@ -103,6 +103,16 @@ using stringToEnumMapType = std::unordered_map<std::string, std::unordered_map<s

#define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__)

#define VALUE_TO_STRING_MACRO(s) #s

#define SG_ADD_OPTION(param_name, enum_value) \
{ \
static_assert( \
std::is_enum<decltype(enum_value)>::value, "Expected an enum!"); \
m_string_to_enum_map[param_name][VALUE_TO_STRING_MACRO(enum_value)] = \
enum_value; \
}

/*******************************************************************************
* End of macros for registering parameter properties
******************************************************************************/
Expand Down Expand Up @@ -401,18 +411,15 @@ class CSGObject
template <class T>
void put(const std::string& name, const std::string& value) noexcept(false)
{
if (m_enum_to_string_map.find(get_name()) == m_enum_to_string_map.end())
SG_ERROR("There is no enum mapping for the %s", get_name());

auto string_to_enum_map = m_enum_to_string_map[get_name()];

if (string_to_enum_map.find(name) == string_to_enum_map.end())
SG_ERROR("There is no enum mapping for the parameter %s::%s", get_name(), name);
if (m_string_to_enum_map.find(name) == m_string_to_enum_map.end()) {
SG_ERROR("There are no options for the parameter %s::%s", get_name(), name);
}

auto string_to_enum = string_to_enum_map[name];
auto string_to_enum = m_string_to_enum_map[name];

if (string_to_enum.find(value) == string_to_enum.end())
if (string_to_enum.find(value) == string_to_enum.end()) {
SG_ERROR("There is no enum mapping for '%s' in %s::%s", value, get_name(), name);
}

machine_int_t enum_value = string_to_enum[value];

Expand Down Expand Up @@ -625,9 +632,9 @@ class CSGObject
void list_observable_parameters();

/** Get string to enum mapping */
stringToEnumMapType get_enum_to_string_map() const
stringToEnumMapType get_string_to_enum_map() const
{
return m_enum_to_string_map;
return m_string_to_enum_map;
}

protected:
Expand Down Expand Up @@ -908,7 +915,7 @@ class CSGObject
const std::string& name, const SG_OBS_VALUE_TYPE type,
const std::string& description);

static stringToEnumMapType m_enum_to_string_map;
stringToEnumMapType m_string_to_enum_map;

private:
void push_back(CDynamicObjectArray* array, CSGObject* value);
Expand Down
7 changes: 3 additions & 4 deletions src/shogun/regression/svr/LibLinearRegression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ void CLibLinearRegression::register_parameters()
SG_ADD(
(machine_int_t*)&m_liblinear_regression_type,
"liblinear_regression_type", "Type of LibLinear regression.");
m_enum_to_string_map[get_name()]["liblinear_regression_type"] = {
{"L2R_L2LOSS_SVR", 0},
{"L2R_L1LOSS_SVR_DUAL", 1},
{"L2R_L2LOSS_SVR_DUAL", 2}};
SG_ADD_OPTION("liblinear_regression_type", L2R_L2LOSS_SVR);
SG_ADD_OPTION("liblinear_regression_type", L2R_L1LOSS_SVR_DUAL);
SG_ADD_OPTION("liblinear_regression_type", L2R_L2LOSS_SVR_DUAL);
}

CLibLinearRegression::~CLibLinearRegression()
Expand Down

0 comments on commit 4fd2301

Please sign in to comment.