projects
/
cgds.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add basic Set implementation - TODO: add iterators for Set and HashTable
[cgds.git]
/
src
/
Stack.h
diff --git
a/src/Stack.h
b/src/Stack.h
index
d246107
..
b6a3bf4
100644
(file)
--- a/
src/Stack.h
+++ b/
src/Stack.h
@@
-9,129
+9,121
@@
#include <string.h>
#include "cgds/types.h"
#include "cgds/safe_alloc.h"
#include <string.h>
#include "cgds/types.h"
#include "cgds/safe_alloc.h"
-
-/**
- * @brief Generic internal stack cell.
- */
-typedef struct StackCell {
- void* data; ///< Generic data contained in the cell.
- struct StackCell* previous; ///< Previous cell in the internal single-linked list.
-} StackCell;
+#include "cgds/Vector.h"
/**
* @brief Stack containing generic data.
*/
typedef struct Stack {
/**
* @brief Stack containing generic data.
*/
typedef struct Stack {
- UInt size; ///< Count elements in the stack.
- size_t dataSize; ///< Size in bytes of a stack element.
- StackCell* top; ///< Last added element, on top of the stack.
+ size_t dataSize; ///< Size in bytes of a stack element.
+ Vector* array; ///< Internal data structure: resizeable array.
} Stack;
/**
* @brief Initialize an empty stack.
*/
void _stack_init(
} Stack;
/**
* @brief Initialize an empty stack.
*/
void _stack_init(
-
Stack* stack, ///< "this" pointer.
-
size_t dataSize ///< Size in bytes of a stack element.
+ Stack* stack, ///< "this" pointer.
+ size_t dataSize ///< Size in bytes of a stack element.
);
);
-/**
+/**
* @brief Return an allocated and initialized stack.
*/
Stack* _stack_new(
* @brief Return an allocated and initialized stack.
*/
Stack* _stack_new(
-
size_t dataSize ///< Size in bytes of a stack element.
+ size_t dataSize ///< Size in bytes of a stack element.
);
);
-/**
+/**
* @brief Return an allocated and initialized stack.
* @param type Type of a stack element (int, char*, ...).
* @brief Return an allocated and initialized stack.
* @param type Type of a stack element (int, char*, ...).
- *
+ *
* Usage: Stack* stack_new(<Type> type)
*/
#define stack_new(type) \
* Usage: Stack* stack_new(<Type> type)
*/
#define stack_new(type) \
-
_stack_new(sizeof(type))
+ _stack_new(sizeof(type))
/**
/**
- * @brief Copy constructor (
works well if items do not have allocated sub-pointer
s).
+ * @brief Copy constructor (
shallow copy, ok for basic type
s).
*/
Stack* stack_copy(
*/
Stack* stack_copy(
-
Stack* stack ///< "this" pointer.
+ Stack* stack ///< "this" pointer.
);
/**
* @brief Check if the stack is empty.
*/
);
/**
* @brief Check if the stack is empty.
*/
-
B
ool stack_empty(
-
Stack* stack ///< "this" pointer.
+
b
ool stack_empty(
+ Stack* stack ///< "this" pointer.
);
/**
* @brief Return size of the current stack.
*/
UInt stack_size(
);
/**
* @brief Return size of the current stack.
*/
UInt stack_size(
-
Stack* stack ///< "this" pointer.
+ Stack* stack ///< "this" pointer.
);
/**
* @brief Add something on top of the stack.
*/
void _stack_push(
);
/**
* @brief Add something on top of the stack.
*/
void _stack_push(
-
Stack* stack, ///< "this" pointer.
-
void* data ///< Data to be added.
+ Stack* stack, ///< "this" pointer.
+ void* data ///< Data to be added.
);
/**
* @brief Add something on top of the stack.
* @param stack "this" pointer.
* @param data Data to be added.
);
/**
* @brief Add something on top of the stack.
* @param stack "this" pointer.
* @param data Data to be added.
- *
+ *
* Usage: void stack_push(Stack* stack, void data)
*/
#define stack_push(stack, data) \
{ \
* Usage: void stack_push(Stack* stack, void data)
*/
#define stack_push(stack, data) \
{ \
-
typeof((data)
) tmp = data; \
-
_stack_push(stack,
&tmp); \
+
typeof(data
) tmp = data; \
+
_stack_push(stack,
&tmp); \
}
/**
* @brief Return what is on top of the stack.
*/
void* _stack_top(
}
/**
* @brief Return what is on top of the stack.
*/
void* _stack_top(
-
Stack* stack ///< "this" pointer.
+ Stack* stack ///< "this" pointer.
);
/**
* @brief Return what is on top of the stack.
* @param stack "this" pointer.
* @param data Data to be assigned.
);
/**
* @brief Return what is on top of the stack.
* @param stack "this" pointer.
* @param data Data to be assigned.
- *
+ *
* Usage: void stack_top(Stack* stack, void data)
*/
#define stack_top(stack, data) \
{ \
* Usage: void stack_top(Stack* stack, void data)
*/
#define stack_top(stack, data) \
{ \
-
void* pData = _stack_top(stack); \
-
data = *((typeof(&data))pData); \
+ void* pData = _stack_top(stack); \
+ data = *((typeof(&data))pData); \
}
/**
* @brief Remove the top of the stack.
*/
void stack_pop(
}
/**
* @brief Remove the top of the stack.
*/
void stack_pop(
-
Stack* stack ///< "this" pointer.
+ Stack* stack ///< "this" pointer.
);
/**
* @brief Clear the entire stack.
*/
void stack_clear(
);
/**
* @brief Clear the entire stack.
*/
void stack_clear(
-
Stack* stack ///< "this" pointer.
+ Stack* stack ///< "this" pointer.
);
/**
* @brief Destroy the stack: clear it, and free 'stack' pointer.
*/
void stack_destroy(
);
/**
* @brief Destroy the stack: clear it, and free 'stack' pointer.
*/
void stack_destroy(
-
Stack* stack ///< "this" pointer.
+ Stack* stack ///< "this" pointer.
);
#endif
);
#endif