1 
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "declarations.h"
5 
6 struct entry {
7   int indexi;
8   int indexj;
9   int indexk;
10   int indexl;
11   double entry;
12 };
13 
mycompare(const void * p1,const void * p2)14 int mycompare(const void *p1, const void *p2)
15 {
16   if (((struct entry *)p1)->indexi < ((struct entry *)p2)->indexi)
17     {
18       return(-1);
19     };
20   if (((struct entry *)p1)->indexi == ((struct entry *)p2)->indexi)
21     {
22       if (((struct entry *)p1)->indexj < ((struct entry *)p2)->indexj)
23 	{
24 	  return(-1);
25 	};
26       if (((struct entry *)p1)->indexj == ((struct entry *)p2)->indexj)
27 	{
28 	  return(0);
29 	};
30       if (((struct entry *)p1)->indexj > ((struct entry *)p2)->indexj)
31 	{
32 	  return(1);
33 	};
34     };
35 
36   /*
37    * If we get here, then p1->indexi > p2->indexi
38    */
39 
40   return(1);
41 
42 }
43 
sort_entries(k,C,constraints)44 void sort_entries(k,C,constraints)
45      int k;
46      struct blockmatrix C;
47      struct constraintmatrix *constraints;
48 {
49   int i,j;
50   struct sparseblock *ptr;
51   int maxentries;
52   struct entry *entries;
53 
54   /*
55    * First, find out who has the most entries.
56    */
57 
58   maxentries=0;
59   for (i=1; i<=k; i++)
60     {
61       ptr=constraints[i].blocks;
62       while (ptr != NULL)
63 	{
64 	  if (ptr->numentries > maxentries)
65 	    maxentries=ptr->numentries;
66 	  ptr=ptr->next;
67 	};
68     };
69 
70   /*
71    * Allocate space for entries.
72    */
73 
74   entries=(struct entry *)malloc(maxentries*sizeof(struct entry));
75   if (entries==NULL)
76     {
77       printf("Storage allocation failed in sortentries.\n");
78       exit(205);
79     };
80 
81   for (i=1; i<=k; i++)
82     {
83       ptr=constraints[i].blocks;
84 
85       /*
86        * There must be at least one block in each constraint.
87        */
88 
89       if (ptr==NULL)
90         {
91           printf("Constraint %d is empty.\n",i);
92           exit(206);
93         };
94 
95       /*
96        * Now, loop through the blocks.
97        */
98 
99       while (ptr != NULL)
100 	{
101 	  /*
102 	   * Copy in
103 	   */
104 
105 	  for (j=1; j<=ptr->numentries; j++)
106 	    {
107 	      entries[j-1].indexi=ptr->iindices[j];
108 	      entries[j-1].indexj=ptr->jindices[j];
109 	      entries[j-1].entry=ptr->entries[j];
110 	    };
111 
112 	  /*
113 	   * Sort
114 	   */
115 
116 	  qsort(entries,(size_t)ptr->numentries,sizeof(struct entry),
117 		mycompare);
118 
119 	  /*
120 	   * Copy out.
121 	   */
122 
123 	  for (j=1; j<=ptr->numentries; j++)
124 	    {
125 	      ptr->iindices[j]=entries[j-1].indexi;
126 	      ptr->jindices[j]=entries[j-1].indexj;
127 	      ptr->entries[j]=entries[j-1].entry;
128 	    };
129 
130 
131 	  ptr=ptr->next;
132 	};
133     }; /* end i */
134 
135   free(entries);
136 
137 
138 }
139