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 CStructuredLabels* CStructuredLabels::obtain_from_generic(CLabels* base_labels) 00035 { 00036 if ( base_labels->get_label_type() == LT_STRUCTURED ) 00037 return (CStructuredLabels*) base_labels; 00038 else 00039 SG_SERROR("base_labels must be of dynamic type CStructuredLabels\n"); 00040 00041 return NULL; 00042 } 00043 00044 void CStructuredLabels::ensure_valid(const char* context) 00045 { 00046 if ( m_labels == NULL ) 00047 SG_ERROR("Non-valid StructuredLabels in %s", context); 00048 } 00049 00050 CDynamicObjectArray* CStructuredLabels::get_labels() const 00051 { 00052 SG_REF(m_labels); 00053 return m_labels; 00054 } 00055 00056 CStructuredData* CStructuredLabels::get_label(int32_t idx) 00057 { 00058 ensure_valid("CStructuredLabels::get_label(int32_t)"); 00059 if ( idx < 0 || idx >= get_num_labels() ) 00060 SG_ERROR("Index must be inside [0, num_labels-1]\n"); 00061 00062 return (CStructuredData*) m_labels->get_element(idx); 00063 } 00064 00065 void CStructuredLabels::add_label(CStructuredData* label) 00066 { 00067 ensure_valid_sdt(label); 00068 m_labels->push_back(label); 00069 } 00070 00071 bool CStructuredLabels::set_label(int32_t idx, CStructuredData* label) 00072 { 00073 ensure_valid_sdt(label); 00074 int32_t real_idx = m_subset_stack->subset_idx_conversion(idx); 00075 00076 if ( real_idx < get_num_labels() ) 00077 { 00078 return m_labels->insert_element(label, real_idx); 00079 } 00080 else 00081 { 00082 return false; 00083 } 00084 } 00085 00086 int32_t CStructuredLabels::get_num_labels() 00087 { 00088 if ( m_labels == NULL ) 00089 return 0; 00090 else 00091 return m_labels->get_num_elements(); 00092 } 00093 00094 void CStructuredLabels::init() 00095 { 00096 SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels", MS_NOT_AVAILABLE); 00097 00098 m_labels = NULL; 00099 m_sdt = SDT_UNKNOWN; 00100 } 00101 00102 void CStructuredLabels::ensure_valid_sdt(CStructuredData* label) 00103 { 00104 if ( m_sdt == SDT_UNKNOWN ) 00105 { 00106 m_sdt = label->get_structured_data_type(); 00107 } 00108 else 00109 { 00110 REQUIRE(label->get_structured_data_type() == m_sdt, "All the labels must " 00111 "belong to the same CStructuredData child class\n"); 00112 } 00113 }