-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.h
110 lines (93 loc) · 3.07 KB
/
hashmap.h
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
/*
* Copyright (c) 2020 Anamitra Ghorui
* This file is part of Calcium.
*
* Calcium is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calcium is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calcium. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file hashmap.h
* \author Anamitra Ghorui
* \brief Hashmap Implementation
*
* TODO: Implement entry relocation based on frequency of access
*/
/*
* From what implementations of hash maps I have seen, resizing is always an
* O(n) operation for any hash function that takes the mod of the result with
* the size of the table. I have not yet found evidence otherwise. (CLRS 17.4)
*
* A new mem block is allocated when the load factor is hit, and elements are
* inserted one by one from the previous table into this new, larger block.
*/
#ifndef CA_HASHMAP_H
#define CA_HASHMAP_H
#include "types.h"
#include "debug.h"
#include <string.h>
#include <stdio.h>
/// The maximum size of a hashtable key.
#define CA_HASH_KEY_SIZE 31
/// The number of elements in the hash data structure, corresponding to the
/// number of capital and small letters together in the English language, plus
/// underscore.
#define CA_HASH_SIZE 53
/// Defines a hashmap/hashtable node. This is a linked-list based hashmap
/// meant to store user variables.
typedef struct CaHashNode CaHashNode;
struct CaHashNode {
void *data;
CaHashNode *next;
CaType type;
CaHashKey key;
};
/// Defines a hashmap/hashtable
typedef CaHashNode **CaHash;
/**
* \brief Initialises a hash table.
*/
CaHash ca_hash_init();
/**
* \brief Frees a hash table.
*/
void ca_hash_free(CaHash map);
CaHashNode *ca_hash_node_init(CaHashKey key, CaSize size, CaType type,
void *data);
/// TODO document
void ca_hash_node_free(CaHashNode *h);
/**
* \brief Enters a key-value pair into a hash table.
* \param table The hash table.
* \param key The key to enter.
* \param value The pointer to the value the key must correspond to.
* \return An error code.
*/
CaError ca_hash_set(CaHash map, CaHashKey key, CaSize size, CaType type,
void *data);
/**
* \brief Get the value of a key from a hash table a hash table.
* \param table The hash table.
* \param key The key to enter.
* \param value The pointer to to the value that may be returned.
* \return An error code.
*/
CaError ca_hash_get(CaHash map, CaHashKey key, CaSize size,
CaHashNode **h);
/**
* \brief Prints all of the contents of the hash table to stdout.
* Meant for debugging purposes.
* \param table The hash table.
* \return An error code.
*/
CaError ca_hash_print(CaHash map);
#endif