SHOGUN
v1.1.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011 Heiko Strathmann 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/base/ParameterMap.h> 00012 #include <shogun/mathematics/Math.h> 00013 00014 using namespace shogun; 00015 00016 SGParamInfo::SGParamInfo() 00017 { 00018 init(); 00019 } 00020 00021 SGParamInfo::SGParamInfo(const char* name, EContainerType ctype, 00022 EStructType stype, EPrimitiveType ptype) 00023 { 00024 init(); 00025 00026 /* copy name */ 00027 m_name=SG_MALLOC(char, strlen(name)+1); 00028 strcpy(m_name, name); 00029 00030 m_ctype=ctype; 00031 m_stype=stype; 00032 m_ptype=ptype; 00033 } 00034 00035 SGParamInfo::~SGParamInfo() 00036 { 00037 SG_FREE(m_name); 00038 } 00039 00040 void SGParamInfo::print_param_info() 00041 { 00042 SG_SPRINT("SGParamInfo with: "); 00043 00044 SG_SPRINT("name=\"%s\"", m_name); 00045 00046 TSGDataType t(m_ctype, m_stype, m_ptype); 00047 index_t buffer_length=100; 00048 char* buffer=SG_MALLOC(char, buffer_length); 00049 t.to_string(buffer, buffer_length); 00050 SG_SPRINT(", type=%s", buffer); 00051 SG_FREE(buffer); 00052 00053 SG_SPRINT("\n"); 00054 } 00055 00056 SGParamInfo* SGParamInfo::duplicate() const 00057 { 00058 return new SGParamInfo(m_name, m_ctype, m_stype, m_ptype); 00059 } 00060 00061 void SGParamInfo::init() 00062 { 00063 m_name=NULL; 00064 m_ctype=(EContainerType) 0; 00065 m_stype=(EStructType) 0; 00066 m_ptype=(EPrimitiveType) 0; 00067 } 00068 00069 bool SGParamInfo::operator==(const SGParamInfo& other) const 00070 { 00071 bool result=true; 00072 result&=!strcmp(m_name, other.m_name); 00073 result&=m_ctype==other.m_ctype; 00074 result&=m_stype==other.m_stype; 00075 result&=m_ptype==other.m_ptype; 00076 return result; 00077 } 00078 00079 bool SGParamInfo::operator<(const SGParamInfo& other) const 00080 { 00081 return strcmp(m_name, other.m_name)<0; 00082 } 00083 00084 bool SGParamInfo::operator>(const SGParamInfo& other) const 00085 { 00086 return strcmp(m_name, other.m_name)>0; 00087 } 00088 00089 ParameterMapElement::ParameterMapElement() 00090 { 00091 init(); 00092 } 00093 00094 ParameterMapElement::ParameterMapElement(SGParamInfo* key, 00095 SGParamInfo* value) 00096 { 00097 init(); 00098 00099 m_key=key; 00100 m_value=value; 00101 } 00102 00103 ParameterMapElement::~ParameterMapElement() 00104 { 00105 delete m_key; 00106 delete m_value; 00107 } 00108 00109 bool ParameterMapElement::operator==(const ParameterMapElement& other) const 00110 { 00111 return *m_key==*other.m_key; 00112 } 00113 00114 bool ParameterMapElement::operator<(const ParameterMapElement& other) const 00115 { 00116 return *m_key<*other.m_key; 00117 } 00118 00119 bool ParameterMapElement::operator>(const ParameterMapElement& other) const 00120 { 00121 return *m_key>*other.m_key; 00122 } 00123 00124 void ParameterMapElement::init() 00125 { 00126 m_key=NULL; 00127 m_value=NULL; 00128 } 00129 00130 ParameterMap::ParameterMap() 00131 { 00132 init(); 00133 } 00134 00135 void ParameterMap::init() 00136 { 00137 m_finalized=false; 00138 } 00139 00140 ParameterMap::~ParameterMap() 00141 { 00142 for (index_t i=0; i<m_map_elements.get_num_elements(); ++i) 00143 delete m_map_elements[i]; 00144 } 00145 00146 void ParameterMap::put(SGParamInfo* key, SGParamInfo* value) 00147 { 00148 m_map_elements.append_element(new ParameterMapElement(key, value)); 00149 m_finalized=false; 00150 } 00151 00152 SGParamInfo* ParameterMap::get(SGParamInfo* key) const 00153 { 00154 index_t num_elements=m_map_elements.get_num_elements(); 00155 00156 /* check if underlying array is sorted */ 00157 if (!m_finalized && num_elements>1) 00158 SG_SERROR("Call finalize_map() before calling get()\n"); 00159 00160 /* do binary search in array of pointers */ 00161 SGVector<ParameterMapElement*> array(m_map_elements.get_array(), 00162 num_elements); 00163 00164 /* dummy element for searching */ 00165 ParameterMapElement* dummy=new ParameterMapElement(key->duplicate(), 00166 key->duplicate()); 00167 index_t index=CMath::binary_search<ParameterMapElement> (array, dummy); 00168 delete dummy; 00169 00170 if (index==-1) 00171 return NULL; 00172 00173 ParameterMapElement* element=m_map_elements.get_element(index); 00174 SGParamInfo* value=element->m_value; 00175 00176 return value; 00177 } 00178 00179 void ParameterMap::finalize_map() 00180 { 00181 /* sort underlying array */ 00182 SGVector<ParameterMapElement*> array(m_map_elements.get_array(), 00183 m_map_elements.get_num_elements()); 00184 00185 CMath::qsort<ParameterMapElement> (array); 00186 00187 m_finalized=true; 00188 } 00189 00190 void ParameterMap::print_map() 00191 { 00192 for (index_t i=0; i< m_map_elements.get_num_elements(); ++i) 00193 { 00194 ParameterMapElement* current=m_map_elements[i]; 00195 SG_SPRINT("%d\n", i); 00196 SG_SPRINT("key: "); 00197 current->m_key->print_param_info(); 00198 SG_SPRINT("value: "); 00199 current->m_value->print_param_info(); 00200 } 00201 }