1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 *
16 * The Original Code is Copyright (C) 2013 Blender Foundation.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup bli
22 *
23 * Utility functions for sorting common types.
24 */
25
26 #include "BLI_sort_utils.h" /* own include */
27
28 struct SortAnyByFloat {
29 float sort_value;
30 };
31
32 struct SortAnyByInt {
33 int sort_value;
34 };
35
36 struct SortAnyByPtr {
37 const void *sort_value;
38 };
39
BLI_sortutil_cmp_float(const void * a_,const void * b_)40 int BLI_sortutil_cmp_float(const void *a_, const void *b_)
41 {
42 const struct SortAnyByFloat *a = a_;
43 const struct SortAnyByFloat *b = b_;
44 if (a->sort_value > b->sort_value) {
45 return 1;
46 }
47 if (a->sort_value < b->sort_value) {
48 return -1;
49 }
50
51 return 0;
52 }
53
BLI_sortutil_cmp_float_reverse(const void * a_,const void * b_)54 int BLI_sortutil_cmp_float_reverse(const void *a_, const void *b_)
55 {
56 const struct SortAnyByFloat *a = a_;
57 const struct SortAnyByFloat *b = b_;
58 if (a->sort_value < b->sort_value) {
59 return 1;
60 }
61 if (a->sort_value > b->sort_value) {
62 return -1;
63 }
64
65 return 0;
66 }
67
BLI_sortutil_cmp_int(const void * a_,const void * b_)68 int BLI_sortutil_cmp_int(const void *a_, const void *b_)
69 {
70 const struct SortAnyByInt *a = a_;
71 const struct SortAnyByInt *b = b_;
72 if (a->sort_value > b->sort_value) {
73 return 1;
74 }
75 if (a->sort_value < b->sort_value) {
76 return -1;
77 }
78
79 return 0;
80 }
81
BLI_sortutil_cmp_int_reverse(const void * a_,const void * b_)82 int BLI_sortutil_cmp_int_reverse(const void *a_, const void *b_)
83 {
84 const struct SortAnyByInt *a = a_;
85 const struct SortAnyByInt *b = b_;
86 if (a->sort_value < b->sort_value) {
87 return 1;
88 }
89 if (a->sort_value > b->sort_value) {
90 return -1;
91 }
92
93 return 0;
94 }
95
BLI_sortutil_cmp_ptr(const void * a_,const void * b_)96 int BLI_sortutil_cmp_ptr(const void *a_, const void *b_)
97 {
98 const struct SortAnyByPtr *a = a_;
99 const struct SortAnyByPtr *b = b_;
100 if (a->sort_value > b->sort_value) {
101 return 1;
102 }
103 if (a->sort_value < b->sort_value) {
104 return -1;
105 }
106
107 return 0;
108 }
109
BLI_sortutil_cmp_ptr_reverse(const void * a_,const void * b_)110 int BLI_sortutil_cmp_ptr_reverse(const void *a_, const void *b_)
111 {
112 const struct SortAnyByPtr *a = a_;
113 const struct SortAnyByPtr *b = b_;
114 if (a->sort_value < b->sort_value) {
115 return 1;
116 }
117 if (a->sort_value > b->sort_value) {
118 return -1;
119 }
120
121 return 0;
122 }
123