forked from czertyaka/prosoft-c-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstack.c
More file actions
176 lines (146 loc) · 4.32 KB
/
cstack.c
File metadata and controls
176 lines (146 loc) · 4.32 KB
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
#include "cstack.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h> // äëÿ ðàáîòû ñ ïàìÿòüþ
#define UNUSED(VAR) (void)(VAR)
struct node {
struct node* prev;
unsigned int size;
char data[];
};
typedef struct node* stack_t;
struct stack_entry {
int used;
stack_t top;
unsigned int count;
};
static struct {
unsigned int capacity;
struct stack_entry* entries;
} g_table = { 0u, NULL };
static int ensure_capacity(unsigned int min_capacity) {
if (g_table.capacity >= min_capacity)
return 1;
unsigned int new_capacity = g_table.capacity ? g_table.capacity * 2u : 16u;
if (new_capacity < min_capacity)
new_capacity = min_capacity;
struct stack_entry* new_entries = (struct stack_entry*)realloc(g_table.entries, new_capacity * sizeof(struct stack_entry));
if (!new_entries)
return 0;
// Èíèöèàëèçàöèÿ íîâûõ ñëîòîâ
for (unsigned int i = g_table.capacity; i < new_capacity; ++i) {
new_entries[i].used = 0;
new_entries[i].top = NULL;
new_entries[i].count = 0u;
}
g_table.entries = new_entries;
g_table.capacity = new_capacity;
return 1;
}
hstack_t stack_new(void) {
// Íóæíî íàéòè ñâîáîäíûé ñëîò èëè ðàñøèðèòü òàáëèöó
if (!ensure_capacity(1u))
return -1;
for (unsigned int i = 0; i < g_table.capacity; ++i) {
if (!g_table.entries[i].used) {
g_table.entries[i].used = 1;
g_table.entries[i].top = NULL;
g_table.entries[i].count = 0u;
return (hstack_t)i;
}
}
// Ñâîáîäíûõ ñëîòîâ íå íàøëîñü, ðàñøèðÿåì è èñïîëüçóåì äàëüøå
if (!ensure_capacity(g_table.capacity + 1u))
return -1;
unsigned int idx = g_table.capacity - 1u;
g_table.entries[idx].used = 1;
g_table.entries[idx].top = NULL;
g_table.entries[idx].count = 0u;
return (hstack_t)idx;
}
void stack_free(const hstack_t hstack) {
if (hstack < 0)
return;
unsigned int idx = (unsigned int)hstack;
if (idx >= g_table.capacity)
return;
if (!g_table.entries[idx].used)
return;
// Îñâîáîæäåíèå âñåõ íîäîâ
stack_t cur = g_table.entries[idx].top;
while (cur) {
stack_t prev = cur->prev;
free(cur);
cur = prev;
}
g_table.entries[idx].used = 0;
g_table.entries[idx].top = NULL;
g_table.entries[idx].count = 0u;
}
int stack_valid_handler(const hstack_t hstack) {
// Ïðîâåðêà õýíäëåðà ñòåêà
if (hstack < 0)
return 1;
unsigned int idx = (unsigned int)hstack;
if (idx >= g_table.capacity)
return 1;
return g_table.entries[idx].used ? 0 : 1;
}
unsigned int stack_size(const hstack_t hstack) {
// Óçíàòü ðàçìåð ñòåêà ñ ïðîâåðêàìè
if (hstack < 0)
return 0u;
unsigned int idx = (unsigned int)hstack;
if (idx >= g_table.capacity)
return 0u;
if (!g_table.entries[idx].used)
return 0u;
return g_table.entries[idx].count;
}
void stack_push(const hstack_t hstack, const void* data_in, const unsigned int size) {
// Ïðîâåðêà âõîäíûõ äàííûõ
if (hstack < 0)
return;
if (!data_in)
return;
if (size == 0u)
return;
unsigned int idx = (unsigned int)hstack;
if (idx >= g_table.capacity)
return;
if (!g_table.entries[idx].used)
return;
// Âûäåëèòü íîä ñ ðàñøèðÿþùèìñÿ ìàññèâîì äëÿ äàííûõ
size_t alloc_size = sizeof(struct node) + (size_t)size;
struct node* n = (struct node*)malloc(alloc_size);
if (!n)
return;
n->prev = g_table.entries[idx].top;
n->size = size;
memcpy(n->data, data_in, size);
g_table.entries[idx].top = n;
g_table.entries[idx].count += 1u;
}
unsigned int stack_pop(const hstack_t hstack, void* data_out, const unsigned int size) {
if (hstack < 0)
return 0u;
unsigned int idx = (unsigned int)hstack;
if (idx >= g_table.capacity)
return 0u;
if (!g_table.entries[idx].used)
return 0u;
stack_t top = g_table.entries[idx].top;
if (!top)
return 0u;
// Ïðîâåðèòü óêàçàòåëü íà âûõîäíîé áóôåð
if (!data_out)
return 0u;
if (size < top->size)
return 0u;
unsigned int copied = top->size;
memcpy(data_out, top->data, copied);
g_table.entries[idx].top = top->prev;
g_table.entries[idx].count -= 1u;
free(top);
return copied;
}