-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyb_types.c
188 lines (173 loc) · 3.89 KB
/
cyb_types.c
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
/* -----------------------------------------------------------------------------
* The Cyberiada GraphML library implemention
*
* The basic data structures
*
* Copyright (C) 2024 Alexey Fedoseev <aleksey@fedoseev.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* ----------------------------------------------------------------------------- */
#include <string.h>
#include <stdlib.h>
#include "cyb_types.h"
int cyberiada_stack_push(CyberiadaStack** stack)
{
CyberiadaStack* new_item = (CyberiadaStack*)malloc(sizeof(CyberiadaStack));
memset(new_item, 0, sizeof(CyberiadaStack));
new_item->next = (*stack);
*stack = new_item;
return 0;
}
int cyberiada_stack_update_top_key(CyberiadaStack** stack, const char* new_key)
{
if (!stack || !*stack) {
return -1;
}
(*stack)->key = (void*)new_key;
return 0;
}
int cyberiada_stack_update_top_data(CyberiadaStack** stack, void* new_data)
{
if (!stack || !*stack) {
return -1;
}
(*stack)->data = new_data;
return 0;
}
void* cyberiada_stack_get_top_data(CyberiadaStack** stack)
{
CyberiadaStack* s;
if (!stack || !*stack) {
return NULL;
}
s = *stack;
while (s) {
if (s->data)
return s->data;
s = s->next;
}
return NULL;
}
int cyberiada_stack_pop(CyberiadaStack** stack)
{
CyberiadaStack* to_pop;
if (!stack || !*stack) {
return -1;
}
to_pop = *stack;
*stack = to_pop->next;
free(to_pop);
return 0;
}
int cyberiada_stack_is_empty(CyberiadaStack** stack)
{
return !stack || !*stack;
}
int cyberiada_stack_free(CyberiadaStack** stack)
{
while (!cyberiada_stack_is_empty(stack)) {
cyberiada_stack_pop(stack);
}
return 0;
}
int cyberiada_list_add(CyberiadaList** list, const char* key, void* data)
{
CyberiadaList *item, *new_item;
if (!list) {
return -1;
}
new_item = (CyberiadaList*)malloc(sizeof(CyberiadaList));
new_item->key = (void*)key;
new_item->data = data;
new_item->next = NULL;
if (!*list) {
*list = new_item;
} else {
item = *list;
while (item->next) item = item->next;
item->next = new_item;
}
return 0;
}
void* cyberiada_list_find(CyberiadaList** list, const char* key)
{
CyberiadaList* item;
if (!list || !*list) {
return NULL;
}
item = *list;
while (item) {
if (item->key && strcmp((const char*)item->key, key) == 0) {
return item->data;
}
item = item->next;
}
return NULL;
}
int cyberiada_list_free(CyberiadaList** list)
{
CyberiadaList* item;
if (!list) {
return 0;
}
while(*list) {
item = *list;
*list = item->next;
free(item);
}
return 0;
}
int cyberiada_queue_add(CyberiadaQueue** queue, void* key, void* data)
{
CyberiadaQueue* new_item = (CyberiadaQueue*)malloc(sizeof(CyberiadaQueue));
memset(new_item, 0, sizeof(CyberiadaQueue));
new_item->key = key;
new_item->data = data;
new_item->next = (*queue);
*queue = new_item;
return 0;
}
int cyberiada_queue_get(CyberiadaQueue** queue, void** key, void** data)
{
CyberiadaQueue *q, *prev = NULL;
if (!queue || !key || !data) return 1;
q = *queue;
while (q->next) {
prev = q;
q = q->next;
}
*key = q->key;
*data = q->data;
free(q);
if (prev) {
prev->next = NULL;
} else {
*queue = NULL;
}
return 0;
}
int cyberiada_queue_free(CyberiadaQueue** queue)
{
CyberiadaQueue* item;
if (!queue) {
return 0;
}
while(*queue) {
item = *queue;
*queue = item->next;
free(item);
}
return 0;
}