-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_map.c
107 lines (97 loc) · 3.35 KB
/
hash_map.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
104
105
106
107
/**
* authors: Dillon O'Leary
* Ezra Boley
*/
#include "hash_map.h"
#include "parser.h"
#include "list_utils.h"
#include <string.h>
#include <stdlib.h>
void createMap(DAG_map * d_map, BuildSpecList * specs) {
int arr_size = (int)(specs->len / LOAD_FACTOR);
if (NULL == (d_map->map = calloc(arr_size, sizeof(BuildSpecNode*)))) {
fprintf(stderr, "Memory allocation error");
exit(-1);
}
//d_map->size = size;
d_map->size = arr_size;
d_map->root = specs->root;
}
// THIS IS NOT MY CODE! its from http://www.cse.yorku.ca/~oz/hash.html
// My TA said I could use it
unsigned long
hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
int getIndex(char * str, int size) {
return hash( (unsigned char*) str) % size;
}
void insertNode(DAG_map * map, BuildSpecNode * node) {
int hashIndex = getIndex(node->data->target, map->size);
while (map->map[hashIndex] != NULL) {
if (strcmp(node->data->target, map->map[hashIndex]->data->target) == 0) {
fprintf(stderr, "Error, same target twice\n");
exit(-1);
}
hashIndex = (hashIndex + 1) % map->size;
}
map->map[hashIndex] = node;
}
void populateMap(DAG_map * map, BuildSpecList* specs) {
BuildSpec* currSpec = specs->frstBS;
for(int i=0; i<specs->len; i++) {
BuildSpecNode * node = malloc(sizeof(BuildSpecNode));
node->data = currSpec;
node->tempMark = node->permMark = 0;
node->children = currSpec->deps;
node->hasExec = 0;
node->isDummy = 0;
insertNode(map, node);
currSpec = next_BS(currSpec);
}
}
void initHashMap(DAG_map * map, BuildSpecList* specs) {
createMap(map, specs);
populateMap(map, specs);
}
BuildSpecNode * createDummyNode(char* target) {
BuildSpecNode* node = calloc(1, sizeof(BuildSpecNode));
BuildSpec* spec = calloc(1, sizeof(BuildSpec));
spec->cmds = calloc(1, sizeof(CommandList));
spec->target = target;
spec->depsLen = 0;
spec->cmds->len = 0;
node->data = spec;
node->hasExec = 0;
node->isDummy = 1;
return node;
}
BuildSpecNode * lookup(DAG_map * map, char* target) {
int hashIndex = getIndex(target, map->size);
if (map->map[hashIndex] == NULL) {
//fprintf(stdout, "This target %s is not in the map! Assume it is a file\n", target);
return createDummyNode(target);
}
while (strcmp(target, map->map[hashIndex]->data->target) != 0) {
hashIndex = (hashIndex + 1) % map->size;
if (map->map[hashIndex] == NULL) {
// if an element is searched for that doesn't
// exit, instead of throwing an error it might be a
// file that isn't a build spec. In that case I need to
// return a dummy node if it exists, if not throw an
// error. Maybe I don't even need to check if it exists?
// That might be handled elsewhere. Couldn't hurt though.
// Also the dummy node probably needs some flag, but
// also it might not because it neither has dependencies nor
// commands
//fprintf(stdout, "This target %s is not in the map! Assume it is a file\n", target);
return createDummyNode(target);
}
}
return map->map[hashIndex];
}