|
TR_datastructures 0.1.0
A generic C data structures library
|
Public API for the stack data structure. More...
#include "tr_datastructures/tr_export.h"#include "tr_datastructures/tr_result.h"#include "tr_datastructures/tr_types.h"Go to the source code of this file.
Typedefs | |
| typedef enum tr_stack_type | tr_stack_type_t |
| Selects the underlying implementation used by the stack. | |
Enumerations | |
| enum | tr_stack_type { TR_STACK_ARRAY_DYNAMIC = 0 , TR_STACK_ARRAY_FIXED = 1 , TR_STACK_LL = 2 } |
| Selects the underlying implementation used by the stack. More... | |
Functions | |
| tr_result_t | tr_stack_create (size_t size_of_datatype, size_t elements_to_allocate, tr_stack_type_t stack_type, struct stack **id_of_stack) |
| Allocates and initialises a new stack instance. | |
| tr_result_t | tr_stack_destroy (struct stack **id_of_stack) |
| Frees all memory associated with the stack instance. | |
| tr_result_t | tr_stack_push (struct stack *id_of_stack, const void *data_to_push) |
| Pushes a deep copy of the data onto the top of the stack. | |
| tr_result_t | tr_stack_pop (struct stack *id_of_stack) |
| Removes the element at the top of the stack. | |
| tr_result_t | tr_stack_top (const struct stack *id_of_stack, void *data_at_top) |
| Copies the element at the top of the stack into the provided buffer. | |
| tr_result_t | tr_stack_size (const struct stack *id_of_stack, size_t *size) |
| Returns the current number of elements in the stack. | |
| tr_result_t | tr_stack_is_empty (const struct stack *id_of_stack, bool *is_empty) |
| Checks whether the stack contains no elements. | |
| tr_result_t | tr_stack_capacity (const struct stack *id_of_stack, size_t *capacity) |
| Returns the total allocated capacity of the stack. | |
Public API for the stack data structure.
Provides a generic stack implementation supporting multiple underlying storage strategies selectable at runtime via tr_stack_type_t.
Typical usage:
| typedef enum tr_stack_type tr_stack_type_t |
Selects the underlying implementation used by the stack.
Passed to tr_stack_create to select which implementation to use. The choice affects memory layout, growth behaviour and performance characteristics.
| Type | Memory | Growth | Best for |
|---|---|---|---|
| TR_STACK_ARRAY_DYNAMIC | Contiguous | Automatic | General purpose |
| TR_STACK_ARRAY_FIXED | Contiguous | None | Bounded/embedded use |
| TR_STACK_LL | Per node | Automatic | Unpredictable size |
| enum tr_stack_type |
Selects the underlying implementation used by the stack.
Passed to tr_stack_create to select which implementation to use. The choice affects memory layout, growth behaviour and performance characteristics.
| Type | Memory | Growth | Best for |
|---|---|---|---|
| TR_STACK_ARRAY_DYNAMIC | Contiguous | Automatic | General purpose |
| TR_STACK_ARRAY_FIXED | Contiguous | None | Bounded/embedded use |
| TR_STACK_LL | Per node | Automatic | Unpredictable size |
| tr_result_t tr_stack_capacity | ( | const struct stack * | id_of_stack, |
| size_t * | capacity | ||
| ) |
Returns the total allocated capacity of the stack.
For TR_STACK_ARRAY_DYNAMIC stacks capacity grows automatically and may be larger than the current size. For TR_STACK_ARRAY_FIXED stacks capacity is fixed at creation time and never changes. For TR_STACK_LL stacks capacity equals the current size since nodes are allocated individually.
| [in] | id_of_stack | Pointer to the stack to query |
| [out] | capacity | Pointer to receive the capacity |
Example:
| tr_result_t tr_stack_create | ( | size_t | size_of_datatype, |
| size_t | elements_to_allocate, | ||
| tr_stack_type_t | stack_type, | ||
| struct stack ** | id_of_stack | ||
| ) |
Allocates and initialises a new stack instance.
| [in] | size_of_datatype | Byte size of the datatype to store |
| [in] | elements_to_allocate | Initial number of elements to allocate |
| [in] | stack_type | Implementation type to use |
| [out] | id_of_stack | Pointer to receive the created stack |
| tr_result_t tr_stack_destroy | ( | struct stack ** | id_of_stack | ) |
Frees all memory associated with the stack instance.
Destroys the stack and sets the pointer to NULL to prevent use after free. Both the implementation data and the stack handle are freed.
| [in,out] | id_of_stack | Pointer to pointer to the stack to destroy. Set to NULL after destruction |
Example:
| tr_result_t tr_stack_is_empty | ( | const struct stack * | id_of_stack, |
| bool * | is_empty | ||
| ) |
Checks whether the stack contains no elements.
| [in] | id_of_stack | Pointer to the stack to check |
| [out] | is_empty | Set to true if the stack is empty, false otherwise |
Example:
| tr_result_t tr_stack_pop | ( | struct stack * | id_of_stack | ) |
Removes the element at the top of the stack.
Decrements the stack size by one. The data is not returned — call tr_stack_top first if you need the value before removing it.
| [in,out] | id_of_stack | Pointer to the stack to pop from |
Example:
| tr_result_t tr_stack_push | ( | struct stack * | id_of_stack, |
| const void * | data_to_push | ||
| ) |
Pushes a deep copy of the data onto the top of the stack.
Copies size_of_datatype bytes from data_to_push into the stack. The caller retains ownership of the original data.
For TR_STACK_ARRAY_DYNAMIC stacks the array grows automatically by a factor of 2 when full. For TR_STACK_ARRAY_FIXED stacks TR_ERR_FULL is returned when capacity is reached.
| [in,out] | id_of_stack | Pointer to the stack to push onto |
| [in] | data_to_push | Pointer to the data to copy onto the stack |
Example:
| tr_result_t tr_stack_size | ( | const struct stack * | id_of_stack, |
| size_t * | size | ||
| ) |
Returns the current number of elements in the stack.
| [in] | id_of_stack | Pointer to the stack to query |
| [out] | size | Pointer to receive the current element count |
Example:
| tr_result_t tr_stack_top | ( | const struct stack * | id_of_stack, |
| void * | data_at_top | ||
| ) |
Copies the element at the top of the stack into the provided buffer.
Copies size_of_datatype bytes from the top of the stack into data_at_top. The element is not removed — call tr_stack_pop afterwards if removal is needed.
The buffer pointed to by data_at_top must be at least size_of_datatype bytes large.
| [in] | id_of_stack | Pointer to the stack to peek at |
| [out] | data_at_top | Buffer to copy the top element into |
Example: