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