external_packages/qpOASES-3.0beta/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-2011 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[n];
122  iSort = new int[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** 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 n d e x
145  */
146 int Indexlist::getIndex( int givennumber ) const
147 {
148  int index = findInsert(givennumber);
149  return number[iSort[index]] == givennumber ? iSort[index] : -1;
150 }
151 
152 
153 /*
154  * a d d N u m b e r
155  */
156 returnValue Indexlist::addNumber( int addnumber )
157 {
158  if ( length >= physicallength )
160 
161  int i, j;
162  number[length] = addnumber;
163  j = findInsert(addnumber);
164  for (i = length; i > j+1; i--)
165  iSort[i] = iSort[i-1];
166  iSort[j+1] = length;
167  ++length;
168 
169  return SUCCESSFUL_RETURN;
170 }
171 
172 
173 /*
174  * r e m o v e N u m b e r
175  */
176 returnValue Indexlist::removeNumber( int removenumber )
177 {
178  int i;
179  int idx = findInsert( removenumber );
180  int iSidx = iSort[idx];
181 
182  /* nothing to be done if number is not contained in index set */
183  if ( number[iSidx] != removenumber )
184  return SUCCESSFUL_RETURN;
185 
186  /* update sorted indices iSort first */
187  for (i = 0; i < length; i++)
188  if (iSort[i] > iSidx) iSort[i]--;
189  for (i = idx+1; i < length; i++)
190  iSort[i-1] = iSort[i];
191 
192  /* remove from numbers list */
193  for( i=iSidx; i<length-1; ++i )
194  number[i] = number[i+1];
195  number[length-1] = -1;
196 
197  --length;
198 
199  return SUCCESSFUL_RETURN;
200 }
201 
202 
203 /*
204  * s w a p N u m b e r s
205  */
206 returnValue Indexlist::swapNumbers( int number1, int number2 )
207 {
208  int index1 = findInsert( number1 );
209  int index2 = findInsert( number2 );
210 
211  /* consistency check */
212  if ( ( number[iSort[index1]] != number1 ) || ( number[iSort[index2]] != number2 ) )
214 
215  int tmp;
216  /* swap numbers */
217  tmp = number[iSort[index1]];
218  number[iSort[index1]] = number[iSort[index2]];
219  number[iSort[index2]] = tmp;
220  /* swap sorting indices */
221  tmp = iSort[index1];
222  iSort[index1] = iSort[index2];
223  iSort[index2] = tmp;
224 
225  return SUCCESSFUL_RETURN;
226 }
227 
228 
229 
230 /*****************************************************************************
231  * P R O T E C T E D *
232  *****************************************************************************/
233 
234 /*
235  * c l e a r
236  */
238 {
239  if ( iSort != 0 )
240  {
241  delete[] iSort;
242  iSort = 0;
243  }
244 
245  if ( number != 0 )
246  {
247  delete[] number;
248  number = 0;
249  }
250 
251  return SUCCESSFUL_RETURN;
252 }
253 
254 
255 /*
256  * c o p y
257  */
259  )
260 {
261  int i;
262 
263  length = rhs.length;
265 
266  if ( rhs.number != 0 )
267  {
268  number = new int[physicallength];
269  for( i=0; i<physicallength; ++i )
270  number[i] = rhs.number[i];
271  iSort = new int[physicallength];
272  for( i=0; i<physicallength; ++i )
273  iSort[i] = rhs.iSort[i];
274  }
275  else
276  {
277  number = 0;
278  iSort = 0;
279  }
280 
281  return SUCCESSFUL_RETURN;
282 }
283 
284 int Indexlist::findInsert(int i) const
285 {
286  /* quick check if index can be appended */
287  if (length == 0 || i < number[iSort[0]]) return -1;
288  if (i >= number[iSort[length-1]]) return length-1;
289 
290  /* otherwise, perform bisection search */
291  int fst = 0, lst = length-1, mid;
292 
293  while (fst < lst - 1)
294  {
295  mid = (fst + lst) / 2;
296  if (i >= number[iSort[mid]]) fst = mid;
297  else lst = mid;
298  }
299 
300  return fst;
301 }
302 
304 
305 
306 /*
307  * end of file
308  */
Allows to pass back messages to the calling function.
returnValue copy(const Indexlist &rhs)
void rhs(const real_t *x, real_t *f)


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