5 #include "sn_comparator.h"
8 sn_stage_t *sn_stage_create (int depth)
12 s = (sn_stage_t *) malloc (sizeof (sn_stage_t));
15 memset (s, '\0', sizeof (sn_stage_t));
20 } /* sn_stage_t *sn_stage_create */
22 void sn_stage_destroy (sn_stage_t *s)
26 if (s->comparators != NULL)
27 free (s->comparators);
29 } /* void sn_stage_destroy */
31 int sn_stage_comparator_add (sn_stage_t *s, const sn_comparator_t *c)
33 sn_comparator_t *temp;
36 temp = (sn_comparator_t *) realloc (s->comparators,
37 (s->comparators_num + 1) * sizeof (sn_comparator_t));
40 s->comparators = temp;
43 for (i = 0; i < s->comparators_num; i++)
44 if (sn_comparator_compare (c, s->comparators + i) <= 0)
47 /* Insert the elements in ascending order */
48 assert (i <= s->comparators_num);
49 if (i < s->comparators_num)
51 int nmemb = s->comparators_num - i;
52 memmove (s->comparators + (i + 1), s->comparators + i,
53 nmemb * sizeof (sn_comparator_t));
55 memcpy (s->comparators + i, c, sizeof (sn_comparator_t));
59 } /* int sn_stage_comparator_add */
61 int sn_stage_comparator_remove (sn_stage_t *s, int c_num)
63 int nmemb = s->comparators_num - (c_num + 1);
64 sn_comparator_t *temp;
66 assert (c_num < s->comparators_num);
70 memmove (s->comparators + c_num, s->comparators + (c_num + 1),
71 nmemb * sizeof (sn_comparator_t));
74 /* Free the unused memory */
75 if (s->comparators_num == 0)
77 free (s->comparators);
78 s->comparators = NULL;
82 temp = (sn_comparator_t *) realloc (s->comparators,
83 s->comparators_num * sizeof (sn_comparator_t));
86 s->comparators = temp;
90 } /* int sn_stage_comparator_remove */
92 sn_stage_t *sn_stage_clone (const sn_stage_t *s)
96 s_copy = sn_stage_create (s->depth);
100 s_copy->comparators = (sn_comparator_t *) malloc (s->comparators_num
101 * sizeof (sn_comparator_t));
102 if (s_copy->comparators == NULL)
108 memcpy (s_copy->comparators, s->comparators,
109 s->comparators_num * sizeof (sn_comparator_t));
110 s_copy->comparators_num = s->comparators_num;
113 } /* sn_stage_t *sn_stage_clone */
115 int sn_stage_comparator_check_conflict (sn_stage_t *s, const sn_comparator_t *c0)
119 for (i = 0; i < s->comparators_num; i++)
121 sn_comparator_t *c1 = s->comparators + i;
122 if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
123 || (SN_COMP_MIN(c0) == SN_COMP_MAX(c1))
124 || (SN_COMP_MAX(c0) == SN_COMP_MIN(c1))
125 || (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
127 if ((SN_COMP_MIN(c0) == SN_COMP_MIN(c1))
128 && (SN_COMP_MAX(c0) == SN_COMP_MAX(c1)))
136 } /* int sn_stage_comparator_check_conflict */
138 int sn_stage_show (sn_stage_t *s)
140 int lines[s->comparators_num];
141 int right[s->comparators_num];
148 for (i = 0; i < s->comparators_num; i++)
154 for (i = 0; i < s->comparators_num; i++)
157 sn_comparator_t *c = s->comparators + i;
159 for (j = 0; j < lines_used; j++)
160 if (SN_COMP_LEFT (c) > right[j])
164 right[j] = SN_COMP_RIGHT (c);
169 for (i = 0; i < lines_used; i++)
171 printf ("%3i: ", s->depth);
173 for (j = 0; j <= right[i]; j++)
178 for (k = 0; k < s->comparators_num; k++)
180 sn_comparator_t *c = s->comparators + k;
182 /* Check if this comparator is displayed on another line */
186 if (j == SN_COMP_MIN (c))
188 else if (j == SN_COMP_MAX (c))
191 if ((j >= SN_COMP_LEFT (c)) && (j < SN_COMP_RIGHT (c)))
194 if ((on_elem != 0) || (line_after != 0))
205 else if (on_elem == -1)
219 } /* for (columns) */
225 } /* int sn_stage_show */
227 int sn_stage_invert (sn_stage_t *s)
231 for (i = 0; i < s->comparators_num; i++)
232 sn_comparator_invert (s->comparators + i);
235 } /* int sn_stage_invert */
237 int sn_stage_swap (sn_stage_t *s, int con0, int con1)
241 for (i = 0; i < s->comparators_num; i++)
242 sn_comparator_swap (s->comparators + i, con0, con1);
245 } /* int sn_stage_swap */
247 int sn_stage_cut_at (sn_stage_t *s, int input, enum sn_network_cut_dir_e dir)
249 int new_position = input;
252 for (i = 0; i < s->comparators_num; i++)
254 sn_comparator_t *c = s->comparators + i;
256 if ((SN_COMP_MIN (c) != input) && (SN_COMP_MAX (c) != input))
259 if ((dir == DIR_MIN) && (SN_COMP_MAX (c) == input))
261 new_position = SN_COMP_MIN (c);
263 else if ((dir == DIR_MAX) && (SN_COMP_MIN (c) == input))
265 new_position = SN_COMP_MAX (c);
271 if (i < s->comparators_num)
272 sn_stage_comparator_remove (s, i);
275 return (new_position);
276 } /* int sn_stage_cut_at */
278 int sn_stage_remove_input (sn_stage_t *s, int input)
282 for (i = 0; i < s->comparators_num; i++)
284 sn_comparator_t *c = s->comparators + i;
286 if ((SN_COMP_MIN (c) == input) || (SN_COMP_MAX (c) == input))
288 sn_stage_comparator_remove (s, i);
293 if (SN_COMP_MIN (c) > input)
294 SN_COMP_MIN (c) = SN_COMP_MIN (c) - 1;
295 if (SN_COMP_MAX (c) > input)
296 SN_COMP_MAX (c) = SN_COMP_MAX (c) - 1;
302 sn_stage_t *sn_stage_read (FILE *fh)
308 s = sn_stage_create (0);
312 while (fgets (buffer, sizeof (buffer), fh) != NULL)
317 if ((buffer[0] == '\0') || (buffer[0] == '\n') || (buffer[0] == '\r'))
322 c.min = (int) strtol (buffer_ptr, &endptr, 0);
323 if (buffer_ptr == endptr)
328 c.max = (int) strtol (buffer_ptr, &endptr, 0);
329 if (buffer_ptr == endptr)
332 sn_stage_comparator_add (s, &c);
335 if (s->comparators_num == 0)
337 sn_stage_destroy (s);
342 } /* sn_stage_t *sn_stage_read */
344 int sn_stage_write (sn_stage_t *s, FILE *fh)
348 if (s->comparators_num <= 0)
351 for (i = 0; i < s->comparators_num; i++)
352 fprintf (fh, "%i %i\n",
353 SN_COMP_MIN (s->comparators + i),
354 SN_COMP_MAX (s->comparators + i));
358 } /* int sn_stage_write */
360 /* vim: set shiftwidth=2 softtabstop=2 : */