summaryrefslogtreecommitdiff
path: root/python/hash.c
blob: e5e9811b8919ac705c28c19f8152684a70dd6c7c (plain)
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
/** \ingroup python
 * \file python/hash.c
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include "hash.h"

#define CHUNK 1

struct filePath {
    char * dir;
    char * base;
} ;

struct bucket {
    struct filePath * data;
    int allocated;
    int firstFree; /* as in data[firstFree] */
};

struct hash_table {
    int size;
    int entries;
    int overHead;
    struct bucket *bucket;
};

struct hash_table *htNewTable(int size)
{
    struct hash_table *res;
    int i = 0;

    res = malloc(sizeof(struct hash_table));
    res->bucket = malloc(sizeof(struct bucket) * size);
    res->size = size;
    res->entries = 0;
    res->overHead = sizeof(struct bucket) * size + CHUNK * sizeof(char *);

    while (i < size) {
	res->bucket[i].data = malloc(CHUNK * sizeof(*res->bucket[i].data));
	res->bucket[i].allocated = CHUNK;
	res->bucket[i].firstFree = 0;
	i++;
    }
    
    return res;
}

void htFreeHashTable(struct hash_table *ht)
{
    struct bucket * b;
    int item;

    b = ht->bucket;
    while (ht->size--) {
	for (item = 0; item < b->firstFree; item++) {
	    free(b->data[item].dir);
	    free(b->data[item].base);
	}
	free(b->data);
	b++;
    }
    free(ht->bucket);
    free(ht);
}

void htHashStats(struct hash_table *t)
{
    int i = 0;
    int empty = 0;

    while (i < t->size) {
	if (t->bucket[i].firstFree != 0) {
	    /*printf("Bucket %d used %d\n", i, t->bucket[i].firstFree);*/
	} else {
	    empty++;
	}
	i++;
    }

    printf("Total Buckets : %d\n", t->size);
    printf("Empty Buckets : %d\n", empty);
    printf("Total Entries : %d\n", t->entries);
    printf("Total Overhead: %d\n", t->overHead);
    printf("Avergage Depth: %f\n", (double)t->entries / (double)t->size);
}

static unsigned int htHashStrings(const char *s, const char *t)
{
    unsigned int res = 0;

    while (*s)
	res = ((res<<1) + (int)(*(s++)));
    while (*t)
	res = ((res<<1) + (int)(*(t++)));

    return res;
}

/* returns bucket # containing item, or -1 */
static int in_table_aux(struct hash_table *t, int hash, const char * dir, 
			const char * base)
{
    int x;

    x = 0;
    while (x < t->bucket[hash].firstFree) {
	if (! strcmp(t->bucket[hash].data[x].dir, dir) &&
	    ! strcmp(t->bucket[hash].data[x].base, base)) {
	    return x;
	}
	x++;
    }
    
    return -1;
}

int htInTable(struct hash_table *t,  const char * dir, const char * base)
{
    int hash;

    hash = htHashStrings(dir, base) % t->size;

    if (in_table_aux(t, hash, dir, base) == -1)
	return 0;
    return 1;
}

void htAddToTable(struct hash_table *t, const char * dir, const char * base)
{
    static int hash = 1;

    if (!dir || !base)
	return;
    
    hash = htHashStrings(dir, base) % t->size;
    if (in_table_aux(t, hash, dir, base) != -1)
	return;

    if (t->bucket[hash].firstFree == t->bucket[hash].allocated) {
	t->bucket[hash].allocated += CHUNK;
	t->bucket[hash].data =
	    realloc(t->bucket[hash].data,
		    t->bucket[hash].allocated * sizeof(*(t->bucket->data)));
	/*printf("Bucket %d grew to %d\n", hash, t->bucket[hash].allocated);*/
	t->overHead += sizeof(char *) * CHUNK;
    }
    /*printf("In bucket %d, item %d\n", hash, t->bucket[hash].firstFree);*/
    t->bucket[hash].data[t->bucket[hash].firstFree].dir = strdup(dir);
    t->bucket[hash].data[t->bucket[hash].firstFree++].base = strdup(base);
    t->entries++;
}

void htRemoveFromTable(struct hash_table *t, const char * dir, 
		       const char * base) {
    int hash;
    int item;
    int last;

    hash = htHashStrings(dir, base) % t->size;
    if ((item = in_table_aux(t, hash, dir, base)) == -1) {
	return;
    }

    free(t->bucket[hash].data[item].dir);
    free(t->bucket[hash].data[item].base);

    last = --t->bucket[hash].firstFree;
    t->bucket[hash].data[item] = t->bucket[hash].data[last];
}

int htNumEntries(struct hash_table *t) {
    return t->entries;
}

void htIterStart(htIterator * iter) {
    iter->bucket = 0;
    iter->item = -1;
}

int htIterGetNext(struct hash_table * t, htIterator * iter, 
		  const char ** dir, const char ** base) {
    iter->item++;
    
    while (iter->bucket < t->size) {
	if (iter->item < t->bucket[iter->bucket].firstFree) {
	    *dir = t->bucket[iter->bucket].data[iter->item].dir;
	    *base = t->bucket[iter->bucket].data[iter->item].base;

	    return 1;
	}

	iter->item++;
	if (iter->item >= t->bucket[iter->bucket].firstFree) {
	    iter->bucket++;
	    iter->item = 0;
	}
    }

    return 0;
}