aboutsummaryrefslogtreecommitdiff
path: root/lib/algolib.c
blob: 8d15782d0acc324a257f55ac2354c64a1ca437b8 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#ifndef algolib
#define algolib

#include <gobjects.c>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <vectorlib.c>

#define flaot float

// Binary Search Tree

typedef struct BS_Node {
    int value;
    struct BS_Node* left;
    struct BS_Node* right;
    Point pos;
    TEXT* text;
} BS_Node;

void BSAddNode(BS_Node** self, BS_Node** node)
{
    if ((*node)->value < (*self)->value) {
        if ((*self)->left == NULL) {
            (*self)->left = (*node);
        } else {
            BSAddNode(&(*self)->left, node);
        }
    } else if ((*node)->value > (*self)->value) {
        if ((*self)->right == NULL) {
            (*self)->right = (*node);
        } else {
            BSAddNode(&(*self)->right, node);
        }
    } else {
        free(*node);
    }
}

void BSAddValue(BS_Node** root, int value)
{
    BS_Node* node = (BS_Node*)malloc(sizeof(BS_Node));
    node->value = value;
    node->left = NULL;
    node->right = NULL;

    if ((*root) == NULL) {
        (*root) = node;
    } else {
        BSAddNode(root, &node);
    }
}

void BSPrintTree(BS_Node** node)
{
    if ((*node)->left != NULL) {
        BSPrintTree(&(*node)->left);
    }
    printf("%d\n", (*node)->value);
    if ((*node)->right != NULL) {
        BSPrintTree(&(*node)->right);
    }
}

BS_Node* BSSearchTree(BS_Node** node, int val)
{
    if ((*node)->value == val) {
        return *node;
    } else if (val < (*node)->value && (*node)->left != NULL) {
        return BSSearchTree(&(*node)->left, val);
    } else if (val > (*node)->value && (*node)->right != NULL) {
        return BSSearchTree(&(*node)->right, val);
    }
    return NULL;
}

void BSFreeTree(BS_Node** node)
{
    if ((*node)->left != NULL) {
        BSFreeTree(&(*node)->left);
    }
    if ((*node)->right != NULL) {
        BSFreeTree(&(*node)->right);
    }
    free(*node);
}

// Breadth-First Search
#define MAX_KEY_LENGTH 2048

typedef struct BFS_Node {
    char* value;
    struct BFS_Node** edges;
    bool searched;
    struct BFS_Node* parent;
} BFS_Node;

typedef struct BFS_Graph_KeyPair {
    char key[MAX_KEY_LENGTH];
    BFS_Node* node;
} BFS_Graph_KeyPair;

typedef struct BFS_Graph {
    BFS_Node** nodes;
    BFS_Graph_KeyPair** graph;
    BFS_Node* start;
    BFS_Node* end;
} BFS_Graph;

size_t hash(char* str)
{
    size_t hash = 5381;
    int c;

    while ((c = *str++))
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash % 200;
}

BFS_Node* BFS_Constructor(char* value)
{
    BFS_Node* node = (BFS_Node*)malloc(sizeof(BFS_Node));
    node->value = value;
    node->parent = NULL;
    node->edges = (BFS_Node**)malloc(sizeof(BFS_Node*) * 200);
    for (int i = 0; i < 200; i++) {
        node->edges[i] = NULL;
    }
    return node;
}

void BFS_AddEdge(BFS_Node* node, BFS_Node* neighbor)
{
    for (int i = 0; i < 200; i++) {
        if (node->edges[i] == NULL) {
            node->edges[i] = neighbor;
            break;
        }
    }
    for (int i = 0; i < 200; i++) {
        if (neighbor->edges[i] == NULL) {
            neighbor->edges[i] = node;
            return;
        }
    }
}

BFS_Graph* BFS_Graph_Constructor()
{
    BFS_Graph* graph = (BFS_Graph*)malloc(sizeof(BFS_Graph));
    graph->nodes = (BFS_Node**)malloc(sizeof(BFS_Node*) * 200);
    graph->graph = (BFS_Graph_KeyPair**)malloc(sizeof(BFS_Graph_KeyPair*) * 200);
    for (int i = 0; i < 200; i++) {
        graph->graph[i] = (BFS_Graph_KeyPair*)malloc(sizeof(BFS_Graph_KeyPair));
        graph->graph[i]->node = NULL;
        graph->nodes[i] = NULL;
    }
    return graph;
}

BFS_Node* BFS_Gr_GetNode(BFS_Graph* graph, char* value)
{
    size_t index = hash(value);
    if (graph->graph[index]->node != NULL && strcmp(graph->graph[index]->node->value, value) == 0)
        return graph->graph[index]->node;
    return NULL;
}

void BFS_Gr_AddNode(BFS_Graph* graph, BFS_Node* node)
{
    if (node == NULL)
        return;

    for (int i = 0; i < 200; i++) {
        if (graph->nodes[i] == NULL) {
            graph->nodes[i] = node;
            char* title = node->value;

            size_t index = hash(title);

            // dont wanna handle collisions
            if (graph->graph[index]->node != NULL) {
                if (strcmp(graph->graph[index]->node->value, title) == 0) {
                    return;
                } else {
                    printf("COLLISION!");
                    exit(-1);
                }
            }

            strncpy(graph->graph[index]->key, title, MAX_KEY_LENGTH - 1);
            graph->graph[index]->key[MAX_KEY_LENGTH - 1] = '\0'; // Ensure null-terminated string
            graph->graph[index]->node = node;

            return;
        }
    }
}

BFS_Node* BFS_Gr_SetStart(BFS_Graph* graph, char* value)
{
    size_t index = hash(value);
    graph->start=graph->graph[index]->node;
    return graph->start;
}

BFS_Node* BFS_Gr_SetEnd(BFS_Graph* graph, char* value)
{
    size_t index = hash(value);
    graph->end=graph->graph[index]->node;
    return graph->end;
}

void BFS_FreeGraph(BFS_Graph* graph)
{
    for (int i = 0; i < 200; i++) {
        if (graph->graph[i] != NULL && graph->graph[i]->node != NULL) {
            free(graph->graph[i]->node->edges);
            graph->graph[i]->node->edges = NULL;
            free(graph->graph[i]->node);
            graph->graph[i]->node = NULL;
        }
        if (graph->graph[i] != NULL) {
            free(graph->graph[i]);
        }
    }
    free(graph->nodes);
    free(graph->graph);
    free(graph);
}

#endif