-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathlib_hash.c
103 lines (86 loc) · 1.49 KB
/
lib_hash.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
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "lib_hash.h"
int is_prime(int num)
{
int i;
assert(num >= 0);
if(num == 1 || num == 0) return 0;
for(i = 2 ; i <= num / 2; i++)
if(num % i == 0) return 0;
return 1;
}
int next_twinprime(int num)
{
assert(num >= 2);
while(num < INT_MAX) {
num++;
if(is_prime(num) && is_prime(num-2))return num;
}
return -1;
}
Hash hash_new(int len)
{
Hash h;
assert(len >= 2);
h.size = len;
h.data = malloc(sizeof(int)*len);
return h;
}
Hash hash_new_prime(int len)
{
Hash h;
assert(len >= 2);
len = next_twinprime(len);
h.size = len;
h.data = malloc(sizeof(int)*len);
return h;
}
int hash_data(void *data, int num, size_t size)
{
int slot = 0;
while(num >= 0)
{
/* slot += (void *) (data) + (num * size); */
slot += (int)data + size * num;
num--;
}
return slot;
}
int hash_insert(Hash h, void * data, size_t length)
{
int slot;
assert(data != NULL);
/*slot = hash_data(data, length);
if(h[slot]->data == NULL) *//* insert data in first node */;
return 0;
}
void * hash_search(Hash h, void * data, size_t length)
{
return NULL;
}
int hash_remove(Hash h, void * data, size_t length)
{
assert(data != NULL);
return 0;
}
int hash_empty(Hash h)
{
return 0;
}
int hash_full(Hash h)
{
return 0;
}
void hash_clear(Hash h)
{
}
void hash_print(Hash h)
{
}
void hash_delete(Hash h)
{
free(&h);
}