public API
(
switch to internal
)
-
revision 3.6.7
casadi
core
runtime
runtime/casadi_low.hpp
1
//
2
// MIT No Attribution
3
//
4
// Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
5
//
6
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
7
// software and associated documentation files (the "Software"), to deal in the Software
8
// without restriction, including without limitation the rights to use, copy, modify,
9
// merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10
// permit persons to whom the Software is furnished to do so.
11
//
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
//
19
20
21
// SYMBOL "low"
22
template
<
typename
T1>
23
casadi_int casadi_low(T1 x,
const
T1* grid, casadi_int ng, casadi_int lookup_mode) {
24
switch
(lookup_mode) {
25
case
1:
26
{
27
T1 g0, dg;
28
casadi_int ret;
29
g0 = grid[0];
30
dg = grid[ng-1]-g0;
31
ret = (casadi_int) ((x-g0)*(ng-1)/dg);
// NOLINT(readability/casting)
32
if
(ret<0) ret=0;
33
if
(ret>ng-2) ret=ng-2;
34
return
ret;
35
}
36
case
2:
37
{
38
casadi_int start, stop, pivot;
39
// Quick return
40
if
(ng<2 || x<grid[1])
return
0;
41
if
(x>grid[ng-1])
return
ng-2;
42
43
start = 0;
44
stop = ng-1;
45
while
(1) {
46
pivot = (stop+start)/2;
47
if
(x < grid[pivot]) {
48
if
(pivot==stop)
return
pivot;
49
stop = pivot;
50
}
else
{
51
if
(pivot==start)
return
pivot;
52
start = pivot;
53
}
54
}
55
}
56
default
:
// linear
57
{
58
casadi_int i;
59
for
(i=0; i<ng-2; ++i) {
60
if
(x < grid[i+1])
break
;
61
}
62
return
i;
63
}
64
}
65
}