-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
384 lines (274 loc) · 7.43 KB
/
Stack.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "Stack.h"
void StackConstructor (Stack* stk, size_t max_size)
{
assert (stk);
if (!(stk->buffer == ERR_FREE && stk->status_error == STK_DEL ||
stk->buffer == nullptr && stk->status_error == NOT_CREATED))
{
stk->status_error = BAD_CREATE;
StackLog (stk);
return;
}
stk_type* buf = (stk_type*) malloc ((max_size + 2) * sizeof (stk_type));
if (!buf)
{
stk->status_error = DO_MEM_ERR;
StackLog (stk);
return;
}
*((storm*) buf) = STORMY_PETREL;
*((storm*) (buf + max_size + 1)) = STORMY_PETREL;
StackPoison (buf + 1, max_size);
stk->buffer = buf + 1;
stk->status_error = STK_GOOD;
stk->capacity = max_size;
stk->min_capacity = max_size;
RecountHash (stk);
return;
}
void StackDestructor (Stack* stk)
{
ASSERT_OK_B{
stk->buffer -= 1;
StackPoison (stk->buffer, stk->capacity + 2);
free (stk->buffer);
stk->buffer = ERR_FREE;
stk->size = 0;
stk->capacity = 0;
stk->status_error = NOT_CREATED;
stk->buf_hash = 0;
stk->stk_hash = 0;
GG
return;
}
void StackPoison (stk_type* buffer, size_t num)
{
assert (buffer);
for (size_t element = 0; element < num; element++)
buffer[element] = STK_POISON;
return;
}
void StackPush (Stack* stk, stk_type elem)
{
ASSERT_OK_B
if (stk->size == stk->capacity) StackResizeUp (stk);
if (stk->status_error == DO_MEM_ERR) return;
*(stk->buffer + (stk->size)++) = elem;
RecountHash (stk);
ASSERT_OK_E
return;
}
stk_type StackPop (Stack* stk)
{
ASSERT_OK_B
if (stk->size == 0)
{
stk->status_error = BAD_SIZE_ZERO;
StackLog (stk);
return STK_POISON;
}
else
{
if (stk->capacity > stk->min_capacity &&
stk->capacity >= STK_RESIZE * STK_RESIZE * stk->size)
StackResizeDown (stk);
if (stk->status_error == DO_MEM_ERR) return STK_POISON;
stk_type popped = stk->buffer[--(stk->size)];
*(stk->buffer + stk->size) = STK_POISON;
RecountHash (stk);
if (StackError (stk)) StackLog (stk);
return popped;
GG
return STK_POISON;
}
void StackResizeUp (Stack* stk)
{
ASSERT_OK_B
stk_type* buf = stk->buffer - 1;
buf = (stk_type*) realloc (buf, sizeof (stk_type) * (stk->capacity * STK_RESIZE + 2));
if (!buf)
{
stk->status_error = DO_MEM_ERR;
StackLog (stk);
return;
}
(stk->capacity) *= STK_RESIZE;
*((storm*) buf) = STORMY_PETREL;
*((storm*) (buf + stk->capacity + 1)) = STORMY_PETREL;
StackPoison (buf + stk->size + 1, stk->capacity - stk->size);
stk->buffer = buf + 1;
RecountHash (stk);
ASSERT_OK_E
return;
}
void StackResizeDown (Stack* stk)
{
ASSERT_OK_B
stk_type* buf = stk->buffer - 1;
if (!buf)
{
stk->status_error = DO_MEM_ERR;
StackLog (stk);
return;
}
*(buf + stk->capacity + 1) = STK_POISON;
if (stk->capacity >= STK_RESIZE * stk->size &&
stk->capacity >= STK_RESIZE * stk->min_capacity)
buf = (stk_type*) realloc (buf,
sizeof (stk_type) * (stk->capacity / STK_RESIZE + 2));
stk->capacity /= STK_RESIZE;
*((storm*) (buf + stk->capacity + 1)) = STORMY_PETREL;
stk->buffer = buf + 1;
RecountHash (stk);
ASSERT_OK_E
return;
}
int StackError (Stack* stk)
{
if (!stk)
return STK_NULL;
if (stk->stormy_petrel_begin != STORMY_PETREL)
return stk->status_error = STK_PETREL_B;
if (stk->stormy_petrel_end != STORMY_PETREL)
return stk->status_error = STK_PETREL_E;
if (stk->buffer == ERR_FREE)
return stk->status_error = STK_DEL;
if (!stk->buffer)
return stk->status_error = BUF_NULL;
if (stk->size > stk->capacity)
return stk->status_error = BAD_SIZE_CAP;
if (stk->capacity < stk->min_capacity)
return stk->status_error = BAD_CAP;
if (*((storm*) (stk->buffer - 1)) != STORMY_PETREL)
return stk->status_error = BUF_PETREL_B;
if (*((storm*) (stk->buffer + stk->capacity)) != STORMY_PETREL)
return stk->status_error = BUF_PETREL_E;
if (stk->status_error != 0)
return stk->status_error;
unsigned long long hash1 = stk->stk_hash;
unsigned long long hash2 = stk->buf_hash;
RecountHash (stk);
if (stk->stk_hash != hash1)
return stk->status_error = ERR_HASH_STK;
if (stk->buf_hash != hash2)
return stk->status_error = ERR_HASH_BUF;
return 0;
}
void StackLog(Stack* stk)
{
FILE* file = fopen ("stklog.txt", "a");
struct tm* mytime = nullptr;
time_t alltime = 0;
time (&alltime);
mytime = localtime (&alltime);
fprintf (file, "\n%s", asctime (mytime));
if (!stk)
{
fprintf (file, "Stack [STK_NULL] : The address of stack is null.\n\n");
fclose (file);
return;
}
if (stk->status_error == BUF_NULL)
{
fprintf (file, "Stack [BUF_NULL] : The address of buffer is nullptr.\n");
fclose (file);
return;
}
if (stk->status_error == STK_PETREL_B)
{
fprintf (file, "Stack [STK_PETREL_B] : The begin stormy petrel in stack was damaged.\n");
fclose (file);
return;
}
if (stk->status_error == STK_PETREL_E)
{
fprintf (file, "Stack [STK_PETREL_E] : The end stormy petrel in stack was damaged.\n");
fclose (file);
return;
}
if (stk->status_error == STK_DEL)
{
fprintf (file, "Stack [STK_DEL] : Stack was deleted.\n");
fclose (file);
return;
}
if (stk->status_error == DO_MEM_ERR)
{
fprintf (file, "Stack [DO_MEM_ERR] : The program can`t pick out new memory.\n");
fclose (file);
return;
}
if (stk->status_error == NOT_CREATED)
{
fprintf (file, "Stack [NOT_CREATED] : Attempt to manipulation with stack which wasn`t constructed.\n");
fclose (file);
return;
}
switch (stk->status_error)
{
case STK_GOOD:
fprintf (file, "Stack [STK_GOOD] : Nice!\n");
break;
case BAD_SIZE_CAP:
fprintf (file, "Stack [BAD_SIZE_CAP] : The size of stack is more then capacity.\n");
break;
case BAD_SIZE_ZERO:
fprintf (file, "Stack [BAD_SIZE_ZERO] : StackPop was called when size was zero.\n");
break;
case BAD_CAP:
fprintf (file, "Stack [BAD_CAP] : The capacity of stack is less then minimal capacity.\n");
break;
case BUF_PETREL_B:
fprintf (file, "Stack [BUF_PETREL_B] : The begin stormy petrel in buffer was damaged.\n");
break;
case BUF_PETREL_E:
fprintf (file, "Stack [BUF_PETREL_E] : The end stormy petrel in buffer was damaged.");
break;
case BAD_CREATE:
fprintf (file, "Stack [BAD_CREATE] : Trying to construct stack which already not empty.\n");
break;
case ERR_HASH_STK:
fprintf (file, "Stack [ERR_HASH_STK] : Data in stack was changed illegally.");
break;
case ERR_HASH_BUF:
fprintf (file, "Stack [ERR_HASH_BUF] : Data in buffer was changed illegally.");
break;
default:
fprintf (file, "Stack [UNK] : Unknown error.\n");
break;
};
fprintf (file, "size = %d\n" "capacity = %d\n" "min_capacity = %d\n" "buffer:\n",
stk->size, stk->capacity, stk->min_capacity);
/*
fprintf (file, "begin buffer petrel = %llX\n" "end buffer petrel = %llX\n",
*((storm*) (stk->buffer - 1)),
*((storm*) (stk->buffer + stk->capacity)));
*/
for (int element = 0; element < stk->capacity; element++)
fprintf (file, "[%d] = %lf\n", element, stk->buffer[element]);
fprintf (file, "\n");
fclose (file);
return;
}
unsigned long long CountHash (char* buffer, size_t num)
{
const int shift = 5;
unsigned long long ans = 0;
char buf = 0;
for (size_t count = 0; count < num; count++)
{
ans += buffer[count];
buf = ans >> (sizeof (unsigned long long) - shift);
ans = (ans << shift) + buf;
}
return ans;
}
void RecountHash (Stack* stk)
{
assert (stk);
stk->stk_hash = 0;
stk->buf_hash = 0;
stk->stk_hash = CountHash ((char*) stk, STK_HASH);
stk->buf_hash = CountHash ((char*) stk->buffer, stk->capacity * sizeof (stk_type));
return;
}