-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
68 lines (57 loc) · 1.72 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
/**
* @file stack.h
* @brief LIFO stacks.
*/
#ifndef UGLY_STACK_H
#define UGLY_STACK_H
#include "list.h"
/// Dynamic LIFO stack with constant access, peek, push (amortized) and pop.
typedef list_t stack_t;
/**
* @brief Initializes a generic stack.
*
* @param stack stack to be initialized, should be destroyed later.
* @param length initial stack capacity in number of elements.
* @param type_size size, in bytes, of each stack element.
* @param alloc memory allocator to be used (performs well with a stack allocator).
*
* @return 0 on success or ENOMEM in case alloc fails.
*/
inline err_t stack_init(stack_t *stack, index_t length, size_t type_size, struct allocator alloc)
{
return list_init(stack, length, type_size, alloc);
}
/// Frees any resources allocated by the given stack.
inline void stack_destroy(stack_t *stack)
{
list_destroy(stack);
}
/// Gets the current depth (number of elements) of the stack.
inline index_t stack_size(const stack_t *stack)
{
return list_size(stack);
}
/// Checks if the stack is empty.
inline bool stack_empty(const stack_t *stack)
{
return list_empty(stack);
}
/// Returns a pointer to the Nth element below the top of the dynamic stack.
inline void *stack_peek(const stack_t *stack, index_t n)
{
return list_ref(stack, stack_size(stack) - 1 - n);
}
/**
* @brief Pushes an element at the given address onto the top of the stack.
* @return 0 on success or ENOMEM in case ALLOC fails.
*/
inline err_t stack_push(stack_t *stack, const void *element)
{
return list_append(stack, element);
}
/// Pops the top of the stack and copies it to the given address.
inline void stack_pop(stack_t *stack, void *restrict sink)
{
list_remove(stack, list_size(stack) - 1, sink);
}
#endif // UGLY_STACK_H