ucx/utils.c

changeset 505
481802342fdf
parent 335
c1bc13faadaa
child 747
efbd59642577
equal deleted inserted replaced
504:bf3695fee719 505:481802342fdf
86 return ncp; 86 return ncp;
87 } 87 }
88 88
89 /* COMPARE FUNCTIONS */ 89 /* COMPARE FUNCTIONS */
90 90
91 int ucx_strcmp(const void *s1, const void *s2, void *data) { 91 int ucx_cmp_str(const void *s1, const void *s2, void *data) {
92 return strcmp((const char*)s1, (const char*)s2); 92 return strcmp((const char*)s1, (const char*)s2);
93 } 93 }
94 94
95 int ucx_strncmp(const void *s1, const void *s2, void *n) { 95 int ucx_cmp_strn(const void *s1, const void *s2, void *n) {
96 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n)); 96 return strncmp((const char*)s1, (const char*)s2, *((size_t*) n));
97 } 97 }
98 98
99 int ucx_intcmp(const void *i1, const void *i2, void *data) { 99 int ucx_cmp_sstr(const void *s1, const void *s2, void *data) {
100 sstr_t a = *(const sstr_t*) s1;
101 sstr_t b = *(const sstr_t*) s2;
102 return sstrcmp(a, b);
103 }
104
105 int ucx_cmp_int(const void *i1, const void *i2, void *data) {
100 int a = *((const int*) i1); 106 int a = *((const int*) i1);
101 int b = *((const int*) i2); 107 int b = *((const int*) i2);
102 if (a == b) { 108 if (a == b) {
103 return 0; 109 return 0;
104 } else { 110 } else {
105 return a < b ? -1 : 1; 111 return a < b ? -1 : 1;
106 } 112 }
107 } 113 }
108 114
109 int ucx_floatcmp(const void *f1, const void *f2, void *epsilon) { 115 int ucx_cmp_longint(const void *i1, const void *i2, void *data) {
116 int a = *((const long int*) i1);
117 int b = *((const long int*) i2);
118 if (a == b) {
119 return 0;
120 } else {
121 return a < b ? -1 : 1;
122 }
123 }
124
125 intmax_t ucx_dist_int(const void *i1, const void *i2, void *data) {
126 intmax_t a = *((const int*) i1);
127 intmax_t b = *((const int*) i2);
128 return a - b;
129 }
130
131 intmax_t ucx_dist_longint(const void *i1, const void *i2, void *data) {
132 intmax_t a = *((const long int*) i1);
133 intmax_t b = *((const long int*) i2);
134 return a - b;
135 }
136
137 int ucx_cmp_float(const void *f1, const void *f2, void *epsilon) {
110 float a = *((const float*) f1); 138 float a = *((const float*) f1);
111 float b = *((const float*) f2); 139 float b = *((const float*) f2);
112 float e = !epsilon ? 1e-6f : *((float*)epsilon); 140 float e = !epsilon ? 1e-6f : *((float*)epsilon);
113 if (fabsf(a - b) < e) { 141 if (fabsf(a - b) < e) {
114 return 0; 142 return 0;
115 } else { 143 } else {
116 return a < b ? -1 : 1; 144 return a < b ? -1 : 1;
117 } 145 }
118 } 146 }
119 147
120 int ucx_doublecmp(const void *d1, const void *d2, void *epsilon) { 148 int ucx_cmp_double(const void *d1, const void *d2, void *epsilon) {
121 double a = *((const double*) d1); 149 double a = *((const double*) d1);
122 double b = *((const double*) d2); 150 double b = *((const double*) d2);
123 double e = !epsilon ? 1e-14 : *((double*)epsilon); 151 double e = !epsilon ? 1e-14 : *((double*)epsilon);
124 if (fabs(a - b) < e) { 152 if (fabs(a - b) < e) {
125 return 0; 153 return 0;
126 } else { 154 } else {
127 return a < b ? -1 : 1; 155 return a < b ? -1 : 1;
128 } 156 }
129 } 157 }
130 158
131 int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data) { 159 int ucx_cmp_ptr(const void *ptr1, const void *ptr2, void *data) {
132 const intptr_t p1 = (const intptr_t) ptr1; 160 const intptr_t p1 = (const intptr_t) ptr1;
133 const intptr_t p2 = (const intptr_t) ptr2; 161 const intptr_t p2 = (const intptr_t) ptr2;
134 if (p1 == p2) { 162 if (p1 == p2) {
135 return 0; 163 return 0;
136 } else { 164 } else {
137 return p1 < p2 ? -1 : 1; 165 return p1 < p2 ? -1 : 1;
138 } 166 }
139 } 167 }
140 168
141 int ucx_memcmp(const void *ptr1, const void *ptr2, void *n) { 169 int ucx_cmp_mem(const void *ptr1, const void *ptr2, void *n) {
142 return memcmp(ptr1, ptr2, *((size_t*)n)); 170 return memcmp(ptr1, ptr2, *((size_t*)n));
143 } 171 }
144 172
145 /* PRINTF FUNCTIONS */ 173 /* PRINTF FUNCTIONS */
146 174

mercurial