Skip to content

Commit

Permalink
Merge pull request #357 from karlnapf/master
Browse files Browse the repository at this point in the history
multiple parameter mappings
  • Loading branch information
Soeren Sonnenburg committed Jan 26, 2012
2 parents 210ae0a + 5c5dce9 commit 6513675
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 123 deletions.
Expand Up @@ -75,16 +75,15 @@ class CTestClassNew : public CSGObject
virtual TParameter* migrate(DynArray<TParameter*>* param_base,
const SGParamInfo* target)
{
SG_PRINT("entering CTestClassNew::migrate\n");
TParameter* result=NULL;
TParameter* to_migrate=NULL;

if (*target==SGParamInfo("m_number", CT_SCALAR, ST_NONE, PT_INT32, 1))
{
/* specify name change here (again, was also done in mappings) */
char* new_name="m_number_to_keep";
char* old_name="m_number_to_keep";
one_to_one_migration_prepare(param_base, target, result,
to_migrate, new_name);
to_migrate, old_name);

/* here: simply copy data because nothing has changed */
*((int32_t*)result->m_parameter)=
Expand Down
121 changes: 112 additions & 9 deletions examples/undocumented/libshogun/base_parameter_map.cpp
Expand Up @@ -41,8 +41,10 @@ void test_mapping_1()
);

/* finalizing the map is needed before accessing it */
SG_SPRINT("\n\before finalization:\n");
map->finalize_map();

SG_SPRINT("\n\nafter finalization:\n");
map->print_map();
SG_SPRINT("\n");

Expand All @@ -68,12 +70,15 @@ void test_mapping_1()
SG_SPRINT("searching for: %s\n", s);
SG_FREE(s);

const SGParamInfo* result=map->get(current);
DynArray<const SGParamInfo*>* result=map->get(current);
if (result)
{
s=result->to_string();
SG_SPRINT("found: %s\n\n", s);
SG_FREE(s);
for (index_t i=0; i<result->get_num_elements(); ++i)
{
s=result->get_element(i)->to_string();
SG_SPRINT("found: %s\n\n", s);
SG_FREE(s);
}
}
else
SG_SPRINT("nothing found\n\n");
Expand All @@ -86,14 +91,17 @@ void test_mapping_1()

void print_value(const SGParamInfo* key, ParameterMap* map)
{
const SGParamInfo* current=map->get(key);
DynArray<const SGParamInfo*>* current=map->get(key);
key->print_param_info();
SG_SPRINT("value: ");

if (current)
current->print_param_info();
{
for (index_t i=0; i<current->get_num_elements(); ++i)
current->get_element(i)->print_param_info("\t");
}
else
SG_SPRINT("no element\n");
SG_SPRINT("no elements\n");

SG_SPRINT("\n");
}
Expand All @@ -120,8 +128,6 @@ void test_mapping_2()
map->put(new SGParamInfo("4", cfrom, sfrom, pfrom, 4),
new SGParamInfo("vier", cto, sto, pto, 3));

SG_SPRINT("before finalization:\n");
map->print_map();
map->finalize_map();

SG_SPRINT("\n\nafter finalization:\n");
Expand Down Expand Up @@ -157,10 +163,107 @@ void test_mapping_2()
delete map;
}

void test_mapping_0()
{
/* test multiple values per key */
ParameterMap* map=new ParameterMap();

EContainerType cfrom=CT_SCALAR;
EContainerType cto=CT_MATRIX;

EStructType sfrom=ST_NONE;
EStructType sto=ST_STRING;

EPrimitiveType pfrom=PT_BOOL;
EPrimitiveType pto=PT_SGOBJECT;

/* 3 equal keys */
map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2),
new SGParamInfo("eins a", cto, sto, pto, 1));

map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2),
new SGParamInfo("eins b", cto, sto, pto, 1));

map->put(new SGParamInfo("1", cfrom, sfrom, pfrom, 2),
new SGParamInfo("eins c", cto, sto, pto, 1));

/* 2 equal keys */
map->put(new SGParamInfo("2", cfrom, sfrom, pfrom, 2),
new SGParamInfo("zwei a", cto, sto, pto, 1));

map->put(new SGParamInfo("2", cfrom, sfrom, pfrom, 2),
new SGParamInfo("zwei b", cto, sto, pto, 1));

map->finalize_map();

SG_SPRINT("printing finalized map\n");
map->print_map();

/* assert that all is there */
DynArray<const SGParamInfo*>* result;
bool found;

/* key 0 */
result=map->get(SGParamInfo("1", cfrom, sfrom, pfrom, 2));
ASSERT(result);

/* first value element */
found=false;
for (index_t i=0; i<result->get_num_elements(); ++i)
{
if (*result->get_element(i) == SGParamInfo("eins a", cto, sto, pto, 1))
found=true;
}
ASSERT(found);

/* second value element */
found=false;
for (index_t i=0; i<result->get_num_elements(); ++i)
{
if (*result->get_element(i) == SGParamInfo("eins b", cto, sto, pto, 1))
found=true;
}
ASSERT(found);

/* third value element */
found=false;
for (index_t i=0; i<result->get_num_elements(); ++i)
{
if (*result->get_element(i) == SGParamInfo("eins c", cto, sto, pto, 1))
found=true;
}
ASSERT(found);

/* key 1 */
result=map->get(SGParamInfo("2", cfrom, sfrom, pfrom, 2));
ASSERT(result);

/* first value element */
found=false;
for (index_t i=0; i<result->get_num_elements(); ++i)
{
if (*result->get_element(i) == SGParamInfo("zwei a", cto, sto, pto, 1))
found=true;
}
ASSERT(found);

/* second value element */
found=false;
for (index_t i=0; i<result->get_num_elements(); ++i)
{
if (*result->get_element(i) == SGParamInfo("zwei b", cto, sto, pto, 1))
found=true;
}
ASSERT(found);

delete map;
}

int main(int argc, char **argv)
{
init_shogun(&print_message, &print_message, &print_message);

test_mapping_0();
test_mapping_1();
test_mapping_2();

Expand Down
7 changes: 7 additions & 0 deletions src/shogun/base/DynArray.h
Expand Up @@ -353,6 +353,13 @@ template <class T> class DynArray
memset(array, 0, (last_element_idx+1)*sizeof(T));
}

/** resets the array (as if it was just created), keeps granularity */
void reset()
{
clear_array();
last_element_idx=-1;
}

/** randomizes the array */
void shuffle()
{
Expand Down

0 comments on commit 6513675

Please sign in to comment.