src/sn_network.[ch]: Implement sn_network_cut().
[sort-networks.git] / src / sn_network.c
1 /**
2  * libsortnetwork - src/sn_network.c
3  * Copyright (C) 2008-2010  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 <ff at octo.it>
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 #if 0
30 # define DPRINTF(...) fprintf (stderr, "sn_network: " __VA_ARGS__)
31 #else
32 # define DPRINTF(...) /**/
33 #endif
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <ctype.h>
40 #include <assert.h>
41 #include <errno.h>
42
43 #include "sn_network.h"
44 #include "sn_random.h"
45
46 sn_network_t *sn_network_create (int inputs_num) /* {{{ */
47 {
48   sn_network_t *n;
49
50   n = (sn_network_t *) malloc (sizeof (sn_network_t));
51   if (n == NULL)
52     return (NULL);
53   memset (n, '\0', sizeof (sn_network_t));
54
55   n->inputs_num = inputs_num;
56
57   return (n);
58 } /* }}} sn_network_t *sn_network_create */
59
60 void sn_network_destroy (sn_network_t *n) /* {{{ */
61 {
62   if (n == NULL)
63     return;
64
65   if (n->stages != NULL)
66   {
67     int i;
68     for (i = 0; i < n->stages_num; i++)
69     {
70       sn_stage_destroy (n->stages[i]);
71       n->stages[i] = NULL;
72     }
73     free (n->stages);
74     n->stages = NULL;
75   }
76
77   free (n);
78 } /* }}} void sn_network_destroy */
79
80 sn_network_t *sn_network_create_odd_even_mergesort (int inputs_num) /* {{{ */
81 {
82   sn_network_t *n;
83
84   n = sn_network_create (inputs_num);
85
86   assert (inputs_num > 0);
87   if (inputs_num == 1)
88   {
89     return (n);
90   }
91   if (inputs_num == 2)
92   {
93     sn_stage_t *s;
94     sn_comparator_t c;
95
96     c.min = 0;
97     c.max = 1;
98
99     s = sn_stage_create (/* depth = */ 0);
100     sn_stage_comparator_add (s, &c);
101     sn_network_stage_add (n, s);
102
103     return (n);
104   }
105   else
106   {
107     sn_network_t *n_left;
108     sn_network_t *n_right;
109     int inputs_left;
110     int inputs_right;
111
112     inputs_left = inputs_num / 2;
113     inputs_right = inputs_num - inputs_left;
114
115     n_left = sn_network_create_odd_even_mergesort (inputs_left);
116     if (n_left == NULL)
117       return (NULL);
118
119     n_right = sn_network_create_odd_even_mergesort (inputs_right);
120     if (n_right == NULL)
121     {
122       sn_network_destroy (n_left);
123       return (NULL);
124     }
125
126     n = sn_network_combine_odd_even_merge (n_left, n_right);
127
128     sn_network_destroy (n_left);
129     sn_network_destroy (n_right);
130
131     if (n != NULL)
132       sn_network_compress (n);
133
134     return (n);
135   }
136 } /* }}} sn_network_t *sn_network_create_odd_even_mergesort */
137
138 static int sn_network_create_pairwise_internal (sn_network_t *n, /* {{{ */
139     int *inputs, int inputs_num)
140 {
141   int i;
142   int inputs_copy[inputs_num];
143   int m;
144
145   for (i = 1; i < inputs_num; i += 2)
146   {
147     sn_comparator_t *c = sn_comparator_create (inputs[i-1], inputs[i]);
148     sn_network_comparator_add (n, c);
149     sn_comparator_destroy (c);
150   }
151
152   if (inputs_num <= 2)
153     return (0);
154
155   /* Sort "pairs" recursively. Like with odd-even mergesort, odd and even lines
156    * are handled recursively and later reunited. */
157   for (i = 0; i < inputs_num; i += 2)
158     inputs_copy[(int) (i / 2)] = inputs[i];
159   /* Recursive call #1 with first set of lines */
160   sn_network_create_pairwise_internal (n, inputs_copy,
161       (int) ((inputs_num + 1) / 2));
162
163   for (i = 1; i < inputs_num; i += 2)
164     inputs_copy[(int) (i / 2)] = inputs[i];
165   /* Recursive call #2 with second set of lines */
166   sn_network_create_pairwise_internal (n, inputs_copy,
167       (int) (inputs_num/ 2));
168
169   /* m is the "amplitude" of the sorted pairs. This is a bit tricky to read due
170    * to different indices being used in the paper, unfortunately. */
171   m = inputs_num / 2;
172   while (m > 1)
173   {
174     for (i = 1; (i + (m - 1)) < inputs_num; i += 2)
175     {
176       int left = i;
177       int right = i + (m - 1);
178       sn_comparator_t *c;
179
180       assert (left < right);
181       c = sn_comparator_create (inputs[left], inputs[right]);
182       sn_network_comparator_add (n, c);
183       sn_comparator_destroy (c);
184     }
185
186     m = m / 2;
187   } /* while (m > 1) */
188
189   return (0);
190 } /* }}} int sn_network_create_pairwise_internal */
191
192 sn_network_t *sn_network_create_pairwise (int inputs_num) /* {{{ */
193 {
194   sn_network_t *n = sn_network_create (inputs_num);
195   int inputs[inputs_num];
196   int i;
197
198   if (n == NULL)
199     return (NULL);
200
201   for (i = 0; i < inputs_num; i++)
202     inputs[i] = i;
203   
204   sn_network_create_pairwise_internal (n, inputs, inputs_num);
205   sn_network_compress (n);
206
207   return (n);
208 } /* }}} sn_network_t *sn_network_create_pairwise */
209
210 int sn_network_stage_add (sn_network_t *n, sn_stage_t *s) /* {{{ */
211 {
212   sn_stage_t **temp;
213
214   if ((n == NULL) || (s == NULL))
215     return (EINVAL);
216
217   temp = (sn_stage_t **) realloc (n->stages, (n->stages_num + 1)
218       * sizeof (sn_stage_t *));
219   if (temp == NULL)
220     return (-1);
221
222   n->stages = temp;
223   SN_STAGE_DEPTH (s) = n->stages_num;
224   n->stages[n->stages_num] = s;
225   n->stages_num++;
226
227   return (0);
228 } /* }}} int sn_network_stage_add */
229
230 int sn_network_stage_remove (sn_network_t *n, int s_num) /* {{{ */
231 {
232   int nmemb = n->stages_num - (s_num + 1);
233   sn_stage_t **temp;
234
235   if ((n == NULL) || (s_num >= n->stages_num))
236     return (EINVAL);
237
238   sn_stage_destroy (n->stages[s_num]);
239   n->stages[s_num] = NULL;
240
241   if (nmemb > 0)
242   {
243     memmove (n->stages + s_num, n->stages + (s_num + 1),
244         nmemb * sizeof (sn_stage_t *));
245     n->stages[n->stages_num - 1] = NULL;
246   }
247   n->stages_num--;
248
249   /* Free the unused memory */
250   if (n->stages_num == 0)
251   {
252     free (n->stages);
253     n->stages = NULL;
254   }
255   else
256   {
257     temp = (sn_stage_t **) realloc (n->stages,
258         n->stages_num * sizeof (sn_stage_t *));
259     if (temp == NULL)
260       return (-1);
261     n->stages = temp;
262   }
263
264   return (0);
265 } /* }}} int sn_network_stage_remove */
266
267 sn_network_t *sn_network_clone (const sn_network_t *n) /* {{{ */
268 {
269   sn_network_t *n_copy;
270   int i;
271
272   n_copy = sn_network_create (n->inputs_num);
273   if (n_copy == NULL)
274     return (NULL);
275
276   for (i = 0; i < n->stages_num; i++)
277   {
278     sn_stage_t *s;
279     int status;
280
281     s = sn_stage_clone (n->stages[i]);
282     if (s == NULL)
283       break;
284
285     status = sn_network_stage_add (n_copy, s);
286     if (status != 0)
287       break;
288   }
289
290   if (i < n->stages_num)
291   {
292     sn_network_destroy (n_copy);
293     return (NULL);
294   }
295
296   return (n_copy);
297 } /* }}} sn_network_t *sn_network_clone */
298
299 int sn_network_comparator_add (sn_network_t *n, /* {{{ */
300     const sn_comparator_t *c)
301 {
302   sn_stage_t *s;
303
304   if ((n == NULL) || (c == NULL))
305     return (EINVAL);
306
307   if (n->stages_num > 0)
308   {
309     s = n->stages[n->stages_num - 1];
310     
311     if (sn_stage_comparator_check_conflict (s, c) == 0)
312     {
313       sn_stage_comparator_add (s, c);
314       return (0);
315     }
316   }
317
318   s = sn_stage_create (n->stages_num);
319   sn_stage_comparator_add (s, c);
320   sn_network_stage_add (n, s);
321
322   return (0);
323 } /* }}} int sn_network_comparator_add */
324
325 int sn_network_get_comparator_num (const sn_network_t *n) /* {{{ */
326 {
327   int num;
328   int i;
329
330   if (n == NULL)
331     return (-1);
332
333   num = 0;
334   for (i = 0; i < n->stages_num; i++)
335     num += n->stages[i]->comparators_num;
336
337   return (num);
338 } /* }}} int sn_network_get_comparator_num */
339
340 int sn_network_show (sn_network_t *n) /* {{{ */
341 {
342   int i;
343
344   for (i = 0; i < n->stages_num; i++)
345     sn_stage_show (n->stages[i]);
346
347   return (0);
348 } /* }}} int sn_network_show */
349
350 int sn_network_invert (sn_network_t *n) /* {{{ */
351 {
352   int i;
353
354   if (n == NULL)
355     return (EINVAL);
356
357   for (i = 0; i < n->stages_num; i++)
358     sn_stage_invert (n->stages[i]);
359
360   return (0);
361 } /* }}} int sn_network_invert */
362
363 int sn_network_shift (sn_network_t *n, int sw) /* {{{ */
364 {
365   int i;
366
367   if ((n == NULL) || (sw < 0))
368     return (EINVAL);
369
370   if (sw == 0)
371     return (0);
372
373   for (i = 0; i < n->stages_num; i++)
374     sn_stage_shift (n->stages[i], sw, SN_NETWORK_INPUT_NUM (n));
375
376   return (0);
377 } /* }}} int sn_network_shift */
378
379 int sn_network_compress (sn_network_t *n) /* {{{ */
380 {
381   int i;
382   int j;
383   int k;
384
385   for (i = 1; i < n->stages_num; i++)
386   {
387     sn_stage_t *s;
388
389     s = n->stages[i];
390
391     for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
392     {
393       sn_comparator_t *c = SN_STAGE_COMP_GET (s, j);
394       int move_to = i;
395
396       for (k = i - 1; k >= 0; k--)
397       {
398         int conflict;
399
400         conflict = sn_stage_comparator_check_conflict (n->stages[k], c);
401         if (conflict == 0)
402         {
403           move_to = k;
404           continue;
405         }
406
407         if (conflict == 2)
408           move_to = -1;
409         break;
410       }
411
412       if (move_to < i)
413       {
414         if (move_to >= 0)
415           sn_stage_comparator_add (n->stages[move_to], c);
416         sn_stage_comparator_remove (s, j);
417         j--;
418       }
419     }
420   }
421
422   while ((n->stages_num > 0)
423       && (SN_STAGE_COMP_NUM (n->stages[n->stages_num - 1]) == 0))
424     sn_network_stage_remove (n, n->stages_num - 1);
425
426   return (0);
427 } /* }}} int sn_network_compress */
428
429 int sn_network_normalize (sn_network_t *n) /* {{{ */
430 {
431   int i;
432
433   for (i = 0; i < n->stages_num; i++)
434   {
435     sn_stage_t *s;
436     int j;
437
438     s = n->stages[i];
439
440     for (j = 0; j < SN_STAGE_COMP_NUM (s); j++)
441     {
442       sn_comparator_t *c;
443       int min;
444       int max;
445
446       c = SN_STAGE_COMP_GET (s, j);
447
448       min = c->min;
449       max = c->max;
450
451       if (min > max)
452       {
453         int k;
454
455         for (k = i; k < n->stages_num; k++) 
456           sn_stage_swap (n->stages[k], min, max);
457
458         i = -1;
459         break; /* for (j) */
460       }
461     } /* for (j = 0 .. #comparators) */
462   } /* for (i = n->stages_num - 1 .. 0) */
463
464   return (0);
465 } /* }}} int sn_network_normalize */
466
467 int sn_network_remove_input (sn_network_t *n, int input) /* {{{ */
468 {
469   int i;
470
471   if ((n == NULL) || (input < 0) || (input >= n->inputs_num))
472     return (EINVAL);
473
474   for (i = 0; i < n->stages_num; i++)
475     sn_stage_remove_input (n->stages[i], input);
476
477   n->inputs_num--;
478
479   return (0);
480 } /* }}} int sn_network_remove_input */
481
482 int sn_network_cut_at (sn_network_t *n, int input, /* {{{ */
483     enum sn_network_cut_dir_e dir)
484 {
485   int i;
486   int position = input;
487
488   for (i = 0; i < n->stages_num; i++)
489   {
490     sn_stage_t *s;
491     int new_position;
492     
493     s = n->stages[i];
494     new_position = sn_stage_cut_at (s, position, dir);
495     
496     if (position != new_position)
497     {
498       int j;
499
500       for (j = 0; j < i; j++)
501         sn_stage_swap (n->stages[j], position, new_position);
502     }
503
504     position = new_position;
505   }
506
507   assert (((dir == DIR_MIN) && (position == 0))
508       || ((dir == DIR_MAX) && (position == (n->inputs_num - 1))));
509
510   sn_network_remove_input (n, position);
511
512   return (0);
513 } /* }}} int sn_network_cut_at */
514
515 int sn_network_cut (sn_network_t *n, int *mask) /* {{{ */
516 {
517   int inputs_num;
518   int i;
519
520   for (i = 0; i < n->stages_num; i++)
521   {
522     sn_stage_t *s = n->stages[i];
523
524     sn_stage_cut (s, mask, n->stages);
525   }
526
527   /* Use a copy of this member since it will be updated by
528    * sn_network_remove_input(). */
529   inputs_num = n->inputs_num;
530   for (i = 0; i < inputs_num; i++)
531   {
532     if (mask[i] < 0)
533       sn_network_remove_input (n, 0);
534     else if (mask[i] > 0)
535       sn_network_remove_input (n, n->inputs_num - 1);
536   }
537
538   return (0);
539 } /* }}} int sn_network_cut */
540
541 /* sn_network_concatenate
542  *
543  * `Glues' two networks together, resulting in a comparator network with twice
544  * as many inputs but one that doesn't really sort anymore. It produces a
545  * bitonic sequence, though, that can be used by the mergers below. */
546 static sn_network_t *sn_network_concatenate (sn_network_t *n0, /* {{{ */
547     sn_network_t *n1)
548 {
549   sn_network_t *n;
550   int stages_num;
551   int i;
552   int j;
553
554   stages_num = (n0->stages_num > n1->stages_num)
555     ? n0->stages_num
556     : n1->stages_num;
557
558   n = sn_network_create (n0->inputs_num + n1->inputs_num);
559   if (n == NULL)
560     return (NULL);
561
562   for (i = 0; i < stages_num; i++)
563   {
564     sn_stage_t *s = sn_stage_create (i);
565
566     if (i < n0->stages_num)
567       for (j = 0; j < SN_STAGE_COMP_NUM (n0->stages[i]); j++)
568       {
569         sn_comparator_t *c = SN_STAGE_COMP_GET (n0->stages[i], j);
570         sn_stage_comparator_add (s, c);
571       }
572
573     if (i < n1->stages_num)
574       for (j = 0; j < SN_STAGE_COMP_NUM (n1->stages[i]); j++)
575       {
576         sn_comparator_t *c_orig = SN_STAGE_COMP_GET (n1->stages[i], j);
577         sn_comparator_t  c_copy;
578
579         SN_COMP_MIN(&c_copy) = SN_COMP_MIN(c_orig) + n0->inputs_num;
580         SN_COMP_MAX(&c_copy) = SN_COMP_MAX(c_orig) + n0->inputs_num;
581
582         sn_stage_comparator_add (s, &c_copy);
583       }
584
585     sn_network_stage_add (n, s);
586   }
587
588   return (n);
589 } /* }}} sn_network_t *sn_network_concatenate */
590
591 static int sn_network_add_bitonic_merger_recursive (sn_network_t *n, /* {{{ */
592     int low, int num)
593 {
594   sn_stage_t *s;
595   int m;
596   int i;
597
598   if (num == 1)
599     return (0);
600
601   s = sn_stage_create (n->stages_num);
602   if (s == NULL)
603     return (-1);
604
605   m = num / 2;
606
607   for (i = low; i < (low + m); i++)
608   {
609     sn_comparator_t c;
610
611     c.min = i;
612     c.max = i + m;
613
614     sn_stage_comparator_add (s, &c);
615   }
616
617   sn_network_stage_add (n, s);
618
619   sn_network_add_bitonic_merger_recursive (n, low, m);
620   sn_network_add_bitonic_merger_recursive (n, low + m, m);
621
622   return (0);
623 } /* }}} int sn_network_add_bitonic_merger_recursive */
624
625 static int sn_network_add_bitonic_merger (sn_network_t *n) /* {{{ */
626 {
627 #if 0
628   sn_stage_t *s;
629   int m;
630   int i;
631
632   s = sn_stage_create (n->stages_num);
633   if (s == NULL)
634     return (-1);
635
636   m = n->inputs_num / 2;
637
638   for (i = 0; i < m; i++)
639   {
640     sn_comparator_t c;
641
642     c.min = i;
643     c.max = n->inputs_num - (i + 1);
644
645     sn_stage_comparator_add (s, &c);
646   }
647
648   sn_network_stage_add (n, s);
649
650   sn_network_add_bitonic_merger_recursive (n, 0, m);
651   sn_network_add_bitonic_merger_recursive (n, m, m);
652 #else
653   sn_network_add_bitonic_merger_recursive (n, 0, SN_NETWORK_INPUT_NUM (n));
654 #endif
655
656   return (0);
657 } /* }}} int sn_network_add_bitonic_merger */
658
659 static int sn_network_add_odd_even_merger (sn_network_t *n, /* {{{ */
660     int *indizes_left, int indizes_left_num,
661     int *indizes_right, int indizes_right_num)
662 {
663   int tmp_left[indizes_left_num];
664   int tmp_left_num;
665   int tmp_right[indizes_left_num];
666   int tmp_right_num;
667   int max_index;
668   sn_stage_t *s;
669   int i;
670
671   if ((indizes_left_num == 0) || (indizes_right_num == 0))
672   {
673     return (0);
674   }
675   else if ((indizes_left_num == 1) && (indizes_right_num == 1))
676   {
677     sn_comparator_t c;
678     sn_stage_t *s;
679
680     c.min = *indizes_left;
681     c.max = *indizes_right;
682
683     s = sn_stage_create (n->stages_num);
684     if (s == NULL)
685       return (-1);
686
687     sn_stage_comparator_add (s, &c);
688     sn_network_stage_add (n, s);
689
690     return (0);
691   }
692
693   /* Merge odd sequences */
694   tmp_left_num = (indizes_left_num + 1) / 2;
695   for (i = 0; i < tmp_left_num; i++)
696     tmp_left[i] = indizes_left[2 * i];
697
698   tmp_right_num = (indizes_right_num + 1) / 2;
699   for (i = 0; i < tmp_right_num; i++)
700     tmp_right[i] = indizes_right[2 * i];
701
702   sn_network_add_odd_even_merger (n,
703       tmp_left, tmp_left_num,
704       tmp_right, tmp_right_num);
705
706   /* Merge even sequences */
707   tmp_left_num = indizes_left_num / 2;
708   for (i = 0; i < tmp_left_num; i++)
709     tmp_left[i] = indizes_left[(2 * i) + 1];
710
711   tmp_right_num = indizes_right_num / 2;
712   for (i = 0; i < tmp_right_num; i++)
713     tmp_right[i] = indizes_right[(2 * i) + 1];
714
715   sn_network_add_odd_even_merger (n,
716       tmp_left, tmp_left_num,
717       tmp_right, tmp_right_num);
718
719   /* Apply ``comparison-interchange'' operations. */
720   s = sn_stage_create (n->stages_num);
721
722   max_index = indizes_left_num + indizes_right_num;
723   if ((max_index % 2) == 0)
724     max_index -= 3;
725   else
726     max_index -= 2;
727
728   for (i = 1; i <= max_index; i += 2)
729   {
730     sn_comparator_t c;
731
732     if (i < indizes_left_num)
733       c.min = indizes_left[i];
734     else
735       c.min = indizes_right[i - indizes_left_num];
736
737     if ((i + 1) < indizes_left_num)
738       c.max = indizes_left[i + 1];
739     else
740       c.max = indizes_right[i + 1 - indizes_left_num];
741
742     sn_stage_comparator_add (s, &c);
743   }
744
745   sn_network_stage_add (n, s);
746
747   return (0);
748 } /* }}} int sn_network_add_odd_even_merger */
749
750 static sn_network_t *sn_network_combine_bitonic_shift (sn_network_t *n0, /* {{{ */
751     sn_network_t *n1, int do_shift)
752 {
753   sn_network_t *n;
754   sn_network_t *n1_clone;
755   int shift;
756
757   n1_clone = sn_network_clone (n1);
758   if (n1_clone == NULL)
759     return (NULL);
760
761   sn_network_invert (n1_clone);
762
763   n = sn_network_concatenate (n0, n1_clone);
764   if (n == NULL)
765     return (NULL);
766
767   sn_network_destroy (n1_clone);
768
769   if (do_shift)
770     shift = sn_bounded_random (0, SN_NETWORK_INPUT_NUM (n) - 1);
771   else
772     shift = 0;
773
774   if (shift > 0)
775   {
776     DPRINTF ("sn_network_combine_bitonic_shift: Shifting by %i.\n", shift);
777     sn_network_shift (n, shift);
778   }
779
780   sn_network_add_bitonic_merger (n);
781
782   return (n);
783 } /* }}} sn_network_t *sn_network_combine_bitonic_shift */
784
785 sn_network_t *sn_network_combine_bitonic_merge (sn_network_t *n0, /* {{{ */
786     sn_network_t *n1)
787 {
788   return (sn_network_combine_bitonic_shift (n0, n1, /* do_shift = */ 0));
789 } /* }}} sn_network_t *sn_network_combine_bitonic_merge */
790
791 sn_network_t *sn_network_combine_odd_even_merge (sn_network_t *n0, /* {{{ */
792     sn_network_t *n1)
793 {
794   sn_network_t *n;
795   int indizes_left[n0->inputs_num];
796   int indizes_left_num;
797   int indizes_right[n1->inputs_num];
798   int indizes_right_num;
799   int status;
800   int i;
801
802   indizes_left_num = n0->inputs_num;
803   indizes_right_num = n1->inputs_num;
804   for (i = 0; i < indizes_left_num; i++)
805     indizes_left[i] = i;
806   for (i = 0; i < indizes_right_num; i++)
807     indizes_right[i] = indizes_left_num + i;
808
809   n = sn_network_concatenate (n0, n1);
810   if (n == NULL)
811     return (NULL);
812
813   status = sn_network_add_odd_even_merger (n,
814       indizes_left, indizes_left_num,
815       indizes_right, indizes_right_num);
816   if (status != 0)
817   {
818     sn_network_destroy (n);
819     return (NULL);
820   }
821
822   sn_network_compress (n);
823   return (n);
824 } /* }}} sn_network_t *sn_network_combine_odd_even_merge */
825
826 sn_network_t *sn_network_combine (sn_network_t *n0, /* {{{ */
827     sn_network_t *n1)
828 {
829   return (sn_network_combine_odd_even_merge (n0, n1));
830 } /* }}} sn_network_t *sn_network_combine */
831
832 int sn_network_sort (sn_network_t *n, int *values) /* {{{ */
833 {
834   int status;
835   int i;
836
837   status = 0;
838   for (i = 0; i < n->stages_num; i++)
839   {
840     status = sn_stage_sort (n->stages[i], values);
841     if (status != 0)
842       return (status);
843   }
844
845   return (status);
846 } /* }}} int sn_network_sort */
847
848 int sn_network_brute_force_check (sn_network_t *n) /* {{{ */
849 {
850   int test_pattern[n->inputs_num];
851   int values[n->inputs_num];
852   int status;
853   int i;
854
855   memset (test_pattern, 0, sizeof (test_pattern));
856   while (42)
857   {
858     int previous;
859     int overflow;
860
861     /* Copy the current pattern and let the network sort it */
862     memcpy (values, test_pattern, sizeof (values));
863     status = sn_network_sort (n, values);
864     if (status != 0)
865       return (status);
866
867     /* Check if the array is now sorted. */
868     previous = values[0];
869     for (i = 1; i < n->inputs_num; i++)
870     {
871       if (previous > values[i])
872         return (1);
873       previous = values[i];
874     }
875
876     /* Generate the next test pattern */
877     overflow = 1;
878     for (i = 0; i < n->inputs_num; i++)
879     {
880       if (test_pattern[i] == 0)
881       {
882         test_pattern[i] = 1;
883         overflow = 0;
884         break;
885       }
886       else
887       {
888         test_pattern[i] = 0;
889         overflow = 1;
890       }
891     }
892
893     /* Break out of the while loop if we tested all possible patterns */
894     if (overflow == 1)
895       break;
896   } /* while (42) */
897
898   /* All tests successfull */
899   return (0);
900 } /* }}} int sn_network_brute_force_check */
901
902 sn_network_t *sn_network_read (FILE *fh) /* {{{ */
903 {
904   sn_network_t *n;
905   char buffer[64];
906
907   int opt_inputs = 0;
908
909   while (fgets (buffer, sizeof (buffer), fh) != NULL)
910   {
911     char *str_key = buffer;
912     char *str_value = NULL;
913     int   buffer_len = strlen (buffer);
914
915     while ((buffer_len > 0) && ((buffer[buffer_len - 1] == '\n')
916           || (buffer[buffer_len - 1] == '\r')))
917     {
918       buffer_len--;
919       buffer[buffer_len] = '\0';
920     }
921     if (buffer_len == 0)
922       break;
923
924     str_value = strchr (buffer, ':');
925     if (str_value == NULL)
926     {
927       printf ("Cannot parse line: %s\n", buffer);
928       continue;
929     }
930
931     *str_value = '\0'; str_value++;
932     while ((*str_value != '\0') && (isspace (*str_value) != 0))
933       str_value++;
934
935     if (strcasecmp ("Inputs", str_key) == 0)
936       opt_inputs = atoi (str_value);
937     else
938       printf ("Unknown key: %s\n", str_key);
939   } /* while (fgets) */
940
941   if (opt_inputs < 2)
942     return (NULL);
943
944   n = sn_network_create (opt_inputs);
945
946   while (42)
947   {
948     sn_stage_t *s;
949
950     s = sn_stage_read (fh);
951     if (s == NULL)
952       break;
953
954     sn_network_stage_add (n, s);
955   }
956
957   if (SN_NETWORK_STAGE_NUM (n) < 1)
958   {
959     sn_network_destroy (n);
960     return (NULL);
961   }
962
963   return (n);
964 } /* }}} sn_network_t *sn_network_read */
965
966 sn_network_t *sn_network_read_file (const char *file) /* {{{ */
967 {
968   sn_network_t *n;
969   FILE *fh;
970
971   fh = fopen (file, "r");
972   if (fh == NULL)
973     return (NULL);
974
975   n = sn_network_read (fh);
976
977   fclose (fh);
978
979   return (n);
980 } /* }}} sn_network_t *sn_network_read_file */
981
982 int sn_network_write (sn_network_t *n, FILE *fh) /* {{{ */
983 {
984   int i;
985
986   fprintf (fh, "Inputs: %i\n", n->inputs_num);
987   fprintf (fh, "\n");
988
989   for (i = 0; i < n->stages_num; i++)
990     sn_stage_write (n->stages[i], fh);
991
992   return (0);
993 } /* }}} int sn_network_write */
994
995 int sn_network_write_file (sn_network_t *n, const char *file) /* {{{ */
996 {
997   int status;
998   FILE *fh;
999
1000   fh = fopen (file, "w");
1001   if (fh == NULL)
1002     return (-1);
1003
1004   status = sn_network_write (n, fh);
1005
1006   fclose (fh);
1007
1008   return (status);
1009 } /* }}} int sn_network_write_file */
1010
1011 int sn_network_serialize (sn_network_t *n, char **ret_buffer, /* {{{ */
1012     size_t *ret_buffer_size)
1013 {
1014   char *buffer;
1015   size_t buffer_size;
1016   int status;
1017   int i;
1018
1019   buffer = *ret_buffer;
1020   buffer_size = *ret_buffer_size;
1021
1022 #define SNPRINTF_OR_FAIL(...) \
1023   status = snprintf (buffer, buffer_size, __VA_ARGS__); \
1024   if ((status < 1) || (((size_t) status) >= buffer_size)) \
1025     return (-1); \
1026   buffer += status; \
1027   buffer_size -= status;
1028
1029   SNPRINTF_OR_FAIL ("Inputs: %i\r\n\r\n", n->inputs_num);
1030
1031   for (i = 0; i < n->stages_num; i++)
1032   {
1033     status = sn_stage_serialize (n->stages[i], &buffer, &buffer_size);
1034     if (status != 0)
1035       return (status);
1036   }
1037
1038   *ret_buffer = buffer;
1039   *ret_buffer_size = buffer_size;
1040   return (0);
1041 } /* }}} int sn_network_serialize */
1042
1043 sn_network_t *sn_network_unserialize (char *buffer, /* {{{ */
1044     size_t buffer_size)
1045 {
1046   sn_network_t *n;
1047   int opt_inputs = 0;
1048
1049   if (buffer_size == 0)
1050     return (NULL);
1051
1052   /* Read options first */
1053   while (buffer_size > 0)
1054   {
1055     char *endptr;
1056     char *str_key;
1057     char *str_value;
1058     char *line;
1059     int   line_len;
1060
1061     line = buffer;
1062     endptr = strchr (buffer, '\n');
1063     if (endptr == NULL)
1064       return (NULL);
1065
1066     *endptr = 0;
1067     endptr++;
1068     buffer = endptr;
1069     line_len = strlen (line);
1070
1071     if ((line_len > 0) && (line[line_len - 1] == '\r'))
1072     {
1073       line[line_len - 1] = 0;
1074       line_len--;
1075     }
1076
1077     if (line_len == 0)
1078       break;
1079
1080     str_key = line;
1081     str_value = strchr (line, ':');
1082     if (str_value == NULL)
1083     {
1084       printf ("Cannot parse line: %s\n", line);
1085       continue;
1086     }
1087
1088     *str_value = '\0'; str_value++;
1089     while ((*str_value != '\0') && (isspace (*str_value) != 0))
1090       str_value++;
1091
1092     if (strcasecmp ("Inputs", str_key) == 0)
1093       opt_inputs = atoi (str_value);
1094     else
1095       printf ("Unknown key: %s\n", str_key);
1096   } /* while (fgets) */
1097
1098   if (opt_inputs < 2)
1099     return (NULL);
1100
1101   n = sn_network_create (opt_inputs);
1102
1103   while (42)
1104   {
1105     sn_stage_t *s;
1106
1107     s = sn_stage_unserialize (&buffer, &buffer_size);
1108     if (s == NULL)
1109       break;
1110
1111     sn_network_stage_add (n, s);
1112   }
1113
1114   if (SN_NETWORK_STAGE_NUM (n) < 1)
1115   {
1116     sn_network_destroy (n);
1117     return (NULL);
1118   }
1119
1120   return (n);
1121 } /* }}} sn_network_t *sn_network_unserialize */
1122
1123 /* vim: set sw=2 sts=2 et fdm=marker : */