Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563680 views
1
/***** This file includes some routines for
2
sorting lists and searching in sorted lists *****/
3
#include"typedef.h"
4
5
/**********************************************************************\
6
| compares the 1xn-vectors x and y lexicographically
7
| returns 1 if x > y, 0 if x = y, -1 if x < y
8
\**********************************************************************/
9
static int comp(x, y, n)
10
int *x, *y, n;
11
{
12
int i;
13
14
for (i = 0; i < n && x[i] == y[i]; ++i);
15
if (i == n)
16
return(0);
17
else if (x[i] > y[i])
18
return(1);
19
else
20
return(-1);
21
}
22
23
/**********************************************************************\
24
| searches for the vector vec in sorted
25
| list v := V.v between v[1] and v[V.n],
26
| where the first non-zero entry in v[i] is > 0,
27
| returns i > 0 if vec = v[i],
28
| returns -i < 0 if -vec = v[i],
29
| if vector is not found it returns
30
| V.n+i if v[i-1] < vec < v[i] or
31
| -(V.n+i) if v[i-1] < -vec < v[i]
32
| if the return value is negative, vec is replaced by -vec
33
\**********************************************************************/
34
static int numberof(vec, V)
35
veclist V;
36
int *vec;
37
{
38
int i, sign, dim, low, high, search, cmp;
39
40
sign = 1;
41
dim = V.dim;
42
low = 1;
43
high = V.n;
44
for (i = 0; i < dim && vec[i] == 0; ++i);
45
if (i < dim && vec[i] < 0)
46
{
47
sign = -1;
48
for (i = 0; i < dim; ++i)
49
vec[i] *= -1;
50
}
51
while (low <= high)
52
{
53
search = low + (high-low)/2;
54
cmp = comp(vec, V.v[search], dim);
55
if (cmp == 1)
56
/* the vector is in the upper half */
57
low = search + 1;
58
else if (cmp == -1)
59
/* the vector is in the lower half */
60
high = search - 1;
61
else
62
/* the vector is found */
63
return(sign * search);
64
}
65
/* if low > high the vector was not found */
66
return(sign * (V.n+low));
67
}
68
69
/**********************************************************************\
70
| sorts the V->n vectors v[1]...v[V->n] and
71
| deletes doublets, V->v is changed !!!
72
\**********************************************************************/
73
static void sortvecs(V)
74
veclist *V;
75
{
76
int i, j;
77
78
/* sort the vectors */
79
quicksort(V->v, 1, V->n, V->dim);
80
/* now delete doublets */
81
j = 1;
82
for (i = 1; i+j <= V->n; ++i)
83
{
84
while (i+j <= V->n && comp(V->v[i], V->v[i+j], V->dim) == 0)
85
{
86
free(V->v[i+j]);
87
++j;
88
}
89
if (i+j <= V->n)
90
{
91
V->v[i+1] = V->v[i+j];
92
if (comp(V->v[i], V->v[i+1], V->dim) == 1)
93
{
94
fprintf(stderr, "Error: v[%d] > v[%d]\n", i, i+1);
95
exit (3);
96
}
97
}
98
}
99
V->n -= j-1;
100
}
101
102
/**********************************************************************\
103
| standard quicksort
104
\**********************************************************************/
105
static void quicksort(v, inf, sup, dim)
106
int **v, inf, sup, dim;
107
{
108
int *tmp, low, med, high;
109
110
if(inf+1 < sup)
111
{
112
/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */
113
med = (inf+1+sup)/2;
114
tmp = v[inf];
115
v[inf] = v[med];
116
v[med] = tmp;
117
low = inf+1;
118
high = sup;
119
while(low < high)
120
{
121
while(comp(v[inf], v[low], dim) >= 0 && low < sup)
122
++low;
123
while(comp(v[inf], v[high], dim) <= 0 && high > inf)
124
--high;
125
if(low < high)
126
{
127
tmp = v[high];
128
v[high] = v[low];
129
v[low] = tmp;
130
}
131
}
132
if(inf != high)
133
{
134
tmp = v[high];
135
v[high] = v[inf];
136
v[inf] = tmp;
137
}
138
quicksort(v, inf, high-1, dim);
139
quicksort(v, high+1, sup, dim);
140
}
141
if(inf+1 == sup)
142
{
143
if(comp(v[inf], v[sup], dim) == 1)
144
{
145
tmp = v[inf];
146
v[inf] = v[sup];
147
v[sup] = tmp;
148
}
149
}
150
}
151
152