SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
labels
StructuredLabels.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) 2012 Fernando José Iglesias García
8
* Copyright (C) 2012 Fernando José Iglesias García
9
*/
10
11
#include <
shogun/labels/StructuredLabels.h
>
12
13
using namespace
shogun;
14
15
CStructuredLabels::CStructuredLabels
()
16
:
CLabels
()
17
{
18
init();
19
}
20
21
CStructuredLabels::CStructuredLabels
(int32_t num_labels)
22
:
CLabels
()
23
{
24
init();
25
m_labels
=
new
CDynamicObjectArray
(num_labels);
26
SG_REF
(
m_labels
);
27
}
28
29
CStructuredLabels::~CStructuredLabels
()
30
{
31
SG_UNREF
(
m_labels
);
32
}
33
34
void
CStructuredLabels::ensure_valid
(
const
char
* context)
35
{
36
if
(
m_labels
== NULL )
37
SG_ERROR
(
"Non-valid StructuredLabels in %s"
, context)
38
}
39
40
CDynamicObjectArray
*
CStructuredLabels::get_labels
()
const
41
{
42
SG_REF
(
m_labels
);
43
return
m_labels
;
44
}
45
46
CStructuredData
*
CStructuredLabels::get_label
(int32_t idx)
47
{
48
ensure_valid
(
"CStructuredLabels::get_label(int32_t)"
);
49
if
( idx < 0 || idx >=
get_num_labels
() )
50
SG_ERROR
(
"Index must be inside [0, num_labels-1]\n"
)
51
52
return
(
CStructuredData
*)
m_labels
->
get_element
(idx);
53
}
54
55
void
CStructuredLabels::add_label
(
CStructuredData
* label)
56
{
57
ensure_valid_sdt(label);
58
m_labels
->
push_back
(label);
59
}
60
61
bool
CStructuredLabels::set_label
(int32_t idx,
CStructuredData
* label)
62
{
63
ensure_valid_sdt(label);
64
int32_t real_idx =
m_subset_stack
->
subset_idx_conversion
(idx);
65
66
if
( real_idx <
get_num_labels
() )
67
{
68
return
m_labels
->
set_element
(label, real_idx);
69
}
70
else
71
{
72
return
false
;
73
}
74
}
75
76
int32_t
CStructuredLabels::get_num_labels
()
const
77
{
78
if
(
m_labels
== NULL )
79
return
0;
80
else
81
return
m_labels
->
get_num_elements
();
82
}
83
84
void
CStructuredLabels::init()
85
{
86
SG_ADD
((
CSGObject
**) &
m_labels
,
"m_labels"
,
"The labels"
,
MS_NOT_AVAILABLE
);
87
88
m_labels
= NULL;
89
m_sdt
=
SDT_UNKNOWN
;
90
}
91
92
void
CStructuredLabels::ensure_valid_sdt(
CStructuredData
* label)
93
{
94
if
(
m_sdt
==
SDT_UNKNOWN
)
95
{
96
m_sdt
= label->
get_structured_data_type
();
97
}
98
else
99
{
100
REQUIRE
(label->
get_structured_data_type
() ==
m_sdt
,
"All the labels must "
101
"belong to the same CStructuredData child class\n"
);
102
}
103
}
SHOGUN
Machine Learning Toolbox - Documentation