-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsearch.c
171 lines (160 loc) · 3.54 KB
/
csearch.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "mex.h"
#include "csearch.h"
#include "string.h"
enum relop{lt, le, gt, ge};
int validateInput(int nrhs, const mxArray* prhs[], size_t* num, int* op)
{
char s[3];
// Validate # of Inputs
if (nrhs < 3) {
printf("Error using csearch\nNot enough input arguments.\n");
return 0;
}
else if (nrhs > 3) {
printf("Error using csearch\nToo many input arguments.\n");
return 0;
}
// Validate 1st Input
if (!mxIsNumeric(prhs[0])) {
printf("First Input Must be a Numeric Array\n");
return 0;
}
else if (mxIsComplex(prhs[0])) {
printf("Complex Data Not Supported\n");
return 0;
}
else {
*num = mxGetNumberOfElements(prhs[0]);
if (*num < 2) {
printf("Input Array Must Have More Than 1 Element");
return 0;
}
}
// Validate 3rd Input
if (!mxIsNumeric(prhs[2])) {
printf("Third Input Must be Numeric\n");
return 0;
}
// Validate 2nd Input
if (!mxIsChar(prhs[1])) {
printf("Second Input Must be an Operator String (lt, le, gt, ge)\n");
return 0;
}
else {
mxGetString(prhs[1], s, 3);
if (strcmp(s, "lt") == 0) {
*op = lt;
return 1;
}
else if (strcmp(s, "le") == 0) {
*op = le;
return 1;
}
else if (strcmp(s, "gt") == 0) {
*op = gt;
return 1;
}
else if (strcmp(s, "ge") == 0) {
*op = ge;
return 1;
}
else {
printf("Second Input Must be A Relational Operator (lt, le, gt, ge)\n");
return 0;
}
}
}
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
// validate inputs to mexFunction
int op;
size_t num;
int isOK = validateInput(nrhs, prhs, &num, &op);
if (!isOK) {
mexErrMsgTxt("Input Validation Failed");
}
int64_t argmin;
// x_min : lower limit
// x_max : upper limit
// matlab equivalent function calls:
// argmin = find(x>x_min, 1, 'first');
// argmax = find(x<x_max, 1, 'last');
// create output array of indices
plhs[0] = mxCreateNumericMatrix(1, 1, mxINT64_CLASS, mxREAL);
int64_t* idx_out = (int64_t*)mxGetData(plhs[0]);
unsigned char* p;
unsigned char* x = (unsigned char*)mxGetData(prhs[0]);
unsigned char* y = (unsigned char*)mxGetData(prhs[2]);
size_t sz = 1;
int (*cmp)(const void* key, const void* elt);
if (mxIsDouble(prhs[0])) {
cmp = cmp_double;
sz = sizeof(double);
}
else if (mxIsSingle(prhs[0])) {
cmp = cmp_float;
sz = sizeof(float);
}
else if (mxIsInt8(prhs[0])) {
cmp = cmp_int8;
sz = sizeof(int8_t);
}
else if (mxIsUint8(prhs[0])) {
cmp = cmp_uint8;
sz = sizeof(uint8_t);
}
else if (mxIsInt16(prhs[0])) {
cmp = cmp_int16;
sz = sizeof(int16_t);
}
else if (mxIsUint16(prhs[0])) {
cmp = cmp_uint16;
sz = sizeof(uint16_t);
}
else if (mxIsInt32(prhs[0])) {
cmp = cmp_int32;
sz = sizeof(int32_t);
}
else if (mxIsUint32(prhs[0])) {
cmp = cmp_uint32;
sz = sizeof(uint32_t);
}
else if (mxIsInt64(prhs[0])) {
cmp = cmp_int64;
sz = sizeof(int64_t);
}
else if (mxIsUint64(prhs[0])) {
cmp = cmp_uint64;
sz = sizeof(uint64_t);
}
else {
mexErrMsgTxt("Incompatible Type. Input Array Must Be Integer, Float, or Double\n");
}
switch (op) {
case 0:
p = (unsigned char*)bsearch_lt(y, x, num, sz, cmp);
break;
case 1:
p = (unsigned char*)bsearch_le(y, x, num, sz, cmp);
break;
case 2:
p = (unsigned char*)bsearch_gt(y, x, num, sz, cmp);
break;
case 3:
p = (unsigned char*)bsearch_ge(y, x, num, sz, cmp);
break;
default:
p = NULL;
}
if (p != NULL) {
//argmin = p - x;
argmin = (p - x) / sz;
}
else {
argmin = -1;
}
// fill and send idx_out back to matlab
// shift the indices from zero-based to one-based
idx_out[0] = argmin + 1;
//idx_out[1] = argmax + 1;
}