about summary refs log tree commit diff
path: root/src/hashmap.c
blob: 5834802f892dd902e08e22f8ee914871a797cdb2 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "types.h"
#define TABLE_SIZE 10007  // Use a prime number for better distribution

typedef struct HashNode {

  uint64_t         key;
  struct HashNode *next;

} HashNode;

typedef struct HashMap {

  HashNode **table;

} HashMap;

static HashMap *_hashmap;

void hashmap_reset() {

  if (unlikely(!_hashmap)) {

    _hashmap = (HashMap *)malloc(sizeof(HashMap));
    _hashmap->table = (HashNode **)malloc(sizeof(HashNode *) * TABLE_SIZE);
    memset((char *)_hashmap->table, 0, sizeof(HashNode *) * TABLE_SIZE);

  } else {

    for (int i = 0; i < TABLE_SIZE; i++) {

      HashNode *node = _hashmap->table[i];
      while (node) {

        HashNode *temp = node;
        node = node->next;
        free(temp);

      }

    }

    memset((char *)_hashmap->table, 0, sizeof(HashNode *) * TABLE_SIZE);

  }

}

static inline unsigned int hash(uint64_t key) {

  return key % TABLE_SIZE;

}

// type must be below 8
bool hashmap_search_and_add(uint8_t type, uint64_t key) {

  if (unlikely(type >= 8)) return false;
  uint64_t     val = (key & 0xf8ffffffffffffff) + ((uint64_t)type << 56);
  unsigned int index = hash(val);
  HashNode    *node = _hashmap->table[index];
  while (node) {

    if (node->key == val) return true;
    node = node->next;

  }

  // not found so add it
  node = (HashNode *)malloc(sizeof(HashNode));
  node->key = val;
  node->next = _hashmap->table[index];
  _hashmap->table[index] = node;

  return false;

}

// type must be below 8
bool hashmap_search_and_add_ptr(uint8_t type, u8 *key) {

  if (unlikely(type >= 8)) return false;
  uint64_t key_t = 0;
  memcpy(((char *)key_t) + (7 - type), key, type + 1);
  return hashmap_search_and_add(type, key_t);

}

/* below is not used */

void hashmap_insert(uint64_t key) {

  unsigned int index = hash(key);
  HashNode    *node = (HashNode *)malloc(sizeof(HashNode));
  node->key = key;
  node->next = _hashmap->table[index];
  _hashmap->table[index] = node;

}

bool hashmap_search(uint64_t key) {

  unsigned int index = hash(key);
  HashNode    *node = _hashmap->table[index];
  while (node) {

    if (node->key == key) return true;
    node = node->next;

  }

  return false;

}

void delete(uint64_t key) {

  unsigned int index = hash(key);
  HashNode    *prev = NULL, *node = _hashmap->table[index];
  while (node) {

    if (node->key == key) {

      if (prev)
        prev->next = node->next;
      else
        _hashmap->table[index] = node->next;
      free(node);
      return;

    }

    prev = node;
    node = node->next;

  }

}

void freeHashMap(HashMap *map) {

  free(_hashmap->table);
  free(map);

}