GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/***** This file includes some routines for1sorting lists and searching in sorted lists *****/2#include"typedef.h"34/**********************************************************************\5| compares the 1xn-vectors x and y lexicographically6| returns 1 if x > y, 0 if x = y, -1 if x < y7\**********************************************************************/8static int comp(x, y, n)9int *x, *y, n;10{11int i;1213for (i = 0; i < n && x[i] == y[i]; ++i);14if (i == n)15return(0);16else if (x[i] > y[i])17return(1);18else19return(-1);20}2122/**********************************************************************\23| searches for the vector vec in sorted24| list v := V.v between v[1] and v[V.n],25| where the first non-zero entry in v[i] is > 0,26| returns i > 0 if vec = v[i],27| returns -i < 0 if -vec = v[i],28| if vector is not found it returns29| V.n+i if v[i-1] < vec < v[i] or30| -(V.n+i) if v[i-1] < -vec < v[i]31| if the return value is negative, vec is replaced by -vec32\**********************************************************************/33static int numberof(vec, V)34veclist V;35int *vec;36{37int i, sign, dim, low, high, search, cmp;3839sign = 1;40dim = V.dim;41low = 1;42high = V.n;43for (i = 0; i < dim && vec[i] == 0; ++i);44if (i < dim && vec[i] < 0)45{46sign = -1;47for (i = 0; i < dim; ++i)48vec[i] *= -1;49}50while (low <= high)51{52search = low + (high-low)/2;53cmp = comp(vec, V.v[search], dim);54if (cmp == 1)55/* the vector is in the upper half */56low = search + 1;57else if (cmp == -1)58/* the vector is in the lower half */59high = search - 1;60else61/* the vector is found */62return(sign * search);63}64/* if low > high the vector was not found */65return(sign * (V.n+low));66}6768/**********************************************************************\69| sorts the V->n vectors v[1]...v[V->n] and70| deletes doublets, V->v is changed !!!71\**********************************************************************/72static void sortvecs(V)73veclist *V;74{75int i, j;7677/* sort the vectors */78quicksort(V->v, 1, V->n, V->dim);79/* now delete doublets */80j = 1;81for (i = 1; i+j <= V->n; ++i)82{83while (i+j <= V->n && comp(V->v[i], V->v[i+j], V->dim) == 0)84{85free(V->v[i+j]);86++j;87}88if (i+j <= V->n)89{90V->v[i+1] = V->v[i+j];91if (comp(V->v[i], V->v[i+1], V->dim) == 1)92{93fprintf(stderr, "Error: v[%d] > v[%d]\n", i, i+1);94exit (3);95}96}97}98V->n -= j-1;99}100101/**********************************************************************\102| standard quicksort103\**********************************************************************/104static void quicksort(v, inf, sup, dim)105int **v, inf, sup, dim;106{107int *tmp, low, med, high;108109if(inf+1 < sup)110{111/* interchange v[inf] and v[med] to avoid worst case for pre-sorted lists */112med = (inf+1+sup)/2;113tmp = v[inf];114v[inf] = v[med];115v[med] = tmp;116low = inf+1;117high = sup;118while(low < high)119{120while(comp(v[inf], v[low], dim) >= 0 && low < sup)121++low;122while(comp(v[inf], v[high], dim) <= 0 && high > inf)123--high;124if(low < high)125{126tmp = v[high];127v[high] = v[low];128v[low] = tmp;129}130}131if(inf != high)132{133tmp = v[high];134v[high] = v[inf];135v[inf] = tmp;136}137quicksort(v, inf, high-1, dim);138quicksort(v, high+1, sup, dim);139}140if(inf+1 == sup)141{142if(comp(v[inf], v[sup], dim) == 1)143{144tmp = v[inf];145v[inf] = v[sup];146v[sup] = tmp;147}148}149}150151152