summaryrefslogtreecommitdiff
path: root/src/02/04/hash.c
blob: f7cc5ec3e0c34b138cd96d86c63f3167d218cf19 (plain)
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
#include "hash.h"
#include "tuple.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
 * Initialize a new hash table.
 *
 * @param size The number of buckets to allocate for the chash table.
 * @return Returns the hash table.
 */
Hash *hash_init(int size) {
  Hash *hash = malloc(sizeof(Hash));
  hash->size = size;
  hash->buckets = calloc(size, sizeof(Node));
  return hash;
}

/**
 * Computes a hash code for the given key.
 *
 * @param hash The hash table that the key is used in.
 * @param key The key to produce a hash code for
 * @return Returns a hash code for the given key.
 */
unsigned int hash_code(Hash *hash, int key) { return key % hash->size; }

/**
 * A function to search for a specific key in a linked list.
 * This performs a O(n) search to find the matching key.
 * A possible optimization would be to convert this to do a binary
 * search for the given key using a binary search tree instead of a linked
 * list.
 *
 * @param list The linked list to search for the given key
 * @param key The key to search for in the linked list
 * @return Returns the data associated with the given key.
 */
void *search(Node *list, int key) {
  Node *current = list;

  while (current) {
    Tuple *t = current->data;
    if (t->key == key)
      return t->value;
    current = current->next;
  }

  return NULL;
}

/**
 * A function to fetch a specify value from a hash table by key.
 *
 * @param hash The hash table to search.
 * @param key The key associated with the value to return
 * @return Returns the data associated with the key or NULL if the key is not
 * found.
 */
void *hash_get(Hash *hash, int key) {
  unsigned int bucket = hash_code(hash, key);
  Node *n = hash->buckets + bucket;
  return (n->data) ? search(n, key) : NULL;
}

/**
 * A function to set the value associated with a key
 * in a hash table.
 *
 * @param hash The hash table to insert the key/value pair into
 * @param key The key associated with the value to insert.
 * @param value The value associated with the key to insert.
 */
void hash_set(Hash *hash, int key, void *value) {
  unsigned int bucket = hash_code(hash, key);
  Tuple *tuple = tuple_initialize(key, value);
  Node *n = hash->buckets + bucket;

  if (n->data)
    list_add(n, tuple);
  else
    hash->buckets[bucket] = *list_initialize(tuple);
}

/**
 * A function that pretty prints a key/value pair.
 *
 * @param data The Tuple to print.
 */
void print_tuple(void *data) {
  Tuple *t = data;

  if (t)
    printf("(%d:%d)", t->key, t->value);
  else
    printf("(nil)");
}

/**
 * Prints a visual representation of a hash table.
 *
 * @param hash The hash table to inspect
 */
void hash_inspect(Hash *hash) {
  for (int i = 0; i < hash->size; i++) {
    printf("%2d: ", i);
    list_inspect(hash->buckets + i, print_tuple);
  }
}