SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParameterMap.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011 Heiko Strathmann
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
13 
14 using namespace shogun;
15 
17 {
18  init();
19 }
20 
21 SGParamInfo::SGParamInfo(const char* name, EContainerType ctype,
22  EStructType stype, EPrimitiveType ptype)
23 {
24  init();
25 
26  /* copy name */
27  m_name=SG_MALLOC(char, strlen(name)+1);
28  strcpy(m_name, name);
29 
30  m_ctype=ctype;
31  m_stype=stype;
32  m_ptype=ptype;
33 }
34 
36 {
37  SG_FREE(m_name);
38 }
39 
41 {
42  SG_SPRINT("SGParamInfo with: ");
43 
44  SG_SPRINT("name=\"%s\"", m_name);
45 
47  index_t buffer_length=100;
48  char* buffer=SG_MALLOC(char, buffer_length);
49  t.to_string(buffer, buffer_length);
50  SG_SPRINT(", type=%s", buffer);
51  SG_FREE(buffer);
52 
53  SG_SPRINT("\n");
54 }
55 
57 {
58  return new SGParamInfo(m_name, m_ctype, m_stype, m_ptype);
59 }
60 
61 void SGParamInfo::init()
62 {
63  m_name=NULL;
64  m_ctype=(EContainerType) 0;
65  m_stype=(EStructType) 0;
66  m_ptype=(EPrimitiveType) 0;
67 }
68 
69 bool SGParamInfo::operator==(const SGParamInfo& other) const
70 {
71  bool result=true;
72  result&=!strcmp(m_name, other.m_name);
73  result&=m_ctype==other.m_ctype;
74  result&=m_stype==other.m_stype;
75  result&=m_ptype==other.m_ptype;
76  return result;
77 }
78 
79 bool SGParamInfo::operator<(const SGParamInfo& other) const
80 {
81  return strcmp(m_name, other.m_name)<0;
82 }
83 
84 bool SGParamInfo::operator>(const SGParamInfo& other) const
85 {
86  return strcmp(m_name, other.m_name)>0;
87 }
88 
90 {
91  init();
92 }
93 
95  SGParamInfo* value)
96 {
97  init();
98 
99  m_key=key;
100  m_value=value;
101 }
102 
104 {
105  delete m_key;
106  delete m_value;
107 }
108 
110 {
111  return *m_key==*other.m_key;
112 }
113 
115 {
116  return *m_key<*other.m_key;
117 }
118 
120 {
121  return *m_key>*other.m_key;
122 }
123 
124 void ParameterMapElement::init()
125 {
126  m_key=NULL;
127  m_value=NULL;
128 }
129 
131 {
132  init();
133 }
134 
135 void ParameterMap::init()
136 {
137  m_finalized=false;
138 }
139 
141 {
142  for (index_t i=0; i<m_map_elements.get_num_elements(); ++i)
143  delete m_map_elements[i];
144 }
145 
147 {
149  m_finalized=false;
150 }
151 
153 {
154  index_t num_elements=m_map_elements.get_num_elements();
155 
156  /* check if underlying array is sorted */
157  if (!m_finalized && num_elements>1)
158  SG_SERROR("Call finalize_map() before calling get()\n");
159 
160  /* do binary search in array of pointers */
162  num_elements);
163 
164  /* dummy element for searching */
166  key->duplicate());
167  index_t index=CMath::binary_search<ParameterMapElement> (array, dummy);
168  delete dummy;
169 
170  if (index==-1)
171  return NULL;
172 
174  SGParamInfo* value=element->m_value;
175 
176  return value;
177 }
178 
180 {
181  /* sort underlying array */
184 
185  CMath::qsort<ParameterMapElement> (array);
186 
187  m_finalized=true;
188 }
189 
191 {
192  for (index_t i=0; i< m_map_elements.get_num_elements(); ++i)
193  {
195  SG_SPRINT("%d\n", i);
196  SG_SPRINT("key: ");
197  current->m_key->print_param_info();
198  SG_SPRINT("value: ");
199  current->m_value->print_param_info();
200  }
201 }

SHOGUN Machine Learning Toolbox - Documentation