SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TreeMachineNode.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012 Chiyuan Zhang
4  * Written (w) 2014 Parijat Mazumdar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #ifndef TREEMACHINENODE_H__
33 #define TREEMACHINENODE_H__
34 
35 #include <shogun/lib/config.h>
36 
37 #include <shogun/base/SGObject.h>
38 #include <shogun/base/Parameter.h>
40 
41 namespace shogun
42 {
43 
51 template <typename T>
53  : public CSGObject
54 {
55 public:
58  {
59  init();
60  }
61 
64  {
65  for(int32_t i=0;i<m_children->get_num_elements();i++)
66  {
68  child->parent(NULL);
69  SG_UNREF(child);
70  }
72  }
73 
77  virtual const char* get_name() const { return "TreeMachineNode"; }
78 
82  void machine(int32_t idx)
83  {
84  m_machine=idx;
85  }
86 
90  int32_t machine()
91  {
92  return m_machine;
93  }
94 
99  {
100  m_parent=par;
101  }
102 
107  {
108  return m_parent;
109  }
110 
114  virtual void set_children(CDynamicObjectArray* children)
115  {
117  for (int32_t i=0; i<children->get_num_elements(); i++)
118  {
119  CTreeMachineNode* child=(CTreeMachineNode*) children->get_element(i);
120  add_child(child);
121  SG_UNREF(child);
122  }
123  }
124 
128  virtual void add_child(CTreeMachineNode* child)
129  {
130  m_children->push_back(child);
131  child->parent(this);
132  }
133 
138  {
140  return m_children;
141  }
142 
144  typedef void (*data_print_func_t) (const T&);
145 
149  void debug_print(data_print_func_t data_print_func)
150  {
151  debug_print_impl(data_print_func, this, 0);
152  }
153 
154 protected:
160  static void debug_print_impl(data_print_func_t data_print_func,
161  CTreeMachineNode<T>* node, int32_t depth)
162  {
163  for (int32_t i=0;i<depth;++i)
164  SG_SPRINT(" ");
165 
166  data_print_func(node->data);
167 
168  CDynamicObjectArray* children_vector=node->get_children();
169  for (int32_t j=0;j<children_vector->get_num_elements();j++)
170  {
172  children_vector->get_element(j);
173  debug_print_impl(data_print_func,child,depth+1);
174  SG_UNREF(child);
175  }
176 
177  SG_UNREF(children_vector);
178  }
179 
180 private:
182  void init()
183  {
184  m_parent=NULL;
185  m_machine=-1;
188  SG_ADD((CSGObject**)&m_parent,"m_parent", "Parent node", MS_NOT_AVAILABLE);
189  SG_ADD(&m_machine,"m_machine", "Index of associated machine", MS_NOT_AVAILABLE);
190  }
191 
192 public:
194  T data;
195 
196 protected:
199 
201  int32_t m_machine;
202 
205 
206 };
207 
208 } /* namespace shogun */
209 
210 #endif /* end of include guard: TREEMACHINENODE_H__ */
211 

SHOGUN Machine Learning Toolbox - Documentation