SHOGUN
v1.1.0
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
src
shogun
base
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
11
#include <
shogun/base/ParameterMap.h
>
12
#include <
shogun/mathematics/Math.h
>
13
14
using namespace
shogun;
15
16
SGParamInfo::SGParamInfo
()
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
35
SGParamInfo::~SGParamInfo
()
36
{
37
SG_FREE
(
m_name
);
38
}
39
40
void
SGParamInfo::print_param_info
()
41
{
42
SG_SPRINT
(
"SGParamInfo with: "
);
43
44
SG_SPRINT
(
"name=\"%s\""
,
m_name
);
45
46
TSGDataType
t(
m_ctype
,
m_stype
,
m_ptype
);
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
56
SGParamInfo
*
SGParamInfo::duplicate
()
const
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
89
ParameterMapElement::ParameterMapElement
()
90
{
91
init();
92
}
93
94
ParameterMapElement::ParameterMapElement
(
SGParamInfo
* key,
95
SGParamInfo
* value)
96
{
97
init();
98
99
m_key
=key;
100
m_value
=value;
101
}
102
103
ParameterMapElement::~ParameterMapElement
()
104
{
105
delete
m_key
;
106
delete
m_value
;
107
}
108
109
bool
ParameterMapElement::operator==
(
const
ParameterMapElement
& other)
const
110
{
111
return
*
m_key
==*other.
m_key
;
112
}
113
114
bool
ParameterMapElement::operator<
(
const
ParameterMapElement
& other)
const
115
{
116
return
*
m_key
<*other.
m_key
;
117
}
118
119
bool
ParameterMapElement::operator>
(
const
ParameterMapElement
& other)
const
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
130
ParameterMap::ParameterMap
()
131
{
132
init();
133
}
134
135
void
ParameterMap::init()
136
{
137
m_finalized
=
false
;
138
}
139
140
ParameterMap::~ParameterMap
()
141
{
142
for
(
index_t
i=0; i<
m_map_elements
.
get_num_elements
(); ++i)
143
delete
m_map_elements
[i];
144
}
145
146
void
ParameterMap::put
(
SGParamInfo
* key,
SGParamInfo
* value)
147
{
148
m_map_elements
.
append_element
(
new
ParameterMapElement
(key, value));
149
m_finalized
=
false
;
150
}
151
152
SGParamInfo
*
ParameterMap::get
(
SGParamInfo
* key)
const
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 */
161
SGVector<ParameterMapElement*>
array(
m_map_elements
.
get_array
(),
162
num_elements);
163
164
/* dummy element for searching */
165
ParameterMapElement
* dummy=
new
ParameterMapElement
(key->
duplicate
(),
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
173
ParameterMapElement
* element=
m_map_elements
.
get_element
(index);
174
SGParamInfo
* value=element->
m_value
;
175
176
return
value;
177
}
178
179
void
ParameterMap::finalize_map
()
180
{
181
/* sort underlying array */
182
SGVector<ParameterMapElement*>
array(
m_map_elements
.
get_array
(),
183
m_map_elements
.
get_num_elements
());
184
185
CMath::qsort<ParameterMapElement> (array);
186
187
m_finalized
=
true
;
188
}
189
190
void
ParameterMap::print_map
()
191
{
192
for
(
index_t
i=0; i<
m_map_elements
.
get_num_elements
(); ++i)
193
{
194
ParameterMapElement
* current=
m_map_elements
[i];
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