-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocate.cpp
executable file
·52 lines (47 loc) · 1.15 KB
/
Locate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/////////////////////////////////////////////////////////////////////////
// Program : locate.cpp
// Coded by : Prof. Gang-Gyoo Jin, Korea Maritime University
// Coded on : 12/12/2003
#include "rmatrix.h"
/////////////////////////////////////////////////////////////////////////
// finds j such that x is between xx[j] and xx[j+1]
long ms_Locate(REAL xx[], long n, REAL x)
{
long ju,jm,jl;
int ascnd;
jl= -1; //modified
ju= n-1; //modified
if(x < xx[0]) return jl; //modified
if(x > xx[n-1]) return ju; //modified
ascnd= (xx[n-1] > xx[0]);
while(ju-jl > 1)
{
jm=(ju+jl) >> 1;
if(x > xx[jm] == ascnd)
jl=jm;
else
ju=jm;
}
return jl;
}
/////////////////////////////////////////////////////////////////////////
// finds j such that x is between xx[j] and xx[j+1]
long ms_Locate(const RVECTOR &xx, REAL x)
{
long ju,jm,jl,n=xx.num;
int ascnd;
jl= -1; //modified
ju= n-1; //modified
if(x < xx(0)) return jl; //modified
if(x > xx(n-1)) return ju; //modified
ascnd= (xx(n-1) > xx(0));
while(ju-jl > 1)
{
jm=(ju+jl) >> 1;
if(x > xx(jm) == ascnd)
jl=jm;
else
ju=jm;
}
return jl;
}