2 * libsortnetwork - src/sn_stage.c
3 * Copyright (C) 2008-2010 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 <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200112L
34 #include "sn_comparator.h"
37 sn_stage_t *sn_stage_create (int depth)
41 s = (sn_stage_t *) malloc (sizeof (sn_stage_t));
44 memset (s, '\0', sizeof (sn_stage_t));
49 } /* sn_stage_t *sn_stage_create */
51 void sn_stage_destroy (sn_stage_t *s)
55 if (s->comparators != NULL)
56 free (s->comparators);
58 } /* void sn_stage_destroy */
60 int sn_stage_sort (sn_stage_t *s, int *values)
65 for (i = 0; i < s->comparators_num; i++)
67 c = s->comparators + i;
68 if (values[c->min] > values[c->max])
72 temp = values[c->min];
73 values[c->min] = values[c->max];
74 values[c->max] = temp;
79 } /* int sn_stage_sort */
81 int sn_stage_comparator_add (sn_stage_t *s, const sn_comparator_t *c)
83 sn_comparator_t *temp;
86 if ((s == NULL) || (c == NULL))
89 i = sn_stage_comparator_check_conflict (s, c);
93 temp = (sn_comparator_t *) realloc (s->comparators,
94 (s->comparators_num + 1) * sizeof (sn_comparator_t));
97 s->comparators = temp;
100 for (i = 0; i < s->comparators_num; i++)
101 if (sn_comparator_compare (c, s->comparators + i) <= 0)
104 /* Insert the elements in ascending order */
105 assert (i <= s->comparators_num);
106 if (i < s->comparators_num)
108 int nmemb = s->comparators_num - i;
109 memmove (s->comparators + (i + 1), s->comparators + i,
110 nmemb * sizeof (sn_comparator_t));
112 memcpy (s->comparators + i, c, sizeof (sn_comparator_t));
113 s->comparators_num++;
116 } /* int sn_stage_comparator_add */
118 int sn_stage_comparator_remove (sn_stage_t *s, int c_num)
120 int nmemb = s->comparators_num - (c_num + 1);
121 sn_comparator_t *temp;
123 if ((s == NULL) || (s->comparators_num <= c_num))
126 assert (c_num < s->comparators_num);
130 memmove (s->comparators + c_num, s->comparators + (c_num + 1),
131 nmemb * sizeof (sn_comparator_t));
132 s->comparators_num--;
134 /* Free the unused memory */
135 if (s->comparators_num == 0)
137 free (s->comparators);
138 s->comparators = NULL;
142 temp = (sn_comparator_t *) realloc (s->comparators,
143 s->comparators_num * sizeof (sn_comparator_t));
146 s->comparators = temp;
150 } /* int sn_stage_comparator_remove */
152 sn_stage_t *sn_stage_clone (const sn_stage_t *s)
157 s_copy = sn_stage_create (s->depth);
161 s_copy->comparators = (sn_comparator_t *) malloc (s->comparators_num
162 * sizeof (sn_comparator_t));
163 if (s_copy->comparators == NULL)
169 memcpy (s_copy->comparators, s->comparators,
170 s->comparators_num * sizeof (sn_comparator_t));
171 s_copy->comparators_num = s->comparators_num;
174 } /* sn_stage_t *sn_stage_clone */
176 int sn_stage_comparator_check_conflict (sn_stage_t *s, const sn_comparator_t *c0)
180 for (i = 0; i < s->comparators_num; i++)
182 sn_comparator_t *c1 = s->comparators + i;
183 if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
184 || (SN_COMP_MIN(c0) == SN_COMP_MAX(c1))
185 || (SN_COMP_MAX(c0) == SN_COMP_MIN(c1))
186 || (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
188 if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
189 && (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
197 } /* int sn_stage_comparator_check_conflict */
199 int sn_stage_show (sn_stage_t *s)
201 int lines[s->comparators_num];
202 int right[s->comparators_num];
208 for (i = 0; i < s->comparators_num; i++)
214 for (i = 0; i < s->comparators_num; i++)
217 sn_comparator_t *c = s->comparators + i;
219 for (j = 0; j < lines_used; j++)
220 if (SN_COMP_LEFT (c) > right[j])
224 right[j] = SN_COMP_RIGHT (c);
229 for (i = 0; i < lines_used; i++)
231 printf ("%3i: ", s->depth);
233 for (j = 0; j <= right[i]; j++)
238 for (k = 0; k < s->comparators_num; k++)
240 sn_comparator_t *c = s->comparators + k;
242 /* Check if this comparator is displayed on another line */
246 if (j == SN_COMP_MIN (c))
248 else if (j == SN_COMP_MAX (c))
251 if ((j >= SN_COMP_LEFT (c)) && (j < SN_COMP_RIGHT (c)))
254 if ((on_elem != 0) || (line_after != 0))
265 else if (on_elem == -1)
279 } /* for (columns) */
285 } /* int sn_stage_show */
287 int sn_stage_invert (sn_stage_t *s)
294 for (i = 0; i < s->comparators_num; i++)
295 sn_comparator_invert (s->comparators + i);
298 } /* int sn_stage_invert */
300 int sn_stage_shift (sn_stage_t *s, int sw, int inputs_num)
304 if ((s == NULL) || (inputs_num < 2))
311 for (i = 0; i < s->comparators_num; i++)
312 sn_comparator_shift (s->comparators + i, sw, inputs_num);
315 } /* int sn_stage_shift */
317 int sn_stage_swap (sn_stage_t *s, int con0, int con1)
324 for (i = 0; i < s->comparators_num; i++)
325 sn_comparator_swap (s->comparators + i, con0, con1);
328 } /* int sn_stage_swap */
330 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir)
332 int new_position = input;
335 if ((s == NULL) || (input < 0))
338 for (i = 0; i < s->comparators_num; i++)
340 sn_comparator_t *c = s->comparators + i;
342 if ((SN_COMP_MIN (c) != input) && (SN_COMP_MAX (c) != input))
345 if ((dir == DIR_MIN) && (SN_COMP_MAX (c) == input))
347 new_position = SN_COMP_MIN (c);
349 else if ((dir == DIR_MAX) && (SN_COMP_MIN (c) == input))
351 new_position = SN_COMP_MAX (c);
357 if (i < s->comparators_num)
358 sn_stage_comparator_remove (s, i);
361 return (new_position);
362 } /* int sn_stage_cut_at */
364 int sn_stage_remove_input (sn_stage_t *s, int input)
368 for (i = 0; i < s->comparators_num; i++)
370 sn_comparator_t *c = s->comparators + i;
372 if ((SN_COMP_MIN (c) == input) || (SN_COMP_MAX (c) == input))
374 sn_stage_comparator_remove (s, i);
379 if (SN_COMP_MIN (c) > input)
380 SN_COMP_MIN (c) = SN_COMP_MIN (c) - 1;
381 if (SN_COMP_MAX (c) > input)
382 SN_COMP_MAX (c) = SN_COMP_MAX (c) - 1;
388 sn_stage_t *sn_stage_read (FILE *fh)
394 s = sn_stage_create (0);
398 while (fgets (buffer, sizeof (buffer), fh) != NULL)
403 if ((buffer[0] == '\0') || (buffer[0] == '\n') || (buffer[0] == '\r'))
408 c.min = (int) strtol (buffer_ptr, &endptr, 0);
409 if (buffer_ptr == endptr)
414 c.max = (int) strtol (buffer_ptr, &endptr, 0);
415 if (buffer_ptr == endptr)
418 sn_stage_comparator_add (s, &c);
421 if (s->comparators_num == 0)
423 sn_stage_destroy (s);
428 } /* sn_stage_t *sn_stage_read */
430 int sn_stage_write (sn_stage_t *s, FILE *fh)
434 if (s->comparators_num <= 0)
437 for (i = 0; i < s->comparators_num; i++)
438 fprintf (fh, "%i %i\n",
439 SN_COMP_MIN (s->comparators + i),
440 SN_COMP_MAX (s->comparators + i));
444 } /* int sn_stage_write */
446 int sn_stage_serialize (sn_stage_t *s,
447 char **ret_buffer, size_t *ret_buffer_size)
454 if (s->comparators_num <= 0)
457 buffer = *ret_buffer;
458 buffer_size = *ret_buffer_size;
460 #define SNPRINTF_OR_FAIL(...) \
461 status = snprintf (buffer, buffer_size, __VA_ARGS__); \
462 if ((status < 1) || (((size_t) status) >= buffer_size)) \
465 buffer_size -= status;
467 for (i = 0; i < s->comparators_num; i++)
469 SNPRINTF_OR_FAIL ("%i %i\r\n",
470 SN_COMP_MIN (s->comparators + i),
471 SN_COMP_MAX (s->comparators + i));
474 SNPRINTF_OR_FAIL ("\r\n");
476 *ret_buffer = buffer;
477 *ret_buffer_size = buffer_size;
479 } /* int sn_stage_serialize */
481 sn_stage_t *sn_stage_unserialize (char **ret_buffer, size_t *ret_buffer_size)
488 buffer = *ret_buffer;
489 buffer_size = *ret_buffer_size;
491 if (buffer_size == 0)
494 s = sn_stage_create (0);
499 while (buffer_size > 0)
504 size_t substr_length;
506 endptr = strchr (buffer, '\n');
517 substr_length = strlen (buffer);
519 buffer_size -= (substr_length + 1);
521 if ((substr_length > 0) && (substr[substr_length - 1] == '\r'))
523 substr[substr_length - 1] = 0;
527 if (substr_length == 0)
534 c.min = (int) strtol (substr, &endptr, 0);
535 if (substr == endptr)
543 c.max = (int) strtol (substr, &endptr, 0);
544 if (substr == endptr)
550 sn_stage_comparator_add (s, &c);
551 } /* while (buffer_size > 0) */
553 if ((status != 0) || (s->comparators_num == 0))
555 sn_stage_destroy (s);
559 *ret_buffer = buffer;
560 *ret_buffer_size = buffer_size;
562 } /* sn_stage_t *sn_stage_unserialize */
564 /* vim: set shiftwidth=2 softtabstop=2 expandtab : */