SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
StructuredLabels.cpp
Go to the documentation of this file.
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) 2012 Fernando José Iglesias García
00008  * Copyright (C) 2012 Fernando José Iglesias García
00009  */
00010 
00011 #include <shogun/labels/StructuredLabels.h>
00012 
00013 using namespace shogun;
00014 
00015 CStructuredLabels::CStructuredLabels()
00016 : CLabels()
00017 {
00018     init();
00019 }
00020 
00021 CStructuredLabels::CStructuredLabels(int32_t num_labels)
00022 : CLabels()
00023 {
00024     init();
00025     m_labels = new CDynamicObjectArray(num_labels);
00026     SG_REF(m_labels);
00027 }
00028 
00029 CStructuredLabels::~CStructuredLabels()
00030 {
00031     SG_UNREF(m_labels);
00032 }
00033 
00034 void CStructuredLabels::ensure_valid(const char* context)
00035 {
00036     if ( m_labels == NULL )
00037         SG_ERROR("Non-valid StructuredLabels in %s", context)
00038 }
00039 
00040 CDynamicObjectArray* CStructuredLabels::get_labels() const
00041 {
00042     SG_REF(m_labels);
00043     return m_labels;
00044 }
00045 
00046 CStructuredData* CStructuredLabels::get_label(int32_t idx)
00047 {
00048     ensure_valid("CStructuredLabels::get_label(int32_t)");
00049     if ( idx < 0 || idx >= get_num_labels() )
00050         SG_ERROR("Index must be inside [0, num_labels-1]\n")
00051 
00052     return (CStructuredData*) m_labels->get_element(idx);
00053 }
00054 
00055 void CStructuredLabels::add_label(CStructuredData* label)
00056 {
00057     ensure_valid_sdt(label);
00058     m_labels->push_back(label);
00059 }
00060 
00061 bool CStructuredLabels::set_label(int32_t idx, CStructuredData* label)
00062 {
00063     ensure_valid_sdt(label);
00064     int32_t real_idx = m_subset_stack->subset_idx_conversion(idx);
00065 
00066     if ( real_idx < get_num_labels() )
00067     {
00068         return m_labels->set_element(label, real_idx);
00069     }
00070     else
00071     {
00072         return false;
00073     }
00074 }
00075 
00076 int32_t CStructuredLabels::get_num_labels() const
00077 {
00078     if ( m_labels == NULL )
00079         return 0;
00080     else
00081         return m_labels->get_num_elements();
00082 }
00083 
00084 void CStructuredLabels::init()
00085 {
00086     SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels", MS_NOT_AVAILABLE);
00087 
00088     m_labels = NULL;
00089     m_sdt = SDT_UNKNOWN;
00090 }
00091 
00092 void CStructuredLabels::ensure_valid_sdt(CStructuredData* label)
00093 {
00094     if ( m_sdt == SDT_UNKNOWN )
00095     {
00096         m_sdt = label->get_structured_data_type();
00097     }
00098     else
00099     {
00100         REQUIRE(label->get_structured_data_type() == m_sdt, "All the labels must "
00101                 "belong to the same CStructuredData child class\n");
00102     }
00103 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation