Repository navigation
Expand file tree
/
Copy pathhash.c
More file actions
144 lines (129 loc) · 3.23 KB
/
Copy pathhash.c
File metadata and controls
144 lines (129 loc) · 3.23 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define m 2013
int kolizje = 0;
int numbers = 0;
int A[m] = { 0 };
struct osoba{
char nazwisko[25];
int ilosc;
};
int hashFunc1(int k){
return k % m;
}
int getSize(const char *filename){
int length = 0;
int k;
char tmp[25];
FILE *f = fopen(filename, "r");
do{
k = fscanf(f, "%s", tmp);
length++;
}while(k == 1);
fclose(f);
return length;
}
int hashFuncA(int k, int i){
return (hashFunc1(k) + i * i) % m;
}
void print(int k, int j){
printf("--> h(%d) = %d\n", k, j);
}
int insert(struct osoba *T[], int k, struct osoba* Ludz){
numbers++;
int i = 0;
int j;
do{
j = hashFuncA(k, i);
if(T[j] == (void*)0 || T[j] == (void*)-1){
//T[j] = k;
T[j] = Ludz;
// print(k, j);
return 1;
} else {
A[j]++;
kolizje++;
i++;
}
}while(i < m);
return 0;
}
void show(int T[]){
int i;
printf("\n====================================\n");
printf("======== T A B L E =============\n");
for(i = 0; i < m; i++){
printf("T[%i] \t==\t %i \n", i, T[i]);
}
printf("====================================\n");
}
void show_K(int T[]){
int i;
printf("\n====================================\n");
printf("======== T A B L E =============\n");
for(i = 0; i < m; i++){
if(T[i] > 0)
printf("T[%i] \t==\t %i \n", i, T[i]);
}
printf("====================================\n");
}
int to_int(const char * word);
void load(struct osoba T[], const char *filename){
int i;
int size = getSize(filename) - 1;
FILE *f = fopen(filename, "r");
printf("File size: %i entries.\n", size);
char tmp[30];
for(i = 0; i < size; i++){
fscanf(f, "%i", &T[i].ilosc);
fscanf(f, "%s", tmp);
strcpy(T[i].nazwisko, tmp);
}
puts("Loaded succesfully! \n");
}
int to_int(const char * word){
long int number = 0;
int length = strlen(word);
int i;
for(i = 0; i < length; i+=2){
number += ((int)word[i] * 11) ^ (int)word[i+1];
}
return number;
}
void zlicz(){
int i = 0;
int zera = 0;
int niezera = 0;
int iloscNiezer = 0;
int max = 0;
for(i = 0; i < m; i++){
if(A[i] == 0){
zera++;
}
if(A[i] > 0){
niezera += A[i];
iloscNiezer++;
}
max = ((A[i] > max) ? (A[i]) : (max));
}
printf("\n");
printf("Wielkosc tablicy: \t\t%d\n", m);
printf("Ilosc zer w tablicy T: \t\t%d \n", zera);
printf("Maksymalna ilosc kolizji: \t%d \n", max);
printf("Srednia ilosc kolizji: \t%lf \n", (double)niezera/(double)iloscNiezer);
}
int main(void){
int i;
struct osoba *J[m] = { 0 }; // tablica hashy
int k = getSize("nazwiskaASCII.txt");
struct osoba T[k];
load(T, "nazwiskaASCII.txt");
for(i = 0; i < 0.9*m; i++){
int liczba = to_int(T[i].nazwisko);
insert(J, liczba, &T[i]);
}
// show_K(A);
zlicz();
return 0;
}