-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
154 lines (132 loc) · 4.86 KB
/
stack.h
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
/** \file stack.h
*/
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define RESET "\033[0m"
#define RED "\a\033[1;31m"
#define GREEN "\033[1;32m"
#define ERROR (0)
#define OK (1)
#define RELEASE (0)
#define TESTING (1)
#define DEBUG (2)
#define LEN_OF_CANARY (3) /**< Length of used canary */
#define ECHO_STR( var ) (printf("%s = \"%s\"", strchr(#var, '>') + 1, var))
#define ECHO_INT( var ) (printf("%s = %d" , strchr(#var, '>') + 1, var))
#define ECHO_VAR( var, format) ({printf("%s = " , strchr(#var, '>') + 1 ); \
printf( format , var);})
#define ASSERT_OK( ) (\
{\
if(!Stack_OK(this))\
{\
if(this->using_state == DEBUG) \
Stack_dump(this); \
if(this->using_state != TESTING)\
assert(!"Object not OK");\
return ERROR;\
}\
});
typedef int stack_val_type;/**< Type of items in stack, may be changed to char/int/double in the code file*/
/** \brief Realization of stack as a structure
*
*
* \param int using_state - current state of using stack: RELEASE / TESTING / DEBUG
*
* \param char name[50] - name of the stack by which it is used in the function that created the stack
* \param int size - current number of items in the stack
* \param capacity - current maximum of size
* \param stack_val_type hash_sum - control sum that is calculated every time stack is modified legally
*
* \param stack_val_type* data - place of all the stack info
* \param stack_val_type* items - actual place of stack items
*
* \param char print_format[3] - format by which items are printed
* \param stack_val_type canary[LEN_OF_CANARY] - canary of the stack
*
* \note Operates with char/int/double only
*/
struct Stack
{
int using_state;
char name[50];
int size;
int capacity;
stack_val_type hash_sum;
stack_val_type* data;
stack_val_type* items;
char print_format[3];
stack_val_type canary[LEN_OF_CANARY];
};
//==========__CONSTRUCTOR__===========
#define Stack_construct( var ) (Stack_construct_with_name(&var, __FUNCTION__, #var))
/** \brief Initializes stack
*
* \param this struct Stack* [in] - place of stack to initialize
* \param creating_function const char* [in] - name of the function that called stack construction
* \param stack_name const char* [in] - name of the stack
*
* \note the define "Stack_construct" allows users to fill last 2 parameters automatically
* \warning Must to use before working with stack
*/
void Stack_construct_with_name(struct Stack* this, const char* creating_function, const char* stack_name);
//==========__DESTRUCTOR__============
/** \brief Destroys stack
*
* \param this struct Stack* [in] - place of stack to destroy
*
* \warning Must to use after working with stack
*/
void Stack_destruct(struct Stack* this);
//==========__PUSH__==================
/** \brief Pushes new item into stack
*
* \param this struct Stack* [in] - stack to push the item into
* \param new_item stack_val_type [in] - new item to be pushed
* \return (ERROR) - if an error occurred, else (OK)
*
* \warning Calls ABORT if (using_state) is RELEASE or DEBUG
*/
int Stack_push(struct Stack* this, stack_val_type new_item);
//==========__POP__===================
/** \brief Pops last item from stack if available
*
* \param this struct Stack* [in] - stack to pop from
* \param new_item stack_val_type [out] - the item that was popped
* \return (ERROR) - if an error occurred, else (OK)
*
* \warning Calls ABORT if (using_state) is RELEASE or DEBUG
*/
int Stack_pop(struct Stack* this, stack_val_type* out_item);
//==========__DUMP__==================
/** \brief Prints detailed info if (using_state) = DEBUG
*
* \param this const struct Stack* [in] - stack to be printed
*/
void Stack_dump(const struct Stack* this);
//==========__OK__====================
/** \brief Checks either the stack works fine or not
*
* \param this const struct Stack* [in] - stack to check
* \return (ERROR) if something is wrong with the stack
*/
int Stack_OK(const struct Stack* this);
//==========__HASH__==================
/** \brief Calculates current hash sum of the (items) array of the stack
*
* \param this const structStack* [in] - stack which sum must be calculated
* \return stack_val_type - current hash sum
*
* \note Returns (0) if (size) < 0
*/
stack_val_type Stack_hash(const struct Stack* this);
//==========__SET_CANARY__============
/** \brief Initializes canary for the stack
*
* \param this struct Stack* [in] - stack to create canary for
*/
void Stack_set_canary(struct Stack* this);
#endif // STACK_H_INCLUDED