Skip to content

Commit f77863c

Browse files
committed
it's going but not too far...
1 parent 3b30c6b commit f77863c

File tree

1 file changed

+134
-30
lines changed

1 file changed

+134
-30
lines changed

hashset.c

+134-30
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#include <stdlib.h>
33
#include <string.h>
44
#include <stdbool.h>
5+
#include <ctype.h>
6+
57
#define true 1
68
#define false 0
7-
#define DEBUG if(1)
9+
#define DEBUG if(0)
810
#define MAX_SIZE 10000
911
/*
1012
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()
4042
new_list->size = 0;
4143
return new_list;
4244
}
43-
table* initTable()
45+
table* initTable(int base)
4446
{
4547
table* newTable = (table*) malloc(sizeof(table));
4648
for (int i = 0; i < 100; i++)
4749
{
4850
newTable->tables[i] = initList();
4951
newTable->numOfData[i] = 0;
5052
}
51-
newTable->base = 7;
53+
newTable->base = base;
5254
return newTable;
5355
}
5456
bool addListHead(list* lista, int value)
@@ -60,40 +62,70 @@ bool addListHead(list* lista, int value)
6062
lista->head = new_node;
6163
lista->tail = new_node;
6264
lista->size++;
63-
return;
65+
return true;
6466
}
6567
new_node->next = lista->head;
6668
lista->head = new_node;
6769
lista->size++;
70+
return true;
6871
}
6972
bool addHashed(table* ht, int value, int key)
7073
{
7174
return addListHead(ht->tables[key],value);
7275
}
73-
bool ADD(table* S, int K, int key
74-
)
76+
bool DEL(table* S, int K,int key,int* comparCount)//remove
7577
{
76-
if (!HAS(S,K))
78+
//DEBUG printf("deletando [%d]\t",K);
79+
node* tmp = S->tables[key]->head;
80+
while (tmp != NULL)
7781
{
78-
addHashed(S,K,)
79-
return true;
82+
if(tmp->data == K){*comparCount += 1;return true;}
83+
tmp = tmp->next;
84+
*comparCount += 1;
8085
}
81-
bool DEL(table* S, int K)//remove
82-
{
83-
return true;
86+
return false;
8487
}
85-
bool HAS(table* S, int K)//percente
88+
bool HAS(table* S, int K, int* comparCount)//percente
8689
{
90+
//DEBUG printf("\ncheguei em HAS! \n");
91+
int voltas = 0;
8792
int key = hashFunction(K,S->base);
93+
DEBUG printf("cheguei na chave [%d],base = [%d], k = [%d]\n",key,S->base,K);
8894
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)
9097
{
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);
92109
tmp = tmp->next;
93-
94110
}
95111
return false;
96112
}
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+
}
97129
int AtoI(char input[]) //returns an int from a string
98130
{
99131
int len = strlen(input)-3;
@@ -102,52 +134,120 @@ int AtoI(char input[]) //returns an int from a string
102134
while (*ptr) {
103135
if (isdigit(*ptr)) { //if string + x is digit
104136
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);
106138
ans = val;
107139
} else {
108140
ptr++;
109141
}
110142
}
111143
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+
112185
}
113186
int main()
114187
{
115-
int count = 0;
188+
int stepCount = 0;
189+
int numCount = 0;
116190
int base = 7;
117191
int key;
192+
int res;
193+
int lenghtOfTable = 7;
118194
char input[300];
119-
table* ht = initTable();
195+
int comparCount = 0;
196+
table* ht = initTable(base);
120197
while (gets(input))
121198
{
122-
int buffer;
199+
long buffer;
123200
switch (input[2])
124201
{
125202
case 'D'://add
126-
//DEBUG printf("[%s]\n",input);
127203
buffer = AtoI(input);
128-
//ADD(ht,buffer);
129204
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);
132214
break;
133215

134216
case 'S'://has
135217
buffer = AtoI(input);
136218
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);
138225
break;
226+
227+
139228
case 'L'://del
140229
buffer = AtoI(input);
141230
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--;
144238
break;
239+
240+
145241
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));
147247
break;
148248
}
149249
memset(input,0,strlen(input));
150-
count++;
250+
stepCount++;
151251
}
152252

153253

@@ -254,4 +354,8 @@ HAS 95
254354
HAS 50
255355
HAS 71
256356
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

Comments
 (0)