File: sorder.c

package info (click to toggle)
trn4 4.0-test77-18
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 4,016 kB
  • sloc: ansic: 48,332; sh: 6,795; tcl: 1,696; yacc: 662; perl: 108; makefile: 26
file content (260 lines) | stat: -rw-r--r-- 5,166 bytes parent folder | download | duplicates (11)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* This file Copyright 1993 by Clifford A. Adams */
/* sorder.c
 *
 * scan ordering
 */

#include "EXTERN.h"
#include "common.h"
#ifdef SCAN
#include "util.h"
#include "scan.h"
#include "smisc.h"
#ifdef SCAN_ART
#include "scanart.h"
#include "samisc.h"
#endif
#include "INTERN.h"
#include "sorder.h"

#ifdef UNDEF
int
s_compare(a,b)
long* a;
long* b;		/* pointers to the two entries to be compared */
 {
    switch(s_cur_type) {
#ifdef SCAN_ART
      case S_ART:
	return sa_compare(*a,*b);
#endif
      default:
	return *a - *b;
    }
}
#endif

int
s_compare(a,b)
long a;
long b;		/* the two entry numbers to be compared */
{
    switch(s_cur_type) {
#ifdef SCAN_ART
      case S_ART:
	return sa_compare(a,b);
#endif
      default:
	return a - b;
    }
}

/* sort offset--used so that the 1-offset algorithm is clear even
 * though the array is 0-offset.
 */
#define SOFF(a) ((a)-1)

/* Uses a heapsort algorithm with the heap readjustment inlined. */
void
s_sort_basic()
{
    int i,n;
    int t1;
    int j;

    n = s_ent_sort_max + 1;
    if (n < 1)
	return;		/* nothing to sort */

    for (i = n/2; i >= 1; i--) {
	/* begin heap readjust */
	t1 = s_ent_sort[SOFF(i)];
	j = 2*i;
	while (j <= n) {
	    if (j < n
	     &&	s_compare(s_ent_sort[SOFF(j)],s_ent_sort[SOFF(j+1)]) < 0)
		j++;
	    if (s_compare(t1,s_ent_sort[SOFF(j)]) > 0)
		break;		/* out of while loop */
	    else {
		s_ent_sort[SOFF(j/2)] = s_ent_sort[SOFF(j)];
		j = j*2;
	    }
	} /* while */
	s_ent_sort[SOFF(j/2)] = t1;
	/* end heap readjust */
    } /* for */

    for (i = n-1; i >= 1; i--) {
	t1 = s_ent_sort[SOFF(i+1)];
	s_ent_sort[SOFF(i+1)] = s_ent_sort[SOFF(1)];
	s_ent_sort[SOFF(1)] = t1;
	/* begin heap readjust */
	j = 2;
	while (j <= i) {
	    if (j < i
	     && s_compare(s_ent_sort[SOFF(j)],s_ent_sort[SOFF(j+1)]) < 0)
		j++;
	    if (s_compare(t1,s_ent_sort[SOFF(j)]) > 0)
		break;	/* out of while */
	    else {
		s_ent_sort[SOFF(j/2)] = s_ent_sort[SOFF(j)];
		j = j*2;
	    }
	} /* while */
	s_ent_sort[SOFF(j/2)] = t1;
	/* end heap readjust */
    } /* for */
    /* end of heapsort */
}

void
s_sort()
{
    long i;

#ifdef UNDEF
    qsort((void*)s_ent_sort,(s_ent_sort_max)+1,sizeof(long),s_compare);
#endif
    s_sort_basic();
    s_ent_sorted_max = s_ent_sort_max;  /* whole array is now sorted */
    s_order_changed = FALSE;
    /* rebuild the indexes */
    for (i = 0; i <= s_ent_sort_max; i++)
	s_ent_index[s_ent_sort[i]] = i;
}

void
s_order_clean()
{
    if (s_ent_sort)
	free(s_ent_sort);
    if (s_ent_index)
	free(s_ent_index);

    s_ent_sort = NULL;
    s_contexts[s_cur_context].ent_sort = s_ent_sort;

    s_ent_index = (long*)0;
    s_contexts[s_cur_context].ent_index = s_ent_index;

    s_ent_sort_max = -1;
    s_ent_sorted_max = -1;
    s_ent_index_max = -1;
}

/* adds the entry number to the current context */
void
s_order_add(ent)
long ent;
{
    long size;

    if (ent < s_ent_index_max && s_ent_index[ent] >= 0)
	return;		/* entry is already in the list */

    /* add entry to end of sorted list */
    s_ent_sort_max += 1;
    if (s_ent_sort_max % 100 == 0) {	/* be nice to realloc */
	size = (s_ent_sort_max+100) * sizeof (long);
	s_ent_sort = (long*)saferealloc((char*)s_ent_sort,size);
	/* change the context too */
	s_contexts[s_cur_context].ent_sort = s_ent_sort;
    }
    s_ent_sort[s_ent_sort_max] = ent;

    /* grow index list if needed */
    if (ent > s_ent_index_max) {
	long old,i;
	old = s_ent_index_max;
	if (s_ent_index_max == -1)
	    s_ent_index_max += 1;
	s_ent_index_max = (ent/100+1) * 100;	/* round up */
	size = (s_ent_index_max + 1) * sizeof (long);
	s_ent_index = (long*)saferealloc((char*)s_ent_index,size);
	/* change the context too */
	s_contexts[s_cur_context].ent_index = s_ent_index;
	/* initialize new indexes */
	for (i = old+1; i < s_ent_index_max; i++)
	    s_ent_index[i] = -1;	/* -1 == not a legal entry */
    }
    s_ent_index[ent] = s_ent_sort_max;
    s_order_changed = TRUE;
}

long
s_prev(ent)
long ent;
{
    long tmp;

    if (ent < 0 || ent > s_ent_index_max || s_ent_sorted_max < 0)
	return 0;
    if (s_order_changed)
	s_sort();
    tmp = s_ent_index[ent];
    if (tmp <= 0)
	return 0;
    return s_ent_sort[tmp-1];
}

long
s_next(ent)
long ent;
{
    long tmp;

    if (ent < 0 || ent > s_ent_index_max || s_ent_sorted_max < 0)
	return 0;
    if (s_order_changed)
	s_sort();
    tmp = s_ent_index[ent];
    if (tmp < 0 || tmp == s_ent_sorted_max)
	return 0;
    return s_ent_sort[tmp+1];
}

/* given an entry, returns previous eligible entry */
/* returns 0 if no previous eligible entry */
long
s_prev_elig(a)
long a;
{
    while ((a = s_prev(a)) != 0)
	if (s_eligible(a))
	    return a;
    return 0L;
}

/* given an entry, returns next eligible entry */
/* returns 0 if no next eligible entry */
long
s_next_elig(a)
long a;
{
    while ((a = s_next(a)) != 0)
	if (s_eligible(a))
	    return a;
    return 0L;
}

long
s_first()
{
    if (s_order_changed)
	s_sort();
    if (s_ent_sorted_max < 0)
	return 0;
    return s_ent_sort[0];
}

long
s_last()
{
    if (s_order_changed)
	s_sort();
    if (s_ent_sorted_max < 0)
	return 0;
    return s_ent_sort[s_ent_sorted_max];
}
#endif /* SCAN */