aboutsummaryrefslogtreecommitdiff
path: root/src/hash.c
blob: edb313922e2584525c002a63bdadc031a4933b64 (plain) (blame)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * =====================================================================================
 *
 *       Filename:  hash.c
 *
 *    Description:  Implementation of a basic hashmap 
 *
 *        Version:  1.0
 *        Created:  12/09/2022 07:24:38 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <math.h>
#include "hash.h"
#include "clog.h"


/*-----------------------------------------------------------------------------
 * We're defining the structs inside the .c file because really no one should be
 * messing around with the internals except through the exported functions
 *-----------------------------------------------------------------------------*/

typedef struct _map_element {
    int key;
    int is_used;
    any_type value;
} map_element;

typedef struct _map {
    int max_size;
    int cur_size;
    map_element* data;
} map;


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  map_init
 *  Description:  Initializes a new hashmap
 * =====================================================================================
 */
hash_map map_init () {
    map* m = (map*) malloc(sizeof(map));

    m->data = (map_element*) calloc(512, sizeof(map_element));

    m->max_size = 512;
    m->cur_size = 0;
    return m;
}		/* -----  end of function map_init  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  hash_int
 *  Description:  
 * =====================================================================================
 */
unsigned int hash_int (map* ma, unsigned int key) {

    /*-----------------------------------------------------------------------------
     * This is a pretty basic hashing function, basically we're still using
     * modulo but in such a way that collisions are less likely than if we just
     * used it raw.
     *
     * The advantage of this is that it doesn't matter the max size of the
     * hashmap, it doesn't have to be sufficiently removed from a power of 2
     *
     * See: Hashing by Multiplication here: https://en.wikipedia.org/wiki/Hash_table#Hash_function
     *-----------------------------------------------------------------------------*/
    return fmodf(floor(ma->max_size * (key * fmodf(0.3, 1.0))), ma->max_size); 
}		/* -----  end of function hash_int  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  hash_get_idx
 *  Description:  
 * =====================================================================================
 */
int hash_get_idx (hash_map* m, int key) {
    int c;
    map* ma = (map*) m;

    if (ma->cur_size == ma->max_size) {
        // Hash map is full, we need to exit now
        return -2;
    }

    c = hash_int(ma, key);                      /* This is the theoretically ideal hash index */

    /*-----------------------------------------------------------------------------
     * Pretty basic collision resolution. Needs to be able to resolve them
     *-----------------------------------------------------------------------------*/

    for(int i = 0; i < ma->max_size; i++) {
        if (ma->data[c].is_used == 0) {
            /*-----------------------------------------------------------------------------
             * This index isn't being used, we can return it 
             *-----------------------------------------------------------------------------*/
            return c;
        }

        if (ma->data[c].key == key && ma->data[c].is_used == 1) {

            /*-----------------------------------------------------------------------------
             * This index is being used, but by this key, so we can return it 
             *-----------------------------------------------------------------------------*/
            return c;
        }


        /*-----------------------------------------------------------------------------
         * This index is being used but not by this key. Increment the index
         * modulo the map size and try again
         *-----------------------------------------------------------------------------*/
        c = (c + 1) % ma->max_size;
    }
    return -2;
}		/* -----  end of function hash_get_idx  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  map_resize
 *  Description:  
 * =====================================================================================
 */
int map_resize(hash_map in) {
    int i;
    int old_size;
    map_element* cur;

    /*  New elements! */
    map *m = (map*) in;
    map_element* tmp = (map_element*) calloc(2 * m->max_size, sizeof(map_element));
    if (!tmp) return 4;                         /* We're out of memory! */

    cur = m->data;
    m->data = tmp;

    old_size = m->max_size;
    m->max_size = 2 * old_size;
    m->cur_size = 0;

    /*  Rehash */
    for (i = 0; i < old_size; i++) {
        int status = map_add(m, cur[i].key, cur[i].value);
        if (status != 0) {
            return status;
        }
    }

    free(cur);
    return 0;
}		/* -----  end of function map_resize  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  map_add
 *  Description:  
 * =====================================================================================
 */
int map_add(hash_map m, int key, any_type val) {
    map* ma = (map*) m; 

    int h = hash_get_idx(m, key);

    while (h == -2) {
        if (map_resize(m) == 4) {
            return 4;
        }
        h = hash_get_idx(m, key);
    }

    ma->data[h].value = val;
    ma->data[h].key = key;
    ma->data[h].is_used = 1;
    return 0;
}		/* -----  end of function map_add  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  map_get
 *  Description:  
 * =====================================================================================
 */
int map_get (hash_map m, int key, any_type* val) {
    map* ma = (map*) m;

    int h = hash_get_idx(m, key);

    if (ma->data[h].is_used == 1 && ma->data[h].key == key) {
        *val = ma->data[h].value;
        return 0;
    }
    return -3;
}		/* -----  end of function map_get  ----- */


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  map_del
 *  Description:  
 * =====================================================================================
 */
int map_del (hash_map m, int key) {
  map* ma = (map*) m;

  int h = hash_get_idx(m, key);

  if (ma->data[h].is_used == 1 && ma->data[h].key == key) {
    ma->data[h].is_used = 0;
    ma->data[h].key = 0;
  }

  return 0;
}		/* -----  end of function map_del  ----- */

/*
 *map_free cleans up the memory used by a hashmap
 */
void map_free(hash_map m) {
  map* ma = (map*) m;

  free(ma->data);
  free(ma);
}

/*
 *map_len returns the current length of the hashmap
 */
int map_len(hash_map m) {
  map* ma = (map*) m;

  return ma->cur_size;
}