Add license
[knight_shuffling_tower.git] / hashmap.c
blob630668025583c3bd6240f23aa13bd277d2eb9716
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "hashmap.h"
6 struct hashlevel;
8 typedef struct hashlevel {
9 ast_var *data;
10 struct hashlevel *sublevels;
11 } hashlevel;
13 static hashlevel *root;
15 void hashmap_initialize()
17 ast_var *temp;
19 root = calloc(1, sizeof (hashlevel));
20 temp = find_var("one");
21 temp->type = KNIGHT_REF;
22 temp->val.knight_ref = 1;
24 temp = find_var("two");
25 temp->type = KNIGHT_REF;
26 temp->val.knight_ref = 2;
28 temp = find_var("three");
29 temp->type = KNIGHT_REF;
30 temp->val.knight_ref = 3;
32 temp = find_var("four");
33 temp->type = KNIGHT_REF;
34 temp->val.knight_ref = 4;
36 temp = find_var("five");
37 temp->type = KNIGHT_REF;
38 temp->val.knight_ref = 5;
40 temp = find_var("six");
41 temp->type = KNIGHT_REF;
42 temp->val.knight_ref = 6;
44 temp = find_var("seven");
45 temp->type = KNIGHT_REF;
46 temp->val.knight_ref = 7;
48 temp = find_var("eight");
49 temp->type = KNIGHT_REF;
50 temp->val.knight_ref = 8;
52 temp = find_var("nine");
53 temp->type = KNIGHT_REF;
54 temp->val.knight_ref = 9;
58 static int index(const char c)
60 int i = c;
62 if (c >= '0')
63 i -= '0';
64 if (c > 'a')
65 i -= ('a' - '0');
66 return i;
69 ast_var *find_var(const char *key)
71 const char *c_key = key;
72 hashlevel *c_hash = root;
73 int c_idx;
75 if (!key)
76 return NULL;
78 while (*c_key != '\0') {
79 c_idx = index(*c_key);
80 if (!c_hash->sublevels)
81 c_hash->sublevels = calloc(37, sizeof (hashlevel));
82 c_hash = &(c_hash->sublevels[c_idx]);
83 c_key++;
86 if (!c_hash->data)
87 c_hash->data = calloc(1, sizeof (ast_var));
89 return c_hash->data;