language

Some fools attempt at an interpreted language
Log | Files | Refs

ht.c (2778B)


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
#define _XOPEN_SOURCE 500

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

#include "ht.h"
#include "var.h"
#include "helper.h"

ht_t* ht_init(int size)
{
	ASSERT((size % 2) == 0, "Hashtable size must be powers of 2\n");

	ht_t* hashtable = (ht_t*)malloc(sizeof(ht_t));
	M_ASSERT(hashtable);

	hashtable->size = size;
	hashtable->table = ht_init_table(hashtable->size);

	return hashtable;
}

ht_entry** ht_init_table(int size)
{
	ASSERT((size % 2 ) == 0, "hashtable size must be powers of 2\n");

	ht_entry** table = (ht_entry**)malloc(sizeof(ht_entry*)*size);
	M_ASSERT(table);

	for (int i = 0; i < size; i++)
	{
		table[i] = NULL;
	}

	return table;
}

void ht_destroy(ht_t* hashtable)
{
	ht_destroy_table(hashtable->table, hashtable->size);
}

void ht_destroy_table(ht_entry** table, int size)
{
	for (int i = 0; i < size; i++)
	{
		if (table[i] != NULL){
			ht_destroy_entry(table[i]);
		}
	}
}

void ht_destroy_entry(ht_entry* entry)
{
	var_del(entry->value);

	if (entry->key != NULL)
		free(entry->key);

	if (entry->next != NULL)
		ht_destroy_entry(entry->next);

	free(entry);
}

void ht_set(ht_t* hashtable, char* key, var_cont* value)
{
	int bucket = 0;
	ht_entry* np = NULL;
	ht_entry* next = NULL;
	ht_entry* last = NULL;

	bucket = ht_hash(hashtable, key);

	next = hashtable->table[ bucket ];

	/* Get relavent key/value pair if it exists in the bucket */
	while (next != NULL && next->key != NULL && strcmp(key, next->key) > 0)
	{
		last = next;
		next = next->next;
	}

	if (next != NULL && next->key != NULL && strcmp(key, next->key) == 0)
	{
		var_del(next->value);
		next->value = value;
	}
	else
	{
		np = ht_newpair(key, value);

		if (next == hashtable->table[bucket])
		{
			np->next = next;
			hashtable->table[bucket] = np;
		}
		else if (next == NULL)
		{
			last->next = np;
		}
		else
		{
			np->next = next;
			last->next = np;
		}
	}
}

var_cont* ht_get(ht_t* hashtable, char* key)
{
	int bucket = ht_hash(hashtable, key);

	ht_entry* pair = hashtable->table[bucket];
	while (pair != NULL && pair->key != NULL && strcmp(key, pair->key) > 0)
	{
		pair = pair->next;
	}

	if (pair == NULL || pair->key == NULL || strcmp(key, pair->key) != 0)
	{
		return var_new(0);
	}
	else
	{
		return pair->value;
	}
}

ht_entry* ht_newpair(char* key, var_cont* value)
{
	ht_entry* newpair;

	newpair = malloc(sizeof(ht_entry));
	M_ASSERT(newpair);

	newpair->key = (char*)strdup(key);
	M_ASSERT(newpair->key);

	newpair->value = value;

	return newpair;
}

int ht_hash(ht_t* hashtable, char* key)
{
	unsigned long int hashval;
	int i = 0;

	/* Convert our string to an integer */
	while (hashval < ULONG_MAX && i < strlen(key))
	{
		hashval = hashval << 8;
		hashval += key[ i ];
		i++;
	}

	return hashval % hashtable->size;
}