SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
14 #include <string.h>
15 
16 
17 using namespace shogun;
18 
20 : CFeatures(0)
21 {
22 }
23 
25 {
26  int32_t idx=find_attr_index(attr_name);
27  if (idx>=0)
28  {
29  CFeatures* f=features[idx].attr_obj;
30  SG_REF(f);
31  return f;
32  }
33 
34  return NULL;
35 }
36 
37 void CAttributeFeatures::get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj)
38 {
39  T_ATTRIBUTE a= features.get_element_safe(idx);
40  attr_name= a.attr_name;
41  attr_obj= a.attr_obj;
42  SG_REF(a.attr_obj);
43 }
44 
45 bool CAttributeFeatures::set_attribute(char* attr_name, CFeatures* attr_obj)
46 {
47  int32_t idx=find_attr_index(attr_name);
48  if (idx==-1)
50 
51  T_ATTRIBUTE a;
52  a.attr_name=strdup(attr_name);
53  a.attr_obj=attr_obj;
54 
55  SG_REF(attr_obj);
56 
57  return features.set_element(a, idx);
58 }
59 
60 bool CAttributeFeatures::del_attribute(char* attr_name)
61 {
62  int32_t idx=find_attr_index(attr_name);
63 
64  if (idx>=0)
65  {
66  T_ATTRIBUTE a= features[idx];
67  SG_FREE(a.attr_name);
68  SG_UNREF(a.attr_obj);
69  return true;
70  }
71  return false;
72 }
73 
75 {
76  return features.get_num_elements();
77 }
78 
79 int32_t CAttributeFeatures::find_attr_index(char* attr_name)
80 {
81  int32_t n=features.get_num_elements();
82  for (int32_t i=0; i<n; i++)
83  {
84  if (!strcmp(features[n].attr_name, attr_name))
85  return i;
86  }
87 
88  return -1;
89 }
90 
92 {
93  int32_t n=features.get_num_elements();
94  for (int32_t i=0; i<n; i++)
95  SG_UNREF_NO_NULL(features[i].attr_obj);
96 }

SHOGUN Machine Learning Toolbox - Documentation