Skip to content

Commit

Permalink
Merge pull request #503 from karlnapf/master
Browse files Browse the repository at this point in the history
get rid of SGVector<void/char>
  • Loading branch information
karlnapf committed May 7, 2012
2 parents a6dd589 + cd00a95 commit 8105d5f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
51 changes: 27 additions & 24 deletions src/shogun/modelselection/ModelSelectionParameters.cpp
Expand Up @@ -45,14 +45,10 @@ void CModelSelectionParameters::init()
m_child_nodes=new CDynamicObjectArray();
SG_REF(m_child_nodes);
m_value_type=MSPT_NONE;
m_values=NULL;
m_values_length=0;

m_parameters->add((char*)m_node_name, "node_name", "Name of node");
m_parameters->add((CSGObject**)&m_sgobject, "sgobject",
"CSGObject of this node");
m_parameters->add((CSGObject**)m_child_nodes, "child nodes",
"children of this node");
// m_parameters->add(&m_value_type, "value_type",
// "type of the values of this node");
/* no parameter registering. These parameter nodes will not be serialized */
}

CModelSelectionParameters::~CModelSelectionParameters()
Expand All @@ -66,7 +62,7 @@ CModelSelectionParameters::~CModelSelectionParameters()
void CModelSelectionParameters::append_child(CModelSelectionParameters* child)
{
/* only possible if there are no values set */
if (m_values.vector)
if (m_values)
SG_ERROR("not possible to append child: there already is a range\n");

/* do a basic check if the add is possible */
Expand Down Expand Up @@ -94,11 +90,14 @@ void CModelSelectionParameters::append_child(CModelSelectionParameters* child)
}

template <class T>
void CModelSelectionParameters::set_values(SGVector<T> values)
void CModelSelectionParameters::set_values(const SGVector<T>& values,
EMSParamType value_type)
{
/* possibly delete old range values */
delete_values();
m_values=(SGVector<char>) values;
m_values=values.vector;
m_values_length=values.vlen;
m_value_type=value_type;
}

void CModelSelectionParameters::build_values(float64_t min, float64_t max,
Expand Down Expand Up @@ -139,8 +138,8 @@ void CModelSelectionParameters::build_values(EMSParamType value_type, void* min,
*((float64_t*)step),
*((float64_t*)type_base));

m_values = SGVector<char>((char*) values.vector, values.vlen);

m_values=values.vector;
m_values_length=values.vlen;
}
else if (value_type==MSPT_INT32)
{
Expand All @@ -150,7 +149,9 @@ void CModelSelectionParameters::build_values(EMSParamType value_type, void* min,
type,
*((int32_t*)step),
*((int32_t*)type_base));
m_values = SGVector<char>((char*) values.vector, values.vlen);

m_values=values.vector;
m_values_length=values.vlen;
}
else if (value_type==MSPT_NONE)
{
Expand All @@ -170,20 +171,20 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations()
* build trees of Parameter instances which each contain one value
*/

if (m_values.vector)
if (m_values)
{
for (index_t i=0; i<m_values.vlen; ++i)
for (index_t i=0; i<m_values_length; ++i)
{
// create tree with only one parameter element //
Parameter* p=new Parameter();

switch (m_value_type)
{
case MSPT_FLOAT64:
p->add(&((float64_t*)m_values.vector)[i], m_node_name);
p->add(&((float64_t*)m_values)[i], m_node_name);
break;
case MSPT_INT32:
p->add(&((int32_t*)m_values.vector)[i], m_node_name);;
p->add(&((int32_t*)m_values)[i], m_node_name);;
break;
case MSPT_NONE:
SG_ERROR("Value node has no type!\n");
Expand Down Expand Up @@ -222,7 +223,7 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations()
(CModelSelectionParameters*)m_child_nodes->get_element(i);

/* split children with values and children with other */
if (current->m_values.vector)
if (current->m_values)
value_children.append_element(current);
else
non_value_children.append_element(current);
Expand Down Expand Up @@ -420,18 +421,20 @@ void CModelSelectionParameters::print_tree(int prefix_num)
}
else
{
if (m_values.vector)
if (m_values)
{
// value node
SG_PRINT("%s%s with values: ", prefix, m_node_name);

switch (m_value_type)
{
case MSPT_FLOAT64:
CMath::display_vector((float64_t*)m_values.vector, m_values.vlen);
CMath::display_vector((float64_t*)m_values,
m_values_length);
break;
case MSPT_INT32:
CMath::display_vector((int32_t*)m_values.vector, m_values.vlen);;
CMath::display_vector((int32_t*)m_values,
m_values_length);;
break;
case MSPT_NONE:
SG_ERROR("Value node has no type!\n");
Expand All @@ -451,15 +454,15 @@ void CModelSelectionParameters::print_tree(int prefix_num)

void CModelSelectionParameters::delete_values()
{
if (m_values.vector)
if (m_values)
{
switch (m_value_type)
{
case MSPT_FLOAT64:
m_values.unref();
SG_FREE((float64_t*)m_values);
break;
case MSPT_INT32:
m_values.unref();
SG_FREE((int32_t*)m_values);
break;
case MSPT_NONE:
SG_ERROR("Value node has no type!\n");
Expand Down
28 changes: 23 additions & 5 deletions src/shogun/modelselection/ModelSelectionParameters.h
Expand Up @@ -89,10 +89,11 @@ class CModelSelectionParameters: public CSGObject
* If the latter are not possible to be produced by set_range, a vector may
* be specified directly.
*
* @param values value vector
* @param values value vector. no ref counting takes place here
* @param value_type type of the provided vector
*/
template <class T>
void set_values(SGVector<T> values);
void set_values(const SGVector<T>& values, EMSParamType value_type);

/** SG_PRINT's the tree of which this node is the base
*
Expand All @@ -105,7 +106,7 @@ class CModelSelectionParameters: public CSGObject
* structure, a set of trees which contain all combinations of parameters
* that are implied by this tree is generated.
*
* @result result all trees of parameter combinations are put into here
* @return result all trees of parameter combinations are put into here
*/
CDynamicObjectArray* get_combinations();

Expand All @@ -123,6 +124,22 @@ class CModelSelectionParameters: public CSGObject
return "ModelSelectionParameters";
}

/** Not allowed for this class, throws an error */
virtual bool save_serializable(CSerializableFile* file,
const char* prefix="", int32_t param_version=VERSION_PARAMETER)
{
SG_ERROR("Serialization is not allowed for %s!\n", get_name());
return false;
}

/** Not allowed for this class, throws an error */
virtual bool load_serializable(CSerializableFile* file,
const char* prefix="", int32_t param_version=VERSION_PARAMETER)
{
SG_ERROR("Serialization is not allowed for %s!\n", get_name());
return false;
}

private:
void init();

Expand All @@ -146,7 +163,8 @@ class CModelSelectionParameters: public CSGObject
private:
CSGObject* m_sgobject;
const char* m_node_name;
SGVector<char> m_values; // dummy void type char
void* m_values;
index_t m_values_length;
CDynamicObjectArray* m_child_nodes;
EMSParamType m_value_type;
};
Expand All @@ -170,7 +188,7 @@ template <class T> SGVector<T> create_range_array(T min, T max,
if (max<min)
SG_SERROR("unable build values: max=%f < min=%f\n", max, min);

/* create value vector */
/* create value vector, no ref-counting */
index_t num_values=CMath::round(max-min)/step+1;
SGVector<T> result(num_values, false);

Expand Down

0 comments on commit 8105d5f

Please sign in to comment.