Skip to content

Commit

Permalink
prepared parameter map for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Dec 28, 2011
1 parent fd27f86 commit b5aaa46
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 22 deletions.
92 changes: 73 additions & 19 deletions src/shogun/base/ParameterMap.cpp
Expand Up @@ -9,6 +9,7 @@
*/

#include <shogun/base/ParameterMap.h>
#include <shogun/base/Parameter.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;
Expand All @@ -18,52 +19,95 @@ SGParamInfo::SGParamInfo()
init();
}

SGParamInfo::SGParamInfo(const SGParamInfo& orig)
{
init();

/* copy name */
m_name=strdup(orig.m_name);

m_ctype=orig.m_ctype;
m_stype=orig.m_stype;
m_ptype=orig.m_ptype;
m_param_version=orig.m_param_version;
}

SGParamInfo::SGParamInfo(const char* name, EContainerType ctype,
EStructType stype, EPrimitiveType ptype)
EStructType stype, EPrimitiveType ptype, int32_t param_version)
{
init();

/* copy name */
m_name=SG_MALLOC(char, strlen(name)+1);
strcpy(m_name, name);
m_name=strdup(name);

m_ctype=ctype;
m_stype=stype;
m_ptype=ptype;
m_param_version=param_version;
}

SGParamInfo::SGParamInfo(const TParameter* param, int32_t param_version)
{
init();

/* copy name */
m_name=strdup(param->m_name);

TSGDataType type=param->m_datatype;
m_ctype=type.m_ctype;
m_stype=type.m_stype;
m_ptype=type.m_ptype;
m_param_version=param_version;
}

SGParamInfo::~SGParamInfo()
{
SG_FREE(m_name);
}

void SGParamInfo::print_param_info()
char* SGParamInfo::to_string()
{
SG_SPRINT("SGParamInfo with: ");

SG_SPRINT("name=\"%s\"", m_name);
char* buffer=SG_MALLOC(char, 200);
strcpy(buffer, "SGParamInfo with: ");
strcat(buffer, "name=\"");
strcat(buffer, m_name);
strcat(buffer, "\", type=");

TSGDataType t(m_ctype, m_stype, m_ptype);
index_t buffer_length=100;
char* buffer=SG_MALLOC(char, buffer_length);
t.to_string(buffer, buffer_length);
SG_SPRINT(", type=%s", buffer);
SG_FREE(buffer);
index_t l=100;
char* b=SG_MALLOC(char, l);
t.to_string(b, l);
strcat(buffer, b);
SG_FREE(b);

b=SG_MALLOC(char, 10);
sprintf(b, "%d", m_param_version);
strcat(buffer, ", version=");
strcat(buffer, b);
SG_FREE(b);

SG_SPRINT("\n");
return buffer;
}

void SGParamInfo::print_param_info()
{
char* s=to_string();
SG_SPRINT("%s\n", s);
SG_FREE(s);
}

SGParamInfo* SGParamInfo::duplicate() const
{
return new SGParamInfo(m_name, m_ctype, m_stype, m_ptype);
return new SGParamInfo(m_name, m_ctype, m_stype, m_ptype, m_param_version);
}

void SGParamInfo::init()
{
m_name=NULL;
m_ctype=(EContainerType) 0;
m_stype=(EStructType) 0;
m_ptype=(EPrimitiveType) 0;
m_ctype=(EContainerType) -1;
m_stype=(EStructType) -1;
m_ptype=(EPrimitiveType) -1;
m_param_version=-1;
}

bool SGParamInfo::operator==(const SGParamInfo& other) const
Expand All @@ -73,17 +117,27 @@ bool SGParamInfo::operator==(const SGParamInfo& other) const
result&=m_ctype==other.m_ctype;
result&=m_stype==other.m_stype;
result&=m_ptype==other.m_ptype;
result&=m_param_version==other.m_param_version;
return result;
}

bool SGParamInfo::operator<(const SGParamInfo& other) const
{
return strcmp(m_name, other.m_name)<0;
int32_t result=strcmp(m_name, other.m_name);

if (!result)
return m_param_version<other.m_param_version;
else
return result<0;
}

bool SGParamInfo::operator>(const SGParamInfo& other) const
{
return strcmp(m_name, other.m_name)>0;
int32_t result=strcmp(m_name, other.m_name);
if (!result)
return m_param_version<other.m_param_version;
else
return result>0;
}

ParameterMapElement::ParameterMapElement()
Expand Down
29 changes: 26 additions & 3 deletions src/shogun/base/ParameterMap.h
Expand Up @@ -16,6 +16,8 @@
namespace shogun
{

struct TParameter;

/** @brief Class that holds informations about a certain parameter of an
* CSGObject. Contains name, type, etc.
* This is used for mapping types that have changed in different versions of
Expand All @@ -35,26 +37,42 @@ class SGParamInfo
* @param ctype container type of parameter
* @param stype struct type of parameter
* @param ptype primitive type of parameter
* @param param_version version of parameter
*/
SGParamInfo(const char* name, EContainerType ctype, EStructType stype,
EPrimitiveType ptype);
EPrimitiveType ptype, int32_t param_version);

/** constructor to create from a TParameter instance
*
* @param param TParameter instance to use
* @param param_version version of parameter
*/
SGParamInfo(const TParameter* param, int32_t param_version);

/** copy constructor
* @param element to copy from
*/
SGParamInfo(const SGParamInfo& orig);

/** destructor */
virtual ~SGParamInfo();

/** prints all parameter values */
void print_param_info();

/** @return string representation, caller has to clean up */
char* to_string();

/** @return an identical copy */
SGParamInfo* duplicate() const;

/** operator for comparison, true iff all attributes are equal */
bool operator==(const SGParamInfo& other) const;

/** operator for comparison (by string m_name) */
/** operator for comparison (by string m_name, if equal by param_version) */
bool operator<(const SGParamInfo& other) const;

/** operator for comparison (by string m_name) */
/** operator for comparison (by string m_name, if equal by param_version) */
bool operator>(const SGParamInfo& other) const;

private:
Expand All @@ -63,12 +81,17 @@ class SGParamInfo
public:
/** name */
char* m_name;

/** container type */
EContainerType m_ctype;

/** struct type */
EStructType m_stype;

/** primitive type */
EPrimitiveType m_ptype;

int32_t m_param_version;
};

/** @brief Class to hold instances of a parameter map. Each element contains a
Expand Down

0 comments on commit b5aaa46

Please sign in to comment.