-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.h
More file actions
executable file
·83 lines (66 loc) · 2.8 KB
/
hash.h
File metadata and controls
executable file
·83 lines (66 loc) · 2.8 KB
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
#ifndef _HASH_H
#define _HASH_H
// ECE466 - Hash Table for Symbol Tables for a Parser for a C Compiler
// Miraj Patel, Hash Table implementation taken from DSA2 (ECE 165)
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
// Each item in the hash table contains:
// key - a string used as a key.
// isOccupied - if false, this entry is empty,
// and the other fields are meaningless.
// isDeleted - if true, this item has been lazily deleted.
// pv - a pointer related to the key;
// NULL if no pointer was provided to insert.
struct hashItem {
int isOccupied;
int isDeleted;
char *key;
void *pv;
};
struct hashTable {
unsigned int capacity; // The current capacity of the hash table.
unsigned int filled; // Number of occupied items in the table.
struct hashItem *data; // The actual entries are here. (The symbols/identifiers associated with this scope
};
struct hashTable *new_hashTable(int size);
// Insert the specified key into the hash table.
// If an optional pointer is provided,
// associate that pointer with the key.
// Returns 0 on success,
// 1 if key already exists in hash table,
// 2 if hash table is full.
int insert(struct hashTable *theTable, char *key, void *pv);
// Check if the specified key is in the hash table.
// If so, return true (1); otherwise, return false (0).
int contains(struct hashTable *theTable, char *key);
// Get the pointer associated with the specified key.
// If the key does not exist in the hash table, return NULL.
// If an optional pointer to a bool is provided,
// set the bool to true if the key is in the hash table,
// and set the bool to false otherwise.
void *getPointer(struct hashTable *theTable, char *key, int *b);
// Set the pointer associated with the specified key.
// Returns 0 on success,
// 1 if the key does not exist in the hash table.
int setPointer(struct hashTable *theTable, char *key, void *pv);
// Delete the item with the specified key.
// Returns true (1) on success,
// false (0) if the specified key is not in the hash table.
int removeItem(struct hashTable *theTable, char *key);
// The hash function.
int hash(struct hashTable *theTable, char *key);
// Search for an item with the specified key.
// Return the position if found, -1 otherwise.
int findPos(struct hashTable *theTable, char *key);
// The rehash function; makes the hash table bigger.
// Returns true (1) on success, false (0) if memory allocation fails.
// int rehash(struct hashTable *theTable);
// Deciding not to implement
// Return a prime number at least as large as size.
// Uses a precomputed sequence of selected prime numbers.
static unsigned int getPrime(int size);
void printTable(struct hashTable *theTable);
#endif //_HASH_H