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
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_shift (sn_stage_t *s, int sw, int inputs_num)
289 for (i = 0; i < s->comparators_num; i++)
290 sn_comparator_shift (s->comparators + i, sw, inputs_num);
293 } /* int sn_stage_shift */
295 int sn_stage_swap (sn_stage_t *s, int con0, int con1)
299 for (i = 0; i < s->comparators_num; i++)
300 sn_comparator_swap (s->comparators + i, con0, con1);
303 } /* int sn_stage_swap */
305 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir)
307 int new_position = input;
310 for (i = 0; i < s->comparators_num; i++)
312 sn_comparator_t *c = s->comparators + i;
314 if ((SN_COMP_MIN (c) != input) && (SN_COMP_MAX (c) != input))
317 if ((dir == DIR_MIN) && (SN_COMP_MAX (c) == input))
319 new_position = SN_COMP_MIN (c);
321 else if ((dir == DIR_MAX) && (SN_COMP_MIN (c) == input))
323 new_position = SN_COMP_MAX (c);
329 if (i < s->comparators_num)
330 sn_stage_comparator_remove (s, i);
333 return (new_position);
334 } /* int sn_stage_cut_at */
336 int sn_stage_remove_input (sn_stage_t *s, int input)
340 for (i = 0; i < s->comparators_num; i++)
342 sn_comparator_t *c = s->comparators + i;
344 if ((SN_COMP_MIN (c) == input) || (SN_COMP_MAX (c) == input))
346 sn_stage_comparator_remove (s, i);
351 if (SN_COMP_MIN (c) > input)
352 SN_COMP_MIN (c) = SN_COMP_MIN (c) - 1;
353 if (SN_COMP_MAX (c) > input)
354 SN_COMP_MAX (c) = SN_COMP_MAX (c) - 1;
360 sn_stage_t *sn_stage_read (FILE *fh)
366 s = sn_stage_create (0);
370 while (fgets (buffer, sizeof (buffer), fh) != NULL)
375 if ((buffer[0] == '\0') || (buffer[0] == '\n') || (buffer[0] == '\r'))
380 c.min = (int) strtol (buffer_ptr, &endptr, 0);
381 if (buffer_ptr == endptr)
386 c.max = (int) strtol (buffer_ptr, &endptr, 0);
387 if (buffer_ptr == endptr)
390 sn_stage_comparator_add (s, &c);
393 if (s->comparators_num == 0)
395 sn_stage_destroy (s);
400 } /* sn_stage_t *sn_stage_read */
402 int sn_stage_write (sn_stage_t *s, FILE *fh)
406 if (s->comparators_num <= 0)
409 for (i = 0; i < s->comparators_num; i++)
410 fprintf (fh, "%i %i\n",
411 SN_COMP_MIN (s->comparators + i),
412 SN_COMP_MAX (s->comparators + i));
416 } /* int sn_stage_write */
418 int sn_stage_serialize (sn_stage_t *s,
419 char **ret_buffer, size_t *ret_buffer_size)
426 if (s->comparators_num <= 0)
429 buffer = *ret_buffer;
430 buffer_size = *ret_buffer_size;
432 #define SNPRINTF_OR_FAIL(...) \
433 status = snprintf (buffer, buffer_size, __VA_ARGS__); \
434 if ((status < 1) || (status >= buffer_size)) \
437 buffer_size -= status;
439 for (i = 0; i < s->comparators_num; i++)
441 SNPRINTF_OR_FAIL ("%i %i\r\n",
442 SN_COMP_MIN (s->comparators + i),
443 SN_COMP_MAX (s->comparators + i));
446 SNPRINTF_OR_FAIL ("\r\n");
448 *ret_buffer = buffer;
449 *ret_buffer_size = buffer_size;
451 } /* int sn_stage_serialize */
453 sn_stage_t *sn_stage_unserialize (char **ret_buffer, size_t *ret_buffer_size)
460 buffer = *ret_buffer;
461 buffer_size = *ret_buffer_size;
463 if (buffer_size == 0)
466 s = sn_stage_create (0);
471 while (buffer_size > 0)
476 size_t substr_length;
478 endptr = strchr (buffer, '\n');
489 substr_length = strlen (buffer);
491 buffer_size -= (substr_length + 1);
493 if ((substr_length > 0) && (substr[substr_length - 1] == '\r'))
495 substr[substr_length - 1] = 0;
499 if (substr_length == 0)
506 c.min = (int) strtol (substr, &endptr, 0);
507 if (substr == endptr)
515 c.max = (int) strtol (substr, &endptr, 0);
516 if (substr == endptr)
522 sn_stage_comparator_add (s, &c);
523 } /* while (buffer_size > 0) */
525 if ((status != 0) || (s->comparators_num == 0))
527 sn_stage_destroy (s);
531 *ret_buffer = buffer;
532 *ret_buffer_size = buffer_size;
534 } /* sn_stage_t *sn_stage_unserialize */
536 /* vim: set shiftwidth=2 softtabstop=2 expandtab : */