2 * collectd - src/sn_stage.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
33 #include "sn_comparator.h"
36 sn_stage_t *sn_stage_create (int depth)
40 s = (sn_stage_t *) malloc (sizeof (sn_stage_t));
43 memset (s, '\0', sizeof (sn_stage_t));
48 } /* sn_stage_t *sn_stage_create */
50 void sn_stage_destroy (sn_stage_t *s)
54 if (s->comparators != NULL)
55 free (s->comparators);
57 } /* void sn_stage_destroy */
59 int sn_stage_sort (sn_stage_t *s, int *values)
64 for (i = 0; i < s->comparators_num; i++)
66 c = s->comparators + i;
67 if (values[c->min] > values[c->max])
71 temp = values[c->min];
72 values[c->min] = values[c->max];
73 values[c->max] = temp;
78 } /* int sn_stage_sort */
80 int sn_stage_comparator_add (sn_stage_t *s, const sn_comparator_t *c)
82 sn_comparator_t *temp;
85 temp = (sn_comparator_t *) realloc (s->comparators,
86 (s->comparators_num + 1) * sizeof (sn_comparator_t));
89 s->comparators = temp;
92 for (i = 0; i < s->comparators_num; i++)
93 if (sn_comparator_compare (c, s->comparators + i) <= 0)
96 /* Insert the elements in ascending order */
97 assert (i <= s->comparators_num);
98 if (i < s->comparators_num)
100 int nmemb = s->comparators_num - i;
101 memmove (s->comparators + (i + 1), s->comparators + i,
102 nmemb * sizeof (sn_comparator_t));
104 memcpy (s->comparators + i, c, sizeof (sn_comparator_t));
105 s->comparators_num++;
108 } /* int sn_stage_comparator_add */
110 int sn_stage_comparator_remove (sn_stage_t *s, int c_num)
112 int nmemb = s->comparators_num - (c_num + 1);
113 sn_comparator_t *temp;
115 assert (c_num < s->comparators_num);
119 memmove (s->comparators + c_num, s->comparators + (c_num + 1),
120 nmemb * sizeof (sn_comparator_t));
121 s->comparators_num--;
123 /* Free the unused memory */
124 if (s->comparators_num == 0)
126 free (s->comparators);
127 s->comparators = NULL;
131 temp = (sn_comparator_t *) realloc (s->comparators,
132 s->comparators_num * sizeof (sn_comparator_t));
135 s->comparators = temp;
139 } /* int sn_stage_comparator_remove */
141 sn_stage_t *sn_stage_clone (const sn_stage_t *s)
145 s_copy = sn_stage_create (s->depth);
149 s_copy->comparators = (sn_comparator_t *) malloc (s->comparators_num
150 * sizeof (sn_comparator_t));
151 if (s_copy->comparators == NULL)
157 memcpy (s_copy->comparators, s->comparators,
158 s->comparators_num * sizeof (sn_comparator_t));
159 s_copy->comparators_num = s->comparators_num;
162 } /* sn_stage_t *sn_stage_clone */
164 int sn_stage_comparator_check_conflict (sn_stage_t *s, const sn_comparator_t *c0)
168 for (i = 0; i < s->comparators_num; i++)
170 sn_comparator_t *c1 = s->comparators + i;
171 if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
172 || (SN_COMP_MIN(c0) == SN_COMP_MAX(c1))
173 || (SN_COMP_MAX(c0) == SN_COMP_MIN(c1))
174 || (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
176 if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
177 && (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
185 } /* int sn_stage_comparator_check_conflict */
187 int sn_stage_show (sn_stage_t *s)
189 int lines[s->comparators_num];
190 int right[s->comparators_num];
196 for (i = 0; i < s->comparators_num; i++)
202 for (i = 0; i < s->comparators_num; i++)
205 sn_comparator_t *c = s->comparators + i;
207 for (j = 0; j < lines_used; j++)
208 if (SN_COMP_LEFT (c) > right[j])
212 right[j] = SN_COMP_RIGHT (c);
217 for (i = 0; i < lines_used; i++)
219 printf ("%3i: ", s->depth);
221 for (j = 0; j <= right[i]; j++)
226 for (k = 0; k < s->comparators_num; k++)
228 sn_comparator_t *c = s->comparators + k;
230 /* Check if this comparator is displayed on another line */
234 if (j == SN_COMP_MIN (c))
236 else if (j == SN_COMP_MAX (c))
239 if ((j >= SN_COMP_LEFT (c)) && (j < SN_COMP_RIGHT (c)))
242 if ((on_elem != 0) || (line_after != 0))
253 else if (on_elem == -1)
267 } /* for (columns) */
273 } /* int sn_stage_show */
275 int sn_stage_invert (sn_stage_t *s)
279 for (i = 0; i < s->comparators_num; i++)
280 sn_comparator_invert (s->comparators + i);
283 } /* int sn_stage_invert */
285 int sn_stage_swap (sn_stage_t *s, int con0, int con1)
289 for (i = 0; i < s->comparators_num; i++)
290 sn_comparator_swap (s->comparators + i, con0, con1);
293 } /* int sn_stage_swap */
295 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir)
297 int new_position = input;
300 for (i = 0; i < s->comparators_num; i++)
302 sn_comparator_t *c = s->comparators + i;
304 if ((SN_COMP_MIN (c) != input) && (SN_COMP_MAX (c) != input))
307 if ((dir == DIR_MIN) && (SN_COMP_MAX (c) == input))
309 new_position = SN_COMP_MIN (c);
311 else if ((dir == DIR_MAX) && (SN_COMP_MIN (c) == input))
313 new_position = SN_COMP_MAX (c);
319 if (i < s->comparators_num)
320 sn_stage_comparator_remove (s, i);
323 return (new_position);
324 } /* int sn_stage_cut_at */
326 int sn_stage_remove_input (sn_stage_t *s, int input)
330 for (i = 0; i < s->comparators_num; i++)
332 sn_comparator_t *c = s->comparators + i;
334 if ((SN_COMP_MIN (c) == input) || (SN_COMP_MAX (c) == input))
336 sn_stage_comparator_remove (s, i);
341 if (SN_COMP_MIN (c) > input)
342 SN_COMP_MIN (c) = SN_COMP_MIN (c) - 1;
343 if (SN_COMP_MAX (c) > input)
344 SN_COMP_MAX (c) = SN_COMP_MAX (c) - 1;
350 sn_stage_t *sn_stage_read (FILE *fh)
356 s = sn_stage_create (0);
360 while (fgets (buffer, sizeof (buffer), fh) != NULL)
365 if ((buffer[0] == '\0') || (buffer[0] == '\n') || (buffer[0] == '\r'))
370 c.min = (int) strtol (buffer_ptr, &endptr, 0);
371 if (buffer_ptr == endptr)
376 c.max = (int) strtol (buffer_ptr, &endptr, 0);
377 if (buffer_ptr == endptr)
380 sn_stage_comparator_add (s, &c);
383 if (s->comparators_num == 0)
385 sn_stage_destroy (s);
390 } /* sn_stage_t *sn_stage_read */
392 int sn_stage_write (sn_stage_t *s, FILE *fh)
396 if (s->comparators_num <= 0)
399 for (i = 0; i < s->comparators_num; i++)
400 fprintf (fh, "%i %i\n",
401 SN_COMP_MIN (s->comparators + i),
402 SN_COMP_MAX (s->comparators + i));
406 } /* int sn_stage_write */
408 int sn_stage_serialize (sn_stage_t *s,
409 char **ret_buffer, size_t *ret_buffer_size)
416 if (s->comparators_num <= 0)
419 buffer = *ret_buffer;
420 buffer_size = *ret_buffer_size;
422 #define SNPRINTF_OR_FAIL(...) \
423 status = snprintf (buffer, buffer_size, __VA_ARGS__); \
424 if ((status < 1) || (status >= buffer_size)) \
427 buffer_size -= status;
429 for (i = 0; i < s->comparators_num; i++)
431 SNPRINTF_OR_FAIL ("%i %i\r\n",
432 SN_COMP_MIN (s->comparators + i),
433 SN_COMP_MAX (s->comparators + i));
436 SNPRINTF_OR_FAIL ("\r\n");
438 *ret_buffer = buffer;
439 *ret_buffer_size = buffer_size;
441 } /* int sn_stage_serialize */
443 sn_stage_t *sn_stage_unserialize (char **ret_buffer, size_t *ret_buffer_size)
450 buffer = *ret_buffer;
451 buffer_size = *ret_buffer_size;
453 if (buffer_size == 0)
456 s = sn_stage_create (0);
461 while (buffer_size > 0)
466 size_t substr_length;
468 endptr = strchr (buffer, '\n');
479 substr_length = strlen (buffer);
481 buffer_size -= (substr_length + 1);
483 if ((substr_length > 0) && (substr[substr_length - 1] == '\r'))
485 substr[substr_length - 1] = 0;
489 if (substr_length == 0)
496 c.min = (int) strtol (substr, &endptr, 0);
497 if (substr == endptr)
505 c.max = (int) strtol (substr, &endptr, 0);
506 if (substr == endptr)
512 sn_stage_comparator_add (s, &c);
513 } /* while (buffer_size > 0) */
515 if ((status != 0) || (s->comparators_num == 0))
517 sn_stage_destroy (s);
521 *ret_buffer = buffer;
522 *ret_buffer_size = buffer_size;
524 } /* sn_stage_t *sn_stage_unserialize */
526 /* vim: set shiftwidth=2 softtabstop=2 expandtab : */