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 #ifndef _CATTRIBUTE_FEATURES__H__ 00011 #define _CATTRIBUTE_FEATURES__H__ 00012 00013 #include <string.h> 00014 00015 #include "features/Features.h" 00016 #include "base/DynArray.h" 00017 00018 namespace shogun 00019 { 00020 00021 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00022 00023 struct T_ATTRIBUTE 00024 { 00026 char* attr_name; 00028 CFeatures* attr_obj; 00029 }; 00030 #endif // DOXYGEN_SHOULD_SKIP_THIS 00031 00046 class CAttributeFeatures : public CFeatures 00047 { 00048 00049 public: 00051 CAttributeFeatures(); 00052 00054 virtual ~CAttributeFeatures(); 00055 00061 CFeatures* get_attribute(char* attr_name) 00062 { 00063 int32_t idx=find_attr_index(attr_name); 00064 if (idx>=0) 00065 { 00066 CFeatures* f=features[idx].attr_obj; 00067 SG_REF(f); 00068 return f; 00069 } 00070 00071 return NULL; 00072 } 00073 00080 inline void get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj) 00081 { 00082 T_ATTRIBUTE a= features.get_element_safe(idx); 00083 attr_name= a.attr_name; 00084 attr_obj= a.attr_obj; 00085 SG_REF(a.attr_obj); 00086 } 00087 00094 inline bool set_attribute(char* attr_name, CFeatures* attr_obj) 00095 { 00096 int32_t idx=find_attr_index(attr_name); 00097 if (idx==-1) 00098 idx=features.get_num_elements(); 00099 00100 T_ATTRIBUTE a; 00101 a.attr_name=strdup(attr_name); 00102 a.attr_obj=attr_obj; 00103 00104 SG_REF(attr_obj); 00105 00106 return features.set_element(a, idx); 00107 } 00108 00114 inline bool del_attribute(char* attr_name) 00115 { 00116 int32_t idx=find_attr_index(attr_name); 00117 00118 if (idx>=0) 00119 { 00120 T_ATTRIBUTE a= features[idx]; 00121 free(a.attr_name); 00122 SG_UNREF(a.attr_obj); 00123 return true; 00124 } 00125 return false; 00126 } 00127 00128 00133 inline int32_t get_num_attributes() 00134 { 00135 return features.get_num_elements(); 00136 } 00137 00139 inline virtual const char* get_name() const { return "AttributeFeatures"; } 00140 00147 virtual CFeatures* duplicate() const=0; 00148 00155 virtual EFeatureType get_feature_type()=0; 00156 00163 virtual EFeatureClass get_feature_class()=0; 00164 00171 virtual int32_t get_num_vectors()=0 ; 00172 00179 virtual int32_t get_size()=0; 00180 00181 protected: 00187 inline int32_t find_attr_index(char* attr_name) 00188 { 00189 int32_t n=features.get_num_elements(); 00190 for (int32_t i=0; i<n; i++) 00191 { 00192 if (!strcmp(features[n].attr_name, attr_name)) 00193 return i; 00194 } 00195 00196 return -1; 00197 } 00198 00199 00200 protected: 00202 DynArray<T_ATTRIBUTE> features; 00203 }; 00204 } 00205 #endif