SDDSlib
Loading...
Searching...
No Matches
findMinMax.c File Reference

Provides functions to find minimum and maximum values in arrays. More...

#include "mdb.h"

Go to the source code of this file.

Functions

int find_min_max (double *min, double *max, double *list, int64_t n)
 Finds the minimum and maximum values in a list of doubles.
 
int update_min_max (double *min, double *max, double *list, int64_t n, int32_t reset)
 Updates the minimum and maximum values based on a list of doubles.
 
int index_min_max (int64_t *imin, int64_t *imax, double *list, int64_t n)
 Finds the indices of the minimum and maximum values in a list of doubles.
 
int index_min_max_long (int64_t *imin, int64_t *imax, long *list, int64_t n)
 Finds the indices of the minimum and maximum values in a list of longs.
 
int assign_min_max (double *min, double *max, double val)
 
int find_min_max_2d (double *min, double *max, double **value, long n1, long n2)
 
int find_min_max_2d_float (float *min, float *max, float **value, long n1, long n2)
 
int find_min (double *min, double *loc, double *c1, double *c2, long n)
 
int find_max (double *max, double *loc, double *c1, double *c2, long n)
 
double max_in_array (double *array, long n)
 Finds the maximum value in an array of doubles.
 
double min_in_array (double *array, long n)
 Finds the minimum value in an array of doubles.
 

Detailed Description

Provides functions to find minimum and maximum values in arrays.

This file contains functions to find the minimum and maximum values in one-dimensional and two-dimensional arrays, as well as functions to find the indices of these values and perform assignments based on comparisons.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file findMinMax.c.

Function Documentation

◆ assign_min_max()

int assign_min_max ( double * min,
double * max,
double val )

Definition at line 197 of file findMinMax.c.

197 {
198 int flag = 0;
199
200 if (!min || !max)
201 return (0);
202 flag |= 1;
203 if (*min > val) {
204 *min = val;
205 flag |= 2;
206 }
207 if (*max < val) {
208 *max = val;
209 flag |= 4;
210 }
211 return (flag);
212}

◆ find_max()

int find_max ( double * max,
double * loc,
double * c1,
double * c2,
long n )

Definition at line 291 of file findMinMax.c.

293 {
294 long i;
295 double val;
296
297 if (!n || !c1 || !c2 || !loc || !max)
298 return (0);
299 *max = -DBL_MAX;
300 for (i = 0; i < n; i++) {
301 if ((val = c2[i]) > *max) {
302 *max = val;
303 *loc = c1[i];
304 }
305 }
306 return (1);
307}

◆ find_min()

int find_min ( double * min,
double * loc,
double * c1,
double * c2,
long n )

Definition at line 272 of file findMinMax.c.

274 {
275 long i;
276 double val;
277
278 if (!n || !loc || !c1 || !c2)
279 return (0);
280
281 *min = DBL_MAX;
282 for (i = 0; i < n; i++) {
283 if ((val = c2[i]) < *min) {
284 *min = val;
285 *loc = c1[i];
286 }
287 }
288 return (1);
289}

◆ find_min_max()

int find_min_max ( double * min,
double * max,
double * list,
int64_t n )

Finds the minimum and maximum values in a list of doubles.

This function iterates through the provided list of doubles to determine the minimum and maximum values. The results are stored in the provided pointers if they are not NULL.

Parameters
minPointer to store the minimum value found. Can be NULL if not needed.
maxPointer to store the maximum value found. Can be NULL if not needed.
listPointer to the array of doubles to search.
nNumber of elements in the list.
Returns
Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).

Definition at line 33 of file findMinMax.c.

35 {
36 register int64_t i;
37 register double lo, hi, val;
38
39 if (!n || !list)
40 return (0);
41 if (!min && !max)
42 return 0;
43
44 lo = DBL_MAX;
45 hi = -DBL_MAX;
46 for (i = 0; i < n; i++) {
47 if ((val = list[i]) < lo)
48 lo = val;
49 if (val > hi)
50 hi = val;
51 }
52 if (min)
53 *min = lo;
54 if (max)
55 *max = hi;
56 return (1);
57}

◆ find_min_max_2d()

int find_min_max_2d ( double * min,
double * max,
double ** value,
long n1,
long n2 )

Definition at line 219 of file findMinMax.c.

220 {
221 double data, rmin, rmax, *value_i1;
222 long i1, i2;
223
224 if (!n1 || !n2 || !min || !max || !value)
225 return (0);
226
227 rmin = DBL_MAX;
228 rmax = -DBL_MAX;
229 for (i1 = 0; i1 < n1; i1++) {
230 if (!(value_i1 = value[i1]))
231 return (0);
232 for (i2 = 0; i2 < n2; i2++) {
233 if ((data = value_i1[i2]) > rmax)
234 rmax = data;
235 if (data < rmin)
236 rmin = data;
237 }
238 }
239 return (1);
240}

◆ find_min_max_2d_float()

int find_min_max_2d_float ( float * min,
float * max,
float ** value,
long n1,
long n2 )

Definition at line 247 of file findMinMax.c.

