SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
12 
13 using namespace shogun;
14 
16 : CLabels()
17 {
18  init();
19 }
20 
22 : CLabels()
23 {
24  init();
25  m_labels = new CDynamicObjectArray(num_labels);
27 }
28 
30 {
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 
41 {
43  return m_labels;
44 }
45 
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 
56 {
57  ensure_valid_sdt(label);
58  m_labels->push_back(label);
59 }
60 
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 
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;
90 }
91 
92 void CStructuredLabels::ensure_valid_sdt(CStructuredData* label)
93 {
94  if ( m_sdt == SDT_UNKNOWN )
95  {
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 }
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
CDynamicObjectArray * get_labels() const
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
CSubsetStack * m_subset_stack
Definition: Labels.h:132
#define SG_REF(x)
Definition: SGObject.h:51
bool set_element(CSGObject *e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
virtual EStructuredDataType get_structured_data_type() const
CDynamicObjectArray * m_labels
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
EStructuredDataType m_sdt
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
#define SG_UNREF(x)
Definition: SGObject.h:52
virtual void add_label(CStructuredData *label)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual int32_t get_num_labels() const
virtual CStructuredData * get_label(int32_t idx)
CSGObject * get_element(int32_t index) const
#define SG_ADD(...)
Definition: SGObject.h:81
virtual void ensure_valid(const char *context=NULL)
Base class of the components of StructuredLabels.
virtual bool set_label(int32_t idx, CStructuredData *label)

SHOGUN Machine Learning Toolbox - Documentation