BRL-CAD
bu_sort.c
Go to the documentation of this file.
1 /* B U _ S O R T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 #include "bu.h"
23 #include "bn.h"
24 #include "string.h"
25 
26 
27 /* sort from small to big for unsigned int */
28 int
29 comp_1(const void *num1, const void *num2, void *UNUSED(arg))
30 {
31  return (*((unsigned int *)num1) - *((unsigned int *)num2));
32 }
33 
34 
35 /* sort from small to big for fastf_t */
36 int
37 comp_2(const void *num1, const void *num2, void *UNUSED(arg))
38 {
39  if (*(fastf_t *)num1 > *(fastf_t *)num2)
40  return 1;
41  else if ((*(fastf_t *)num1 < *(fastf_t *)num2))
42  return -1;
43  return 0;
44 }
45 
46 
47 /* sort strings based on ASCII-table */
48 int
49 comp_3(const void *str1, const void *str2, void *UNUSED(arg))
50 {
51  return bu_strcmp((char *)str1, (char *)str2);
52 }
53 
54 
55 /* sort fastf_t's by their distance to cmp */
56 int
57 comp_4(const void *num1, const void *num2, void *cmp)
58 {
59  if (fabs(*(fastf_t *)num1 - *(fastf_t *)cmp) > fabs((*(fastf_t *)num2)- *(fastf_t *)cmp))
60  return 1;
61  else if (fabs(*(fastf_t *)num1 - *(fastf_t *)cmp) < fabs((*(fastf_t *)num2)- *(fastf_t *)cmp))
62  return -1;
63  return 0;
64 }
65 
66 
67 int
68 main(int argc, char **argv)
69 {
70  int arg_1[6] = {5, 2, 6, -15, 168, 3};
71  int exp_1[6] = {-15, 2, 3, 5, 6, 168};
72  unsigned int arg_2[8] = {56, 4, 7, 156, 2, 0, 23, 8};
73  unsigned int exp_2[8] = {0, 2, 4, 7, 8, 23, 56, 156};
74  fastf_t arg_3[5] = {5.5, 3.8, -5.5, 1, -7};
75  fastf_t exp_3[5] = {-7, -5.5, 1, 3.8, 5.5};
76  fastf_t arg_4[7] = {7.42, -5.2, -5.9, 7.36, 7.0, 0, 7.36};
77  fastf_t exp_4[7] = {-5.9, -5.2, 0, 7.0, 7.36, 7.36, 7.42};
78  char arg_5[4][256] = {"Zfg", "ZFg", "azf", "bzf"};
79  char exp_5[4][256] = {"ZFg", "Zfg", "azf", "bzf"};
80  char arg_6[3][256] = {"test", "BAB", "aab"};
81  char exp_6[3][256] = {"BAB", "aab", "test"};
82  fastf_t cmp_7 = -2;
83  fastf_t arg_7[9] = {-3, 7, -9, 34, 33, -34, 0, -12, 6};
84  fastf_t exp_7[9] = {-3, 0, -9, 6, 7, -12, -34, 33, 34};
85  fastf_t cmp_8 = 3;
86  fastf_t arg_8[5] = {-5, 23, 5.5, 0, 2};
87  fastf_t exp_8[5] = {2, 5.5, 0, -5, 23};
88  fastf_t arg_9[5] = {0, 0, 0, 0, 0};
89  fastf_t exp_9[5] = {0, 0, 0, 0, 0};
90  fastf_t arg_10[5] = {INFINITY, 5, 5.2, 0, -INFINITY};
91  fastf_t exp_10[5] = {-INFINITY, 0, 5, 5.2, INFINITY};
92  int i, function_num;
93 
94  if (argc != 2)
95  bu_exit(1, "ERROR: wrong number of parameters");
96  sscanf(argv[1], "%d", &function_num);
97  switch (function_num) {
98  case 1:
99  bu_sort(&arg_1, 6, sizeof(int), comp_1, NULL);
100  for (i = 0; i < 6; i++)
101  if (arg_1[i] != exp_1[i])
102  return 1;
103  break;
104  case 2:
105  bu_sort(&arg_2, 8, sizeof(int), comp_1, NULL);
106  for (i = 0; i < 8; i++)
107  if (arg_2[i] != exp_2[i])
108  return 1;
109  break;
110  case 3:
111  bu_sort(&arg_3, 5, sizeof(fastf_t), comp_2, NULL);
112  for (i = 0; i < 5; i++)
113  if (!EQUAL(arg_3[i], exp_3[i]))
114  return 1;
115  break;
116  case 4:
117  bu_sort(&arg_4, 7, sizeof(fastf_t), comp_2, NULL);
118  for (i = 0; i < 7; i++)
119  if (!EQUAL(arg_4[i], exp_4[i]))
120  return 1;
121  break;
122  case 5:
123  bu_sort(&arg_5, 4, sizeof(char[256]), comp_3, NULL);
124  for (i = 0; i < 4; i++)
125  if (bu_strcmp(arg_5[i], exp_5[i]) != 0)
126  return 1;
127  break;
128  case 6:
129  bu_sort(&arg_6, 3, sizeof(char[256]), comp_3, NULL);
130  for (i = 0; i < 3; i++)
131  if (bu_strcmp(arg_6[i], exp_6[i]) != 0)
132  return 1;
133  break;
134  case 7:
135  bu_sort(&arg_7, 9, sizeof(fastf_t), comp_4, &cmp_7);
136  for (i = 0; i < 9; i++)
137  if (!EQUAL(arg_7[i], exp_7[i]))
138  return 1;
139  break;
140  case 8:
141  bu_sort(&arg_8, 5, sizeof(fastf_t), comp_4, &cmp_8);
142  for (i = 0; i < 5; i++)
143  if (!EQUAL(arg_8[i], exp_8[i]))
144  return 1;
145  break;
146  case 9:
147  bu_sort(&arg_9, 5, sizeof(fastf_t), comp_2, NULL);
148  for (i = 0; i < 5; i++)
149  if (!EQUAL(arg_9[i], exp_9[i]))
150  return 1;
151  break;
152  case 10:
153  bu_sort(&arg_10, 5, sizeof(fastf_t), comp_2, NULL);
154  for (i = 0; i < 5; i++)
155  if ((!EQUAL(arg_10[i], exp_10[i]) && (!isinf(arg_10[i]) || !isinf(exp_10[i]))) || ((exp_10[i] < 0) != (arg_10[i] < 0)))
156  return 1;
157  break;
158  }
159  return 0;
160 }
161 
162 /*
163  * Local Variables:
164  * mode: C
165  * tab-width: 8
166  * indent-tabs-mode: t
167  * c-file-style: "stroustrup"
168  * End:
169  * ex: shiftwidth=4 tabstop=8
170  */
Header file for the BRL-CAD common definitions.
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
void bu_sort(void *array, size_t nummemb, size_t sizememb, int(*compare)(const void *, const void *, void *), void *context)
Definition: sort.c:110
#define UNUSED(parameter)
Definition: common.h:239
int comp_2(const void *num1, const void *num2, void *arg)
Definition: bu_sort.c:37
int bu_strcmp(const char *string1, const char *string2)
Definition: str.c:171
int comp_1(const void *num1, const void *num2, void *arg)
Definition: bu_sort.c:29
int comp_3(const void *str1, const void *str2, void *arg)
Definition: bu_sort.c:49
int comp_4(const void *num1, const void *num2, void *cmp)
Definition: bu_sort.c:57
int main(int argc, char **argv)
Definition: bu_sort.c:68
double fastf_t
Definition: defines.h:300