external_packages/qpOASES-3.2.0/src/Indexlist.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of qpOASES.
3  *
4  * qpOASES -- An Implementation of the Online Active Set Strategy.
5  * Copyright (C) 2007-2015 by Hans Joachim Ferreau, Andreas Potschka,
6  * Christian Kirches et al. All rights reserved.
7  *
8  * qpOASES is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * qpOASES is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with qpOASES; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 
36 #include <qpOASES/Indexlist.hpp>
37 
38 
40 
41 
42 /*****************************************************************************
43  * P U B L I C *
44  *****************************************************************************/
45 
46 
47 /*
48  * I n d e x l i s t
49  */
51 {
52  number = 0;
53  iSort = 0;
54 
55  init( );
56 }
57 
58 
59 /*
60  * I n d e x l i s t
61  */
63 {
64  number = 0;
65  iSort = 0;
66 
67  init( n );
68 }
69 
70 
71 /*
72  * I n d e x l i s t
73  */
75 {
76  copy( rhs );
77 }
78 
79 
80 /*
81  * ~ I n d e x l i s t
82  */
84 {
85  clear( );
86 }
87 
88 
89 /*
90  * o p e r a t o r =
91  */
93 {
94  if ( this != &rhs )
95  {
96  clear( );
97  copy( rhs );
98  }
99 
100  return *this;
101 }
102 
103 
104 
105 /*
106  * i n i t
107  */
109  )
110 {
111  if ( n < 0 )
113 
114  clear( );
115 
116  length = 0;
117  physicallength = n;
118 
119  if ( n > 0 )
120  {
121  number = new int_t[n];
122  iSort = new int_t[n];
123  }
124 
125  return SUCCESSFUL_RETURN;
126 }
127 
128 
129 /*
130  * g e t N u m b e r A r r a y
131  */
132 returnValue Indexlist::getNumberArray( int_t** const numberarray ) const
133 {
134  if (numberarray == 0)
136 
137  *numberarray = number;
138 
139  return SUCCESSFUL_RETURN;
140 }
141 
142 
143 /*
144  * g e t I S o r t A r r a y
145  */
146 returnValue Indexlist::getISortArray( int_t** const iSortArray ) const
147 {
148  *iSortArray = iSort;
149 
150  return SUCCESSFUL_RETURN;
151 }
152 
153 
154 /*
155  * g e t I n d e x
156  */
157 int_t Indexlist::getIndex( int_t givennumber ) const
158 {
159  int_t index = findInsert(givennumber);
160  return number[iSort[index]] == givennumber ? iSort[index] : -1;
161 }
162 
163 
164 /*
165  * a d d N u m b e r
166  */
168 {
169  if ( length >= physicallength )
171 
172  int_t i, j;
173  number[length] = addnumber;
174  j = findInsert(addnumber);
175  for (i = length; i > j+1; i--)
176  iSort[i] = iSort[i-1];
177  iSort[j+1] = length;
178  ++length;
179 
180  return SUCCESSFUL_RETURN;
181 }
182 
183 
184 /*
185  * r e m o v e N u m b e r
186  */
188 {
189  int_t i;
190  int_t idx = findInsert( removenumber );
191  int_t iSidx = iSort[idx];
192 
193  /* nothing to be done if number is not contained in index set */
194  if ( number[iSidx] != removenumber )
195  return SUCCESSFUL_RETURN;
196 
197  /* update sorted indices iSort first */
198  for (i = 0; i < length; i++)
199  if (iSort[i] > iSidx) iSort[i]--;
200  for (i = idx+1; i < length; i++)
201  iSort[i-1] = iSort[i];
202 
203  /* remove from numbers list */
204  for( i=iSidx; i<length-1; ++i )
205  number[i] = number[i+1];
206  number[length-1] = -1;
207 
208  --length;
209 
210  return SUCCESSFUL_RETURN;
211 }
212 
213 
214 /*
215  * s w a p N u m b e r s
216  */
217 returnValue Indexlist::swapNumbers( int_t number1, int_t number2 )
218 {
219  int_t index1 = findInsert( number1 );
220  int_t index2 = findInsert( number2 );
221 
222  /* consistency check */
223  if ( ( number[iSort[index1]] != number1 ) || ( number[iSort[index2]] != number2 ) )
225 
226  int_t tmp;
227  /* swap numbers */
228  tmp = number[iSort[index1]];
229  number[iSort[index1]] = number[iSort[index2]];
230  number[iSort[index2]] = tmp;
231  /* swap sorting indices */
232  tmp = iSort[index1];
233  iSort[index1] = iSort[index2];
234  iSort[index2] = tmp;
235 
236  return SUCCESSFUL_RETURN;
237 }
238 
239 
240 
241 /*****************************************************************************
242  * P R O T E C T E D *
243  *****************************************************************************/
244 
245 /*
246  * c l e a r
247  */
249 {
250  if ( iSort != 0 )
251  {
252  delete[] iSort;
253  iSort = 0;
254  }
255 
256  if ( number != 0 )
257  {
258  delete[] number;
259  number = 0;
260  }
261 
262  return SUCCESSFUL_RETURN;
263 }
264 
265 
266 /*
267  * c o p y
268  */
270  )
271 {
272  int_t i;
273 
274  length = rhs.length;
276 
277  if ( rhs.number != 0 )
278  {
279  number = new int_t[physicallength];
280  for( i=0; i<physicallength; ++i )
281  number[i] = rhs.number[i];
282  iSort = new int_t[physicallength];
283  for( i=0; i<physicallength; ++i )
284  iSort[i] = rhs.iSort[i];
285  }
286  else
287  {
288  number = 0;
289  iSort = 0;
290  }
291 
292  return SUCCESSFUL_RETURN;
293 }
294 
296 {
297  /* quick check if index can be appended */
298  if (length == 0 || i < number[iSort[0]]) return -1;
299  if (i >= number[iSort[length-1]]) return length-1;
300 
301  /* otherwise, perform bisection search */
302  int_t fst = 0, lst = length-1, mid;
303 
304  while (fst < lst - 1)
305  {
306  mid = (fst + lst) / 2;
307  if (i >= number[iSort[mid]]) fst = mid;
308  else lst = mid;
309  }
310 
311  return fst;
312 }
313 
315 
316 
317 /*
318  * end of file
319  */
Allows to pass back messages to the calling function.
returnValue copy(const Indexlist &rhs)
void rhs(const real_t *x, real_t *f)
returnValue getISortArray(int_t **const iSortArray) const


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:41