LibCDS
stack.h
Go to the documentation of this file.
1 /**
2  * @file stack.h The LIFO stack.
3  */
4 
5 #ifndef _STACK_H_
6 #define _STACK_H_
7 
8 #include "../util.h"
9 
10 /** StackData is the data type for the container private information. */
11 typedef struct _StackData StackData;
12 
13 /** The implementation for stack. */
14 typedef struct _Stack {
15  /** The container private information */
17 
18  /** Insert an item to the top of the stack.
19  @see StackPush */
20  int32_t (*push) (struct _Stack*, Item);
21 
22  /** Retrieve item from the top of the stack.
23  @see StackTop */
24  int32_t (*top) (struct _Stack*, Item*);
25 
26  /** Delete item from the top of the stack.
27  @see StackPop */
28  int32_t (*pop) (struct _Stack*);
29 
30  /** Return the number of stored items.
31  @see StackSize */
32  int32_t (*size) (struct _Stack*);
33 
34  /** Set the custom item resource clean method.
35  @see StackSetDestroy */
36  int32_t (*set_destroy) (struct _Stack*, void (*) (Item));
37 } Stack;
38 
39 
40 /*===========================================================================*
41  * Definition for the exported member operations *
42  *===========================================================================*/
43 /**
44  * @brief The constructor for Stack.
45  *
46  * @param ppObj The double pointer to the to be constructed stack
47  *
48  * @retval SUCC
49  * @retval ERR_NOMEM Insufficient memory for stack construction
50  */
51 int32_t StackInit(Stack **ppObj);
52 
53 /**
54  * @brief The destructor for Stack.
55  *
56  * If the custom resource clean method is set, it also runs the clean method
57  * for all the items.
58  *
59  * @param ppObj The double pointer to the to be destructed stack
60  */
61 void StackDeinit(Stack **ppObj);
62 
63 /**
64  * @brief Insert an item to the top of the stack.
65  *
66  * This function inserts an item to the top of the stack with the corresponding
67  * stack size extension.
68  *
69  * @param self The pointer to Stack structure
70  * @param item The designated item
71  *
72  * @retval SUCC
73  * @retval ERR_NOINIT Uninitialized container
74  * @retval ERR_NOMEM Insufficient memory for stack extension
75  */
76 int32_t StackPush(Stack *self, Item item);
77 
78 /**
79  * @brief Delete item from top of the stack.
80  *
81  * This function deletes item from the top of the stack. If the custom resource
82  * clean method is set, it also runs the clean method for the deleted item.
83  *
84  * @param self The pointer to Stack structure
85  *
86  * @retval SUCC
87  * @retval ERR_NOINIT Uninitialized container
88  * @retval ERR_IDX Empty stack
89  */
90 int32_t StackPop(Stack *self);
91 
92 /**
93  * @brief Retrieve item from top of the stack.
94  *
95  * This function retrieves item from the top of the stack. If the stack is not
96  * empty, the item is returned by the second parameter. Otherwise, the error
97  * code is returned and the second parameter is updated with NULL.
98  *
99  * @param self The pointer to Stack structure
100  * @param pItem The pointer to the returned item
101  *
102  * @retval SUCC
103  * @retval ERR_NOINIT Uninitialized container
104  * @retval ERR_IDX Empty stack
105  * @retval ERR_GET Invalid parameter to store returned item
106  */
107 int32_t StackTop(Stack *self, Item *pItem);
108 
109 /**
110  * @brief Return the number of stored items.
111  *
112  * @param self The pointer to Stack structure
113  *
114  * @return The number of items
115  * @retval ERR_NOINIT Uninitialized container
116  */
117 int32_t StackSize(Stack *self);
118 
119 /**
120  * @brief Set the custom item resource clean method.
121  *
122  * @param self The pointer to Stack structure
123  * @param pFunc The function pointer to the custom method
124  *
125  * @retval SUCC
126  * @retval ERR_NOINIT Uninitialized container
127  */
128 int32_t StackSetDestroy(Stack *self, void (*pFunc) (Item));
129 
130 #endif
int32_t StackSize(Stack *self)
Return the number of stored items.
struct _StackData StackData
StackData is the data type for the container private information.
Definition: stack.h:11
int32_t StackInit(Stack **ppObj)
The constructor for Stack.
int32_t StackPop(Stack *self)
Delete item from top of the stack.
int32_t StackPush(Stack *self, Item item)
Insert an item to the top of the stack.
StackData * pData
The container private information.
Definition: stack.h:16
int32_t StackSetDestroy(Stack *self, void(*pFunc)(Item))
Set the custom item resource clean method.
int32_t StackTop(Stack *self, Item *pItem)
Retrieve item from top of the stack.
The implementation for stack.
Definition: stack.h:14
void StackDeinit(Stack **ppObj)
The destructor for Stack.