SHOGUN  6.1.3
SubsetStack.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012-2014 Heiko Strathmann
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
32 #include <shogun/io/SGIO.h>
33 #include <shogun/base/Parameter.h>
34 
35 using namespace shogun;
36 
38 {
39  init();
40 }
41 
43 {
44  init();
45 
46  for (int32_t i=0; i < other.m_active_subsets_stack->get_num_elements(); ++i)
47  {
48  auto subset = other.m_active_subsets_stack->get_element(i);
49  m_active_subsets_stack->append_element(subset);
50  SG_UNREF(subset)
51  }
52  m_active_subset = other.m_active_subset;
53  SG_REF(m_active_subset)
54 }
55 
57 {
58  SG_UNREF(m_active_subsets_stack);
59  SG_UNREF(m_active_subset);
60 }
61 
63 {
64  /* delete all active subsets, backwards due to DynArray implementation */
65  for (index_t i=m_active_subsets_stack->get_num_elements()-1; i>=0; --i)
66  m_active_subsets_stack->delete_element(i);
67 
68  SG_UNREF(m_active_subset);
69  m_active_subset = nullptr;
70 }
71 
72 void CSubsetStack::init()
73 {
74  SG_ADD((CSGObject**)&m_active_subset, "active_subset",
75  "Currently active subset", MS_NOT_AVAILABLE);
76  SG_ADD((CSGObject**)&m_active_subsets_stack, "active_subsets_stack",
77  "Stack of active subsets", MS_NOT_AVAILABLE);
78 
79  m_active_subset=NULL;
80  m_active_subsets_stack=new CDynamicObjectArray();
81  SG_REF(m_active_subsets_stack);
82 }
83 
85 {
86  /* if there are already subsets on stack, do some legality checks */
87  if (m_active_subsets_stack->get_num_elements())
88  {
89  /* check that subsets may only be smaller or equal than existing */
90  CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element();
91  if (subset.vlen>latest->m_subset_idx.vlen)
92  {
93  subset.display_vector("subset");
94  latest->m_subset_idx.display_vector("last on stack");
95  SG_ERROR("%s::add_subset(): Provided index vector is "
96  "larger than the subsets on the stubset stack!\n", get_name());
97  }
98 
99  /* check for range of indices */
100  index_t max_index=CMath::max(subset.vector, subset.vlen);
101  if (max_index>=latest->m_subset_idx.vlen)
102  {
103  subset.display_vector("subset");
104  latest->m_subset_idx.display_vector("last on stack");
105  SG_ERROR("%s::add_subset(): Provided index vector contains"
106  " indices larger than possible range!\n", get_name());
107  }
108 
109  /* clean up */
110  SG_UNREF(latest);
111  }
112 
113  /* active subset will be changed anyway, no setting to NULL */
114  SG_UNREF(m_active_subset);
115 
116  /* two cases: stack is empty/stack is not empty */
117  if (m_active_subsets_stack->get_num_elements())
118  {
119  /* if there are already subsets, we need to map given one through
120  * existing ones */
121 
122  /* get latest current subset */
123  CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element();
124 
125  /* create new index vector */
126  SGVector<index_t> new_active_subset=SGVector<index_t>(subset.vlen);
127 
128  /* using the latest current subset, transform all indices by the latest
129  * added subset (dynamic programming greets you) */
130  for (index_t i=0; i<subset.vlen; ++i)
131  {
132  new_active_subset.vector[i]=
133  latest->m_subset_idx.vector[subset.vector[i]];
134  }
135 
136  /* replace active subset */
137  m_active_subset=new CSubset(new_active_subset);
138  SG_REF(m_active_subset);
139  SG_UNREF(latest);
140  }
141  else
142  {
143  /* just use plain given subset since there is nothing to map */
144  m_active_subset=new CSubset(subset);
145  SG_REF(m_active_subset);
146  }
147 
148  /* add current active subset on stack of active subsets in any case */
149  m_active_subsets_stack->append_element(m_active_subset);
150 }
151 
153 {
155 }
156 
158 {
159  index_t num_subsets=m_active_subsets_stack->get_num_elements();
160  if (num_subsets)
161  {
162  /* unref current subset */
163  SG_UNREF(m_active_subset);
164  m_active_subset=NULL;
165 
166  /* delete last element on stack */
167  if (num_subsets>=1)
168  {
169  index_t last_idx=m_active_subsets_stack->get_num_elements()-1;
170  m_active_subsets_stack->delete_element(last_idx);
171  }
172 
173  /* if there are subsets left on stack, set the next one as active */
174  if (num_subsets>1)
175  {
176  /* use new last element on stack as active subset */
177  index_t last_idx=m_active_subsets_stack->get_num_elements()-1;
178  m_active_subset=(CSubset*)
179  m_active_subsets_stack->get_element(last_idx);
180  }
181 
182  /* otherwise, active subset is just empty */
183  }
184 
185  /* do nothing if nothing on stack */
186 }
Wrapper class for an index subset which is used by SubsetStack.
Definition: Subset.h:24
static T max(T a, T b)
Definition: Math.h:149
int32_t index_t
Definition: common.h:72
const char * get_name() const
Definition: SubsetStack.h:52
#define SG_ERROR(...)
Definition: SGIO.h:128
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:138
#define SG_REF(x)
Definition: SGObject.h:52
class to add subset support to another class. A CSubsetStackStack instance should be added and wrappe...
Definition: SubsetStack.h:37
virtual void add_subset(SGVector< index_t > subset)
Definition: SubsetStack.cpp:84
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:124
virtual void remove_all_subsets()
Definition: SubsetStack.cpp:62
virtual void add_subset_in_place(SGVector< index_t > subset)
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
#define SG_UNREF(x)
Definition: SGObject.h:53
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CSGObject * get_element(int32_t index) const
virtual void remove_subset()
#define SG_ADD(...)
Definition: SGObject.h:93
void display_vector(const char *name="vector", const char *prefix="") const
Definition: SGVector.cpp:411
CSGObject * get_last_element() const
index_t vlen
Definition: SGVector.h:571
bool append_element(CSGObject *e)

SHOGUN Machine Learning Toolbox - Documentation