src/*.[ch]: Added GPLv2 license information.
[sort-networks.git] / src / sn_stage.c
1 /**
2  * collectd - src/sn_stage.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "sn_comparator.h"
27 #include "sn_stage.h"
28
29 sn_stage_t *sn_stage_create (int depth)
30 {
31   sn_stage_t *s;
32
33   s = (sn_stage_t *) malloc (sizeof (sn_stage_t));
34   if (s == NULL)
35     return (NULL);
36   memset (s, '\0', sizeof (sn_stage_t));
37
38   s->depth = depth;
39   
40   return (s);
41 } /* sn_stage_t *sn_stage_create */
42
43 void sn_stage_destroy (sn_stage_t *s)
44 {
45   if (s == NULL)
46     return;
47   if (s->comparators != NULL)
48     free (s->comparators);
49   free (s);
50 } /* void sn_stage_destroy */
51
52 int sn_stage_comparator_add (sn_stage_t *s, const sn_comparator_t *c)
53 {
54   sn_comparator_t *temp;
55   int i;
56
57   temp = (sn_comparator_t *) realloc (s->comparators,
58       (s->comparators_num + 1) * sizeof (sn_comparator_t));
59   if (temp == NULL)
60     return (-1);
61   s->comparators = temp;
62   temp = NULL;
63
64   for (i = 0; i < s->comparators_num; i++)
65     if (sn_comparator_compare (c, s->comparators + i) <= 0)
66       break;
67
68   /* Insert the elements in ascending order */
69   assert (i <= s->comparators_num);
70   if (i < s->comparators_num)
71   {
72     int nmemb = s->comparators_num - i;
73     memmove (s->comparators + (i + 1), s->comparators + i,
74         nmemb * sizeof (sn_comparator_t));
75   }
76   memcpy (s->comparators + i, c, sizeof (sn_comparator_t));
77   s->comparators_num++;
78
79   return (0);
80 } /* int sn_stage_comparator_add */
81
82 int sn_stage_comparator_remove (sn_stage_t *s, int c_num)
83 {
84   int nmemb = s->comparators_num - (c_num + 1);
85   sn_comparator_t *temp;
86
87   assert (c_num < s->comparators_num);
88   assert (c_num >= 0);
89
90   if (nmemb > 0)
91     memmove (s->comparators + c_num, s->comparators + (c_num + 1),
92         nmemb * sizeof (sn_comparator_t));
93   s->comparators_num--;
94
95   /* Free the unused memory */
96   if (s->comparators_num == 0)
97   {
98     free (s->comparators);
99     s->comparators = NULL;
100   }
101   else
102   {
103     temp = (sn_comparator_t *) realloc (s->comparators,
104         s->comparators_num * sizeof (sn_comparator_t));
105     if (temp == NULL)
106       return (-1);
107     s->comparators = temp;
108   }
109
110   return (0);
111 } /* int sn_stage_comparator_remove */
112
113 sn_stage_t *sn_stage_clone (const sn_stage_t *s)
114 {
115   sn_stage_t *s_copy;
116
117   s_copy = sn_stage_create (s->depth);
118   if (s_copy == NULL)
119     return (NULL);
120
121   s_copy->comparators = (sn_comparator_t *) malloc (s->comparators_num
122       * sizeof (sn_comparator_t));
123   if (s_copy->comparators == NULL)
124   {
125     free (s_copy);
126     return (NULL);
127   }
128
129   memcpy (s_copy->comparators, s->comparators,
130       s->comparators_num * sizeof (sn_comparator_t));
131   s_copy->comparators_num = s->comparators_num;
132
133   return (s_copy);
134 } /* sn_stage_t *sn_stage_clone */
135
136 int sn_stage_comparator_check_conflict (sn_stage_t *s, const sn_comparator_t *c0)
137 {
138   int i;
139
140   for (i = 0; i < s->comparators_num; i++)
141   {
142     sn_comparator_t *c1 = s->comparators + i;
143     if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
144         || (SN_COMP_MIN(c0) == SN_COMP_MAX(c1))
145         || (SN_COMP_MAX(c0) == SN_COMP_MIN(c1))
146         || (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
147     {
148       if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
149           && (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
150         return (2);
151       else
152         return (1);
153     }
154   }
155
156   return (0);
157 } /* int sn_stage_comparator_check_conflict */
158
159 int sn_stage_show (sn_stage_t *s)
160 {
161   int lines[s->comparators_num];
162   int right[s->comparators_num];
163   int lines_used = 0;
164   int i;
165   int j;
166   int k;
167
168
169   for (i = 0; i < s->comparators_num; i++)
170   {
171     lines[i] = -1;
172     right[i] = -1;
173   }
174
175   for (i = 0; i < s->comparators_num; i++)
176   {
177     int j;
178     sn_comparator_t *c = s->comparators + i;
179
180     for (j = 0; j < lines_used; j++)
181       if (SN_COMP_LEFT (c) > right[j])
182         break;
183
184     lines[i] = j;
185     right[j] = SN_COMP_RIGHT (c);
186     if (j >= lines_used)
187       lines_used = j + 1;
188   }
189
190   for (i = 0; i < lines_used; i++)
191   {
192     printf ("%3i: ", s->depth);
193
194     for (j = 0; j <= right[i]; j++)
195     {
196       int on_elem = 0;
197       int line_after = 0;
198
199       for (k = 0; k < s->comparators_num; k++)
200       {
201         sn_comparator_t *c = s->comparators + k;
202
203         /* Check if this comparator is displayed on another line */
204         if (lines[k] != i)
205           continue;
206
207         if (j == SN_COMP_MIN (c))
208           on_elem = -1;
209         else if (j == SN_COMP_MAX (c))
210           on_elem = 1;
211
212         if ((j >= SN_COMP_LEFT (c)) && (j < SN_COMP_RIGHT (c)))
213           line_after = 1;
214
215         if ((on_elem != 0) || (line_after != 0))
216           break;
217       }
218
219       if (on_elem == 0)
220       {
221         if (line_after == 0)
222           printf ("     ");
223         else
224           printf ("-----");
225       }
226       else if (on_elem == -1)
227       {
228         if (line_after == 0)
229           printf ("-!   ");
230         else
231           printf (" !---");
232       }
233       else
234       {
235         if (line_after == 0)
236           printf ("->   ");
237         else
238           printf (" <---");
239       }
240     } /* for (columns) */
241
242     printf ("\n");
243   } /* for (lines) */
244
245   return (0);
246 } /* int sn_stage_show */
247
248 int sn_stage_invert (sn_stage_t *s)
249 {
250   int i;
251
252   for (i = 0; i < s->comparators_num; i++)
253     sn_comparator_invert (s->comparators + i);
254
255   return (0);
256 } /* int sn_stage_invert */
257
258 int sn_stage_swap (sn_stage_t *s, int con0, int con1)
259 {
260   int i;
261
262   for (i = 0; i < s->comparators_num; i++)
263     sn_comparator_swap (s->comparators + i, con0, con1);
264
265   return (0);
266 } /* int sn_stage_swap */
267
268 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir)
269 {
270   int new_position = input;
271   int i;
272
273   for (i = 0; i < s->comparators_num; i++)
274   {
275     sn_comparator_t *c = s->comparators + i;
276
277     if ((SN_COMP_MIN (c) != input) && (SN_COMP_MAX (c) != input))
278       continue;
279
280     if ((dir == DIR_MIN) && (SN_COMP_MAX (c) == input))
281     {
282       new_position = SN_COMP_MIN (c);
283     }
284     else if ((dir == DIR_MAX) && (SN_COMP_MIN (c) == input))
285     {
286       new_position = SN_COMP_MAX (c);
287     }
288     break;
289   }
290
291   /*
292   if (i < s->comparators_num)
293     sn_stage_comparator_remove (s, i);
294     */
295
296   return (new_position);
297 } /* int sn_stage_cut_at */
298
299 int sn_stage_remove_input (sn_stage_t *s, int input)
300 {
301   int i;
302
303   for (i = 0; i < s->comparators_num; i++)
304   {
305     sn_comparator_t *c = s->comparators + i;
306
307     if ((SN_COMP_MIN (c) == input) || (SN_COMP_MAX (c) == input))
308     {
309       sn_stage_comparator_remove (s, i);
310       i--;
311       continue;
312     }
313
314     if (SN_COMP_MIN (c) > input)
315       SN_COMP_MIN (c) = SN_COMP_MIN (c) - 1;
316     if (SN_COMP_MAX (c) > input)
317       SN_COMP_MAX (c) = SN_COMP_MAX (c) - 1;
318   }
319
320   return (0);
321 }
322
323 sn_stage_t *sn_stage_read (FILE *fh)
324 {
325   sn_stage_t *s;
326   char buffer[64];
327   char *buffer_ptr;
328   
329   s = sn_stage_create (0);
330   if (s == NULL)
331     return (NULL);
332
333   while (fgets (buffer, sizeof (buffer), fh) != NULL)
334   {
335     char *endptr;
336     sn_comparator_t c;
337
338     if ((buffer[0] == '\0') || (buffer[0] == '\n') || (buffer[0] == '\r'))
339       break;
340     
341     buffer_ptr = buffer;
342     endptr = NULL;
343     c.min = (int) strtol (buffer_ptr, &endptr, 0);
344     if (buffer_ptr == endptr)
345       continue;
346
347     buffer_ptr = endptr;
348     endptr = NULL;
349     c.max = (int) strtol (buffer_ptr, &endptr, 0);
350     if (buffer_ptr == endptr)
351       continue;
352
353     sn_stage_comparator_add (s, &c);
354   }
355
356   if (s->comparators_num == 0)
357   {
358     sn_stage_destroy (s);
359     return (NULL);
360   }
361
362   return (s);
363 } /* sn_stage_t *sn_stage_read */
364
365 int sn_stage_write (sn_stage_t *s, FILE *fh)
366 {
367   int i;
368
369   if (s->comparators_num <= 0)
370     return (0);
371
372   for (i = 0; i < s->comparators_num; i++)
373     fprintf (fh, "%i %i\n",
374         SN_COMP_MIN (s->comparators + i),
375         SN_COMP_MAX (s->comparators + i));
376   fprintf (fh, "\n");
377
378   return (0);
379 } /* int sn_stage_write */
380
381 /* vim: set shiftwidth=2 softtabstop=2 : */