-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.c
259 lines (211 loc) · 5.13 KB
/
types.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* types.c
*
* Created on: 4 nov. 2018
* Author: migueltoro
*/
#include "types.h"
/* Hash a string */
unsigned long int hash(const char *key) {
unsigned long int hashval = 0 ;
int i = 0;
/* Convert our string to an integer */
while( hashval < ULONG_MAX && i < strlen( key ) ) {
hashval = hashval << 5;
hashval += key[ i ];
i++;
}
return hashval;
}
// int type
char * int_tostring(const void * e, char * mem){
int a = *(int *)e;
sprintf(mem,"%d",a);
return mem;
}
long int_hashcode(const void * e){
int a = *(int *)e;
char mem[256];
sprintf(mem,"%d",a);
return hash(mem);
}
int int_equals(const void * e1, const void * e2){
int a = *(int *)e1;
int b = *(int *)e2;
return a == b;
}
int int_naturalorder(const void * e1,const void * e2){
int a = *(int *)e1;
int b = *(int *)e2;
int r;
if(a==b) r = 0;
else if(a<b) r = -1;
else r = +1;
return r;
}
// long type
char * long_tostring(const void * e, char * mem){
long a = *(long *)e;
sprintf(mem,"%ld",a);
return mem;
}
long long_hashcode(const void * e){
long a = *(long *)e;
char mem[256];
sprintf(mem,"%ld",a);
return hash(mem);
}
int long_equals(const void * e1,const void * e2){
long a = *(long *)e1;
long b = *(long *)e2;
return a == b;
}
int long_naturalorder(const void * e1,const void * e2){
long a = *(long *)e1;
long b = *(long *)e2;
long r;
if(a==b) r = 0;
else if(a<b) r = -1;
else r = +1;
return r;
}
// float type
char * float_tostring(const void * e, char * mem){
float a = *(float *)e;
sprintf(mem,"%.2f",a);
return mem;
}
long float_hashcode(const void * e){
float a = *(float *)e;
char mem[256];
sprintf(mem,"%.2f",a);
return hash(mem);
}
int float_equals(const void * e1, const void * e2){
float a = *(float *)e1;
float b = *(float *)e2;
return a == b;
}
int float_naturalorder(const void * e1, const void * e2){
float a = *(float *)e1;
float b = *(float *)e2;
int r;
if(a==b) r = 0;
else if(a<b) r = -1;
else r = +1;
return r;
}
//double type
char * double_tostring(const void * e, char * mem){
double a = *(double *)e;
sprintf(mem,"%0.2lf",a);
return mem;
}
long double_hashcode(const void * e){
double a = *(double *)e;
char mem[256];
sprintf(mem,"%.2lf",a);
return hash(mem);
}
int double_equals(const void * e1, const void * e2){
double a = *(double *)e1;
double b = *(double *)e2;
return a == b;
}
int double_naturalorder(const void * e1, const void * e2){
double a = *(double *)e1;
double b = *(double *)e2;
int r;
if(a==b) r = 0;
else if(a<b) r = -1;
else r = +1;
return r;
}
// string type
char * string_tostring(const void * e, char * mem) {
char * a = (char *) e;
return a;
}
long string_hashcode(const void * e) {
char * a = (char *) e;
return hash(a);
}
int string_equals(const void * e1, const void * e2) {
char * a1 = (char *) e1;
char * a2 = (char *) e2;
return strcmp(a1, a2) == 0;
}
int string_naturalorder(const void * e1, const void * e2) {
char * a1 = (char *) e1;
char * a2 = (char *) e2;
return strcmp(a1, a2);
}
char * remove_eol(char * string){
int len = strlen(string);
if (len>0) {
string[len-1] = '\0';
}
return string;
}
char * int_pair_tostring(const void * p, char * mem){
int_pair np = *(int_pair *)p;
sprintf(mem,"(%d,%d)",np.a,np.b);
return mem;
}
int int_pair_equals(const void * p1, const void * p2){
int_pair np1 = *(int_pair *)p1;
int_pair np2 = *(int_pair *)p2;
return np1.a == np2.a && np1.b == np2.b;
}
long int_pair_hashcode(const void * p){
char mem[256];
char * s = int_pair_tostring(p,mem);
return hash(s);
}
int int_pair_naturalorder(const void * p1, const void * p2){
int_pair np1 = *(int_pair *)p1;
int_pair np2 = *(int_pair *)p2;
int r = int_naturalorder(&np1.a,&np2.a);
if(r==0) r = int_naturalorder(&np1.b,&np2.b);
return r;
}
Cuadrante punto_cuadrante(const punto p) {
Cuadrante r;
if(p.x >=0 && p.y>=0){
r = PRIMERO;
} else if(p.x < 0 && p.y>=0) {
r = SEGUNDO;
} else if(p.x < 0 && p.y<0) {
r = TERCERO;
} else {
r = CUARTO;
}
return r;
}
double punto_distancia_al_origen(const punto p){
double x2 = p.x*p.x;
double y2 = p.y*p.y;
return sqrt(x2+y2);
}
char * punto_tostring(const void * p, char * mem){
punto np = *(punto *)p;
sprintf(mem,"(%lf,%lf)",np.x,np.y);
return mem;
}
int punto_equals(const void * p1, const void * p2){
punto np1 = *(punto *)p1;
punto np2 = *(punto *)p2;
return np1.x == np2.x && np1.y == np2.y;
}
long punto_hashcode(const void * p){
char mem[256];
char * s = punto_tostring(p,mem);
return hash(s);
}
int punto_naturalorder(const void * p1, const void * p2){
punto np1 = *(punto *)p1;
punto np2 = *(punto *)p2;
double d1 = punto_distancia_al_origen(np1);
double d2 = punto_distancia_al_origen(np2);
return double_naturalorder(&d1,&d2);
}