SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
AttributeFeatures.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
13 #include <shogun/lib/memory.h>
14 
15 using namespace shogun;
16 
18 : CFeatures(0)
19 {
20 }
21 
23 {
24  int32_t idx=find_attr_index(attr_name);
25  if (idx>=0)
26  {
27  CFeatures* f=features[idx].attr_obj;
28  SG_REF(f);
29  return f;
30  }
31 
32  return NULL;
33 }
34 
35 void CAttributeFeatures::get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj)
36 {
37  T_ATTRIBUTE a= features.get_element_safe(idx);
38  attr_name= a.attr_name;
39  attr_obj= a.attr_obj;
40  SG_REF(a.attr_obj);
41 }
42 
43 bool CAttributeFeatures::set_attribute(char* attr_name, CFeatures* attr_obj)
44 {
45  int32_t idx=find_attr_index(attr_name);
46  if (idx==-1)
48 
49  T_ATTRIBUTE a;
50  a.attr_name=get_strdup(attr_name);
51  a.attr_obj=attr_obj;
52 
53  SG_REF(attr_obj);
54 
55  return features.set_element(a, idx);
56 }
57 
58 bool CAttributeFeatures::del_attribute(char* attr_name)
59 {
60  int32_t idx=find_attr_index(attr_name);
61 
62  if (idx>=0)
63  {
64  T_ATTRIBUTE a= features[idx];
65  SG_FREE(a.attr_name);
66  SG_UNREF(a.attr_obj);
67  return true;
68  }
69  return false;
70 }
71 
73 {
74  return features.get_num_elements();
75 }
76 
77 int32_t CAttributeFeatures::find_attr_index(char* attr_name)
78 {
79  int32_t n=features.get_num_elements();
80  for (int32_t i=0; i<n; i++)
81  {
82  if (!strcmp(features[n].attr_name, attr_name))
83  return i;
84  }
85 
86  return -1;
87 }
88 
90 {
91  int32_t n=features.get_num_elements();
92  for (int32_t i=0; i<n; i++)
93  SG_UNREF_NO_NULL(features[i].attr_obj);
94 }
CFeatures * get_attribute(char *attr_name)
int32_t find_attr_index(char *attr_name)
void get_attribute_by_index(int idx, const char *&attr_name, CFeatures *&attr_obj)
bool set_attribute(char *attr_name, CFeatures *attr_obj)
#define SG_UNREF_NO_NULL(x)
Definition: SGObject.h:53
int32_t get_num_elements() const
Definition: DynArray.h:130
#define SG_REF(x)
Definition: SGObject.h:51
bool set_element(T element, int32_t index)
Definition: DynArray.h:191
#define SG_UNREF(x)
Definition: SGObject.h:52
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
DynArray< T_ATTRIBUTE > features
list of attributes (sorted)
The class Features is the base class of all feature objects.
Definition: Features.h:68
bool del_attribute(char *attr_name)
T get_element_safe(int32_t index) const
Definition: DynArray.h:175

SHOGUN Machine Learning Toolbox - Documentation