DSDP
sdpxlist.c
1#include "numchol.h"
2
3static int ChkXlist(xlist *xt,
4 int ind)
5{
6 return (xt->port[ind]==xt->idep);
7} /* ChkXlist */
8
9static void XtClear(xlist *xt)
10{
11 int i,sze;
12
13 sze =xt->last;
14 xt->idep=xt->most+1;
15 xt->lowp=xt->idep;
16 xt->cure=sze;
17 xt->ntot=0;
18
19 for (i=0; i<xt->idep; i++)
20 xt->head[i]=xt->last;
21
22 for (i=0; i<sze; i++) {
23 xt->port[i]=xt->idep;
24 xt->fwrd[i]=xt->last;
25 xt->bwrd[i]=xt->last;
26 }
27} /* XtClear */
28
29int XtAlloc(int last,
30 int most,
31 char *info,
32 xlist**rr)
33{
34 xlist *r;
35 int ierr=0;
36
37 r=(xlist*) calloc(1,sizeof(xlist));
38 if (!r) ExitProc(OutOfSpc,info);
39
40
41 r->loca=TRUE;
42 r->last=last;
43 r->most=most;
44 r->ntot=0;
45
46 ierr=iAlloc(most+1,info,&r->head); if(ierr) return 1;
47 ierr=iAlloc(last,info,&r->port); if(ierr) return 1;
48 ierr=iAlloc(last,info,&r->fwrd); if(ierr) return 1;
49 ierr=iAlloc(last,info,&r->bwrd); if(ierr) return 1;
50
51 XtClear(r);
52
53 *rr=r;
54 return (0);
55} /* XtAlloc */
56
57void XtFree(xlist **xt)
58{
59 xlist *r=*xt;
60
61 if (r) {
62 if (r->loca) {
63 iFree(&r->head);
64 iFree(&r->port);
65 iFree(&r->fwrd);
66 iFree(&r->bwrd);
67 }
68 free(r);
69 *xt=NULL;
70 }
71} /* XtFree */
72
73int XtSucc(xlist *xt)
74{
75 int t,last=xt->last,most=xt->most,
76 *head;
77
78 if (xt->cure==last)
79 return (FALSE);
80
81 if (xt->fwrd[xt->cure]!=last)
82 xt->cure=xt->fwrd[xt->cure];
83
84 else {
85 head=xt->head;
86
87 for(t=xt->port[xt->cure]+1; t<=most && head[t]==last; ++t);
88
89 if (t>most)
90 xt->cure=last;
91 else
92 xt->cure=xt->head[t];
93 }
94
95 return (TRUE);
96} /* XtSucc */
97
98void XtDel(xlist *xt,
99 int e)
100{
101 int p;
102
103 if (!ChkXlist(xt,e)) {
104
105 if (xt->ntot<=0)
106 ExitProc(SysError,NULL);
107
108 xt->ntot--;
109
110 if (xt->cure==e) {
111 if (xt->ntot)
112 XtSucc(xt);
113 else
114 xt->cure=xt->last;
115 }
116
117 p =xt->port[e];
118 xt->port[e]=xt->idep;
119
120 if (xt->bwrd[e]!=xt->last)
121 xt->fwrd[xt->bwrd[e]]=xt->fwrd[e];
122 else
123 xt->head[p]=xt->fwrd[e];
124
125 if (xt->fwrd[e]!=xt->last)
126 xt->bwrd[xt->fwrd[e]]=xt->bwrd[e];
127
128 if (xt->head[p]==xt->last &&
129 xt->lowp==p) {
130 xt->lowp=xt->idep;
131 if (xt->ntot) {
132 for(++p; p<=xt->most; ++p){
133 if (xt->head[p]!=xt->last){
134 xt->lowp=p;
135 break;
136 }
137 }
138 }
139 }
140 }
141} /* XtDel */
142
143void XtPut(xlist *xt,
144 int e,
145 int p)
146{
147 if (0<=e && e<xt->last && 0<=p && p<=xt->most) {
148 XtDel(xt,e);
149 xt->ntot++;
150 xt->port[e] =p;
151 xt->fwrd[e] =xt->head[p];
152 xt->bwrd[e]=xt->last;
153
154 if (xt->head[p]!=xt->last)
155 xt->bwrd[xt->head[p]]=e;
156
157 xt->head[p]=e;
158 xt->lowp =min(p,xt->lowp);
159 }
160
161 else
162 ExitProc(SysError,NULL);
163} /* XtPut */
164
165int XtLeast(xlist *xt)
166{
167 if (xt->lowp==xt->idep) {
168 if (xt->ntot!=0)
169 ExitProc(SysError,NULL);
170
171 xt->cure=xt->last;
172 return FALSE;
173 }
174
175 else {
176 if (xt->ntot<=0)
177 ExitProc(SysError,NULL);
178
179 xt->cure=xt->head[xt->lowp];
180 return TRUE;
181 }
182} /* XtLeast */
183
184int XtGet(xlist *xt,
185 int *e,
186 int *p)
187{
188 if (xt->cure>xt->last)
189 ExitProc(SysError,NULL);
190
191 if (xt->cure==xt->last)
192 return FALSE;
193
194 *e=xt->cure;
195 *p=xt->port[*e];
196
197 return TRUE;
198} /* XtGet */