Debian Codegraph @

packages/seacas/libraries/chaco/eigen/scanmin.c

raw
1 /*
2 * Copyright(C) 1999-2020 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "structs.h" // for scanlink
10 #include <stdio.h> // for NULL
11
12 /* Return minimum of vector over range */
13 void scanmin(double *vec, /* vector to scan */
14 int beg, int end, /* index range */
15 struct scanlink **scanlist /* pntr to list holding results of scan */
16 )
17 {
18 extern double DOUBLE_MAX;
19 struct scanlink *top;
20 struct scanlink *curlnk;
21 struct scanlink *prevlnk;
22 double val;
23 int i;
24
25 curlnk = *scanlist;
26 while (curlnk != NULL) {
27 curlnk->indx = 0;
28 curlnk->val = DOUBLE_MAX;
29 curlnk = curlnk->pntr;
30 }
31
32 /* Note: Uses current top link (which would need to be deleted anyway) each time
33 an insertion to the list is required. */
34
35 for (i = beg; i <= end; i++) {
36 /* consider each element for insertion */
37 top = *scanlist;
38 val = vec[i];
39 if (val < top->val) {
40 if (top->pntr == NULL) {
41 /* the list is only one long, so just replace */
42 top->val = val;
43 top->indx = i;
44 }
45 else {
46 /* beats top element; scan for insertion point */
47 if (val < (top->pntr)->val) {
48 /* 2nd link becomes list pntr; otherwise stays same */
49 *scanlist = top->pntr;
50 }
51 prevlnk = curlnk = top;
52 while ((val < curlnk->val) && (curlnk->pntr != NULL)) {
53 prevlnk = curlnk;
54 curlnk = curlnk->pntr;
55 }
56 if (val < curlnk->val) {
57 /* got to end of list; add top to bottom */
58 curlnk->pntr = top;
59 top->val = val;
60 top->indx = i;
61 top->pntr = NULL;
62 }
63 else {
64 /* stopped within list; insert top here */
65 prevlnk->pntr = top;
66 top->val = val;
67 top->indx = i;
68 top->pntr = curlnk;
69 }
70 }
71 }
72 }
73 }