2
2
#include <stdlib.h>
3
3
#include <string.h>
4
4
#include <stdbool.h>
5
+ #include <ctype.h>
6
+
5
7
#define true 1
6
8
#define false 0
7
- #define DEBUG if(1 )
9
+ #define DEBUG if(0 )
8
10
#define MAX_SIZE 10000
9
11
/*
10
12
1. ADD(S, k) = adiciona o int k ao conjunto S, retornando True se o int foi corretamente adicionado ou false se o int já pertencia ao conjunto.
@@ -40,15 +42,15 @@ list* initList()
40
42
new_list -> size = 0 ;
41
43
return new_list ;
42
44
}
43
- table * initTable ()
45
+ table * initTable (int base )
44
46
{
45
47
table * newTable = (table * ) malloc (sizeof (table ));
46
48
for (int i = 0 ; i < 100 ; i ++ )
47
49
{
48
50
newTable -> tables [i ] = initList ();
49
51
newTable -> numOfData [i ] = 0 ;
50
52
}
51
- newTable -> base = 7 ;
53
+ newTable -> base = base ;
52
54
return newTable ;
53
55
}
54
56
bool addListHead (list * lista , int value )
@@ -60,40 +62,70 @@ bool addListHead(list* lista, int value)
60
62
lista -> head = new_node ;
61
63
lista -> tail = new_node ;
62
64
lista -> size ++ ;
63
- return ;
65
+ return true ;
64
66
}
65
67
new_node -> next = lista -> head ;
66
68
lista -> head = new_node ;
67
69
lista -> size ++ ;
70
+ return true;
68
71
}
69
72
bool addHashed (table * ht , int value , int key )
70
73
{
71
74
return addListHead (ht -> tables [key ],value );
72
75
}
73
- bool ADD (table * S , int K , int key
74
- )
76
+ bool DEL (table * S , int K ,int key ,int * comparCount )//remove
75
77
{
76
- if (!HAS (S ,K ))
78
+ //DEBUG printf("deletando [%d]\t",K);
79
+ node * tmp = S -> tables [key ]-> head ;
80
+ while (tmp != NULL )
77
81
{
78
- addHashed (S ,K ,)
79
- return true;
82
+ if (tmp -> data == K ){* comparCount += 1 ;return true;}
83
+ tmp = tmp -> next ;
84
+ * comparCount += 1 ;
80
85
}
81
- bool DEL (table * S , int K )//remove
82
- {
83
- return true;
86
+ return false;
84
87
}
85
- bool HAS (table * S , int K )//percente
88
+ bool HAS (table * S , int K , int * comparCount )//percente
86
89
{
90
+ //DEBUG printf("\ncheguei em HAS! \n");
91
+ int voltas = 0 ;
87
92
int key = hashFunction (K ,S -> base );
93
+ DEBUG printf ("cheguei na chave [%d],base = [%d], k = [%d]\n" ,key ,S -> base ,K );
88
94
node * tmp = S -> tables [key ]-> head ;
89
- while (S -> tables [key ] != NULL )
95
+ //if (tmp == NULL) DEBUG printf("CABEÇA NULA!\n");
96
+ while (tmp != NULL )
90
97
{
91
- if (tmp -> data == K )return true;
98
+ //DEBUG printf("oi\n");
99
+ if (tmp -> data == K )
100
+ {
101
+ * comparCount += 1 ;
102
+ voltas ++ ;
103
+ DEBUG printf ("voltinhas [%d]\n" ,voltas );
104
+ return true;
105
+ }
106
+ * comparCount += 1 ;
107
+ voltas ++ ;
108
+ //DEBUG printf("comparCount = [%d], voltas= [%d]\n",*comparCount,voltas);
92
109
tmp = tmp -> next ;
93
-
94
110
}
95
111
return false;
96
112
}
113
+ bool ADD (table * S , int K , int key , int * comparCount )
114
+ {
115
+ int ptr = 0 ;
116
+ if (!HAS (S ,K , & ptr ))
117
+ {
118
+ DEBUG printf ("======================= ADICIONANDO ->\t chave[%d],base = [%d], k = [%d]\n" ,key ,S -> base ,K );
119
+ addHashed (S ,K ,key );
120
+ * comparCount = ptr ;
121
+ return true;
122
+ }
123
+ else
124
+ {
125
+ * comparCount = ptr ;
126
+ return false;
127
+ }
128
+ }
97
129
int AtoI (char input []) //returns an int from a string
98
130
{
99
131
int len = strlen (input )- 3 ;
@@ -102,52 +134,120 @@ int AtoI(char input[]) //returns an int from a string
102
134
while (* ptr ) {
103
135
if (isdigit (* ptr )) { //if string + x is digit
104
136
long val = strtol (ptr , & ptr , 10 ); //transforms the characters from string into base10 ints.
105
- DEBUG printf ("%ld\n" , val );
137
+ // DEBUG printf("%ld\n", val);
106
138
ans = val ;
107
139
} else {
108
140
ptr ++ ;
109
141
}
110
142
}
111
143
return ans ;
144
+ }
145
+ int biggest (table * ht )
146
+ {
147
+ int max = 0 ;
148
+ for (int i = 0 ; i < 100 ; i ++ )
149
+ {
150
+ if (ht -> numOfData [i ]> max ) max = ht -> numOfData [i ];
151
+ }
152
+ }
153
+ table * rehash (table * ht , int numCount )
154
+ {
155
+ int ptr = 0 ;
156
+ int new_base = 2 * (ht -> base )- 1 ;
157
+ table * new_table = initTable (new_base );
158
+ if (numCount /ht -> base >= 0.75 )
159
+ {
160
+ DEBUG printf ("%d/%d = %d\n" ,numCount ,ht -> base ,numCount /ht -> base );
161
+ DEBUG printf ("\n!!!!REHASH?!?!?!?\n" );
162
+ for (int i = 0 ; i < ht -> base ; i ++ )
163
+ {
164
+ node * tmp = ht -> tables [i ]-> head ;
165
+ while (tmp != NULL )
166
+ {
167
+ int oldkey , value , newkey ;
168
+ oldkey = i ;
169
+ value = tmp -> data ;
170
+ DEBUG printf ("fazendo rehash de [%d]\t" ,value );
171
+ newkey = hashFunction (value ,new_base );
172
+ ADD (new_table ,value ,newkey ,& ptr );
173
+ tmp = tmp -> next ;
174
+ }
175
+ }
176
+ free (ht );
177
+ return new_table ;
178
+ }
179
+ else
180
+ {
181
+ return ht ;
182
+ }
183
+
184
+
112
185
}
113
186
int main ()
114
187
{
115
- int count = 0 ;
188
+ int stepCount = 0 ;
189
+ int numCount = 0 ;
116
190
int base = 7 ;
117
191
int key ;
192
+ int res ;
193
+ int lenghtOfTable = 7 ;
118
194
char input [300 ];
119
- table * ht = initTable ();
195
+ int comparCount = 0 ;
196
+ table * ht = initTable (base );
120
197
while (gets (input ))
121
198
{
122
- int buffer ;
199
+ long buffer ;
123
200
switch (input [2 ])
124
201
{
125
202
case 'D' ://add
126
- //DEBUG printf("[%s]\n",input);
127
203
buffer = AtoI (input );
128
- //ADD(ht,buffer);
129
204
key = hashFunction (buffer ,base );
130
- addHashed (ht ,buffer ,key );
131
- //rehash?
205
+ comparCount = 0 ;
206
+ //DEBUG printf("ADICIONANDO [%d]\n",buffer);
207
+ res = ADD (ht ,buffer ,key ,& comparCount );
208
+ numCount ++ ;
209
+ ht = rehash (ht ,numCount );
210
+ //DEBUG printf("PASSOU! -: [%s]\n",input);
211
+ printf ("%d " ,stepCount );
212
+ printf ("%d " ,res );
213
+ printf ("%d\n" ,comparCount );
132
214
break ;
133
215
134
216
case 'S' ://has
135
217
buffer = AtoI (input );
136
218
key = hashFunction (buffer ,base );
137
- HAS (ht ,buffer );
219
+ comparCount = 0 ;
220
+ res = HAS (ht ,buffer ,& comparCount );
221
+ printf ("%d " ,stepCount );
222
+ //DEBUG printf("HAS [%d] ",buffer);
223
+ printf ("%d " ,res );
224
+ printf ("%d\n" ,comparCount );
138
225
break ;
226
+
227
+
139
228
case 'L' ://del
140
229
buffer = AtoI (input );
141
230
key = hashFunction (buffer ,base );
142
-
143
- DEL (ht , buffer );
231
+ comparCount = 0 ;
232
+ //DEBUG printf("DELETANDO [%d],key[%d]\n",buffer,key);
233
+ res = DEL (ht ,buffer ,key ,& comparCount );
234
+ printf ("%d " ,stepCount );
235
+ printf ("%d " ,res );
236
+ printf ("%d\n" ,comparCount );
237
+ numCount -- ;
144
238
break ;
239
+
240
+
145
241
case 'T' ://prt
146
- DEBUG printf ("PRT" );
242
+ //DEBUG printf("PRT");
243
+ printf ("%d " ,stepCount );
244
+ printf ("%d " ,ht -> base );
245
+ printf ("%d " ,numCount );
246
+ printf ("%d\n" ,biggest (ht ));
147
247
break ;
148
248
}
149
249
memset (input ,0 ,strlen (input ));
150
- count ++ ;
250
+ stepCount ++ ;
151
251
}
152
252
153
253
@@ -254,4 +354,8 @@ HAS 95
254
354
HAS 50
255
355
HAS 71
256
356
PRT
257
- */
357
+ */
358
+
359
+
360
+
361
+ //TODO quando chega na linha 13, e na linha 15, parece que nao há adição de 59
0 commit comments