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