aboutsummaryrefslogtreecommitdiff
path: root/symtable.c
blob: 1358b5e87df24fbd8112e01932ce9dc0a5e0536c (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
/* Copyright (C) 2025 Aryadev Chavali

 * This program 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 Unlicense for details.

 * You may distribute and modify this code under the terms of the Unlicense,
 * which you should have received a copy of along with this program.  If not,
 * please go to <https://unlicense.org/>.

 * Created: 2025-08-19
 * Description: Symbol Table implementation
 */

#include "./alisp.h"

#include <malloc.h>
#include <string.h>

u64 djb2(sv_t string)
{
  u64 hash = 5381;
  for (u64 i = 0; i < string.size; ++i)
    hash = string.data[i] + (hash + (hash << 5));
  return hash;
}

void sym_table_init(sym_table_t *table)
{
  table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE);
  table->count    = 0;
  ivec_make((void **)&table->entries,
            table->capacity * sizeof(*table->entries));
}

sv_t *sym_table_find(sym_table_t *table, sv_t sv)
{
  // WIP: Deal with resizing this when table->count > table->size / 2
  u64 index = djb2(sv) & (table->capacity - 1);

  for (sv_t comp  = table->entries[index]; comp.data; index += 1,
            index = index & (table->capacity - 1), comp = table->entries[index])
    // Is it present in the table?
    if (sv.size == comp.size && strncmp(sv.data, comp.data, sv.size) == 0)
      break;

  // we couldn't find it in our linear search (worst case scenario)
  if (!table->entries[index].data)
  {
    sv_t newsv            = sv_copy(sv);
    table->entries[index] = newsv;
    ++table->count;
  }

  return table->entries + index;
}

void sym_table_cleanup(sym_table_t *table)
{
  // kill the data
  for (u64 i = 0; i < table->capacity; ++i)
    if (table->entries[i].data)
      free(table->entries[i].data);
  // kill the container
  ivec_free((void **)&table->entries);
  memset(table, 0, sizeof(*table));
}