248 {
249 float data, rmin, rmax, *value_i1;
250 long i1, i2;
251
252 if (!n1 || !n2 || !min || !max || !value)
253 return (0);
254
255 rmin = FLT_MAX;
256 rmax = -FLT_MAX;
257 for (i1 = 0; i1 < n1; i1++) {
258 if (!(value_i1 = value[i1]))
259 return (0);
260 for (i2 = 0; i2 < n2; i2++) {
261 if ((data = value_i1[i2]) > rmax)
262 rmax = data;
263 if (data < rmin)
264 rmin = data;
265 }
266 }
267 *min = rmin;
268 *max = rmax;
269 return (1);
270}

◆ index_min_max()

int index_min_max ( int64_t * imin,
int64_t * imax,
double * list,
int64_t n )

Finds the indices of the minimum and maximum values in a list of doubles.

This function iterates through the provided list of doubles to determine the indices of the minimum and maximum values. The indices are stored in the provided pointers if they are not NULL.

Parameters
iminPointer to store the index of the minimum value. Can be NULL if not needed.
imaxPointer to store the index of the maximum value. Can be NULL if not needed.
listPointer to the array of doubles to search.
nNumber of elements in the list.
Returns
Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).

Definition at line 116 of file findMinMax.c.

117 {
118 register int64_t i;
119 register double lo, hi, val;
120 int64_t iMin, iMax;
121
122 if (!n || !list)
123 return (0);
124 if (!imin && !imax)
125 return 0;
126
127 lo = DBL_MAX;
128 hi = -DBL_MAX;
129 iMin = iMax = 0;
130 for (i = 0; i < n; i++) {
131 if ((val = list[i]) < lo) {
132 iMin = i;
133 lo = val;
134 }
135 if (val > hi) {
136 hi = val;
137 iMax = i;
138 }
139 }
140 if (imin)
141 *imin = iMin;
142 if (imax)
143 *imax = iMax;
144
145 return (1);
146}

◆ index_min_max_long()

int index_min_max_long ( int64_t * imin,
int64_t * imax,
long * list,
int64_t n )

Finds the indices of the minimum and maximum values in a list of longs.

This function iterates through the provided list of longs to determine the indices of the minimum and maximum values. The indices are stored in the provided pointers if they are not NULL.

Parameters
iminPointer to store the index of the minimum value. Can be NULL if not needed.
imaxPointer to store the index of the maximum value. Can be NULL if not needed.
listPointer to the array of longs to search.
nNumber of elements in the list.
Returns
Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).

Definition at line 160 of file findMinMax.c.

161 {
162 register int64_t i;
163 register long lo, hi, val;
164 int64_t iMin, iMax;
165
166 if (!n || !list)
167 return (0);
168 if (!imin && !imax)
169 return 0;
170
171 lo = LONG_MAX;
172 hi = -LONG_MAX;
173 iMin = iMax = 0;
174 for (i = 0; i < n; i++) {
175 if ((val = list[i]) < lo) {
176 iMin = i;
177 lo = val;
178 }
179 if (val > hi) {
180 hi = val;
181 iMax = i;
182 }
183 }
184 if (imin)
185 *imin = iMin;
186 if (imax)
187 *imax = iMax;
188
189 return (1);
190}

◆ max_in_array()

double max_in_array ( double * array,
long n )

Finds the maximum value in an array of doubles.

This function iterates through the provided array of doubles to determine the maximum value.

Parameters
arrayPointer to the array of doubles to search.
nNumber of elements in the array.
Returns
Returns the maximum value found in the array. If the array is empty, returns -DBL_MAX.

Definition at line 318 of file findMinMax.c.

318 {
319 double max = -DBL_MAX;
320
321 while (n--)
322 if (array[n] > max)
323 max = array[n];
324 return (max);
325}

◆ min_in_array()

double min_in_array ( double * array,
long n )

Finds the minimum value in an array of doubles.

This function iterates through the provided array of doubles to determine the minimum value.

Parameters
arrayPointer to the array of doubles to search.
nNumber of elements in the array.
Returns
Returns the minimum value found in the array. If the array is empty, returns DBL_MAX.

Definition at line 336 of file findMinMax.c.

336 {
337 double min = DBL_MAX;
338
339 while (n--)
340 if (array[n] < min)
341 min = array[n];
342 return (min);
343}

◆ update_min_max()

int update_min_max ( double * min,
double * max,
double * list,
int64_t n,
int32_t reset )

Updates the minimum and maximum values based on a list of doubles.

This function iterates through the provided list of doubles to update the minimum and maximum values. If the 'reset' flag is non-zero, the function resets the current min and max before processing the list.

Parameters
minPointer to the current minimum value. If 'reset' is non-zero, this will be set to the new minimum.
maxPointer to the current maximum value. If 'reset' is non-zero, this will be set to the new maximum.
listPointer to the array of doubles to process.
nNumber of elements in the list.
resetFlag indicating whether to reset the current min and max before updating. Non-zero to reset.
Returns
Returns 1 on success, 0 on failure (e.g., if n <= 0 or list is NULL).

Definition at line 72 of file findMinMax.c.

74 {
75 register int64_t i;
76 register double lo, hi, val;
77
78 if (!n || !list)
79 return (0);
80 if (!min && !max)
81 return 0;
82
83 if (reset) {
84 lo = DBL_MAX;
85 hi = -DBL_MAX;
86 } else {
87 lo = *min;
88 hi = *max;
89 }
90
91 for (i = 0; i < n; i++) {
92 if ((val = list[i]) < lo)
93 lo = val;
94 if (val > hi)
95 hi = val;
96 }
97 if (min)
98 *min = lo;
99 if (max)
100 *max = hi;
101 return (1);
102}