SHOGUN
v3.2.0
|
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) 2009 Soeren Sonnenburg 00008 * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #include <shogun/features/Features.h> 00012 #include <shogun/features/AttributeFeatures.h> 00013 #include <shogun/lib/memory.h> 00014 00015 using namespace shogun; 00016 00017 CAttributeFeatures::CAttributeFeatures() 00018 : CFeatures(0) 00019 { 00020 } 00021 00022 CFeatures* CAttributeFeatures::get_attribute(char* attr_name) 00023 { 00024 int32_t idx=find_attr_index(attr_name); 00025 if (idx>=0) 00026 { 00027 CFeatures* f=features[idx].attr_obj; 00028 SG_REF(f); 00029 return f; 00030 } 00031 00032 return NULL; 00033 } 00034 00035 void CAttributeFeatures::get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj) 00036 { 00037 T_ATTRIBUTE a= features.get_element_safe(idx); 00038 attr_name= a.attr_name; 00039 attr_obj= a.attr_obj; 00040 SG_REF(a.attr_obj); 00041 } 00042 00043 bool CAttributeFeatures::set_attribute(char* attr_name, CFeatures* attr_obj) 00044 { 00045 int32_t idx=find_attr_index(attr_name); 00046 if (idx==-1) 00047 idx=features.get_num_elements(); 00048 00049 T_ATTRIBUTE a; 00050 a.attr_name=get_strdup(attr_name); 00051 a.attr_obj=attr_obj; 00052 00053 SG_REF(attr_obj); 00054 00055 return features.set_element(a, idx); 00056 } 00057 00058 bool CAttributeFeatures::del_attribute(char* attr_name) 00059 { 00060 int32_t idx=find_attr_index(attr_name); 00061 00062 if (idx>=0) 00063 { 00064 T_ATTRIBUTE a= features[idx]; 00065 SG_FREE(a.attr_name); 00066 SG_UNREF(a.attr_obj); 00067 return true; 00068 } 00069 return false; 00070 } 00071 00072 int32_t CAttributeFeatures::get_num_attributes() 00073 { 00074 return features.get_num_elements(); 00075 } 00076 00077 int32_t CAttributeFeatures::find_attr_index(char* attr_name) 00078 { 00079 int32_t n=features.get_num_elements(); 00080 for (int32_t i=0; i<n; i++) 00081 { 00082 if (!strcmp(features[n].attr_name, attr_name)) 00083 return i; 00084 } 00085 00086 return -1; 00087 } 00088 00089 CAttributeFeatures::~CAttributeFeatures() 00090 { 00091 int32_t n=features.get_num_elements(); 00092 for (int32_t i=0; i<n; i++) 00093 SG_UNREF_NO_NULL(features[i].attr_obj); 00094 }