AttributeFeatures.h

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) 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 "lib/DynamicArray.h"
00017 
00018 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00019 
00020 struct T_ATTRIBUTE
00021 {
00023     char* attr_name;
00025     CFeatures* attr_obj;
00026 };
00027 #endif // DOXYGEN_SHOULD_SKIP_THIS
00028 
00043 class CAttributeFeatures : public CFeatures
00044 {
00045 
00046 public:
00048     CAttributeFeatures();
00049 
00051     virtual ~CAttributeFeatures();
00052 
00058     CFeatures* get_attribute(char* attr_name)
00059     {
00060         int32_t idx=find_attr_index(attr_name);
00061         if (idx>=0)
00062         {
00063             CFeatures* f=features[idx].attr_obj;
00064             SG_REF(f);
00065             return f;
00066         }
00067 
00068         return NULL;
00069     }
00070 
00077     inline void get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj)
00078     {
00079         T_ATTRIBUTE a= features.get_element_safe(idx);
00080         attr_name= a.attr_name;
00081         attr_obj= a.attr_obj;
00082         SG_REF(a.attr_obj);
00083     }
00084 
00091     inline bool set_attribute(char* attr_name, CFeatures* attr_obj)
00092     {
00093         int32_t idx=find_attr_index(attr_name);
00094         if (idx==-1)
00095             idx=features.get_num_elements();
00096 
00097         T_ATTRIBUTE a;
00098         a.attr_name=strdup(attr_name);
00099         a.attr_obj=attr_obj;
00100 
00101         SG_REF(attr_obj);
00102 
00103         return features.set_element(a, idx);
00104     }
00105 
00111     inline bool del_attribute(char* attr_name)
00112     {
00113         int32_t idx=find_attr_index(attr_name);
00114 
00115         if (idx>=0)
00116         {
00117             T_ATTRIBUTE a= features[idx];
00118             free(a.attr_name);
00119             SG_UNREF(a.attr_obj);
00120             return true;
00121         }
00122         return false;
00123     }
00124 
00125 
00130     inline int32_t get_num_attributes()
00131     {
00132         return features.get_num_elements();
00133     }
00134 
00136     inline virtual const char* get_name() const { return "AttributeFeatures"; }
00137 
00138 protected:
00144     inline int32_t find_attr_index(char* attr_name)
00145     {
00146         int32_t n=features.get_num_elements();
00147         for (int32_t i=0; i<n; i++)
00148         {
00149             if (!strcmp(features[n].attr_name, attr_name))
00150                 return i;
00151         }
00152 
00153         return -1;
00154     }
00155 
00156 
00157 protected:
00159     CDynamicArray<T_ATTRIBUTE> features;
00160 };
00161 #endif

SHOGUN Machine Learning Toolbox - Documentation