Did the doxygen comments, did all the .md files for report too
This commit is contained in:
@@ -7,34 +7,75 @@
|
||||
#include <new>
|
||||
#include <print>
|
||||
|
||||
/// @brief Default initial size for the stack
|
||||
#define DEFAULT_STACK_SIZE 4
|
||||
|
||||
/**
|
||||
* @brief Error codes for stack operations
|
||||
*/
|
||||
enum class StackErr {
|
||||
ok,
|
||||
bad_alloc,
|
||||
empty,
|
||||
ok, ///< Operation successful
|
||||
bad_alloc, ///< Memory allocation failed
|
||||
empty, ///< Stack is empty
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generic stack implementation
|
||||
*
|
||||
* @tparam T Type of elements stored in the stack
|
||||
* @note T must be formattable with std::format
|
||||
*/
|
||||
template<typename T>
|
||||
requires std::formattable<T, char>
|
||||
class Stack {
|
||||
private:
|
||||
uint64_t len;
|
||||
uint64_t cap;
|
||||
T *data;
|
||||
uint64_t len; ///< Current number of elements
|
||||
uint64_t cap; ///< Current capacity
|
||||
T *data; ///< Pointer to data array
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new Stack object
|
||||
*/
|
||||
Stack();
|
||||
|
||||
/**
|
||||
* @brief Destroy the Stack object
|
||||
*/
|
||||
~Stack();
|
||||
|
||||
/**
|
||||
* @brief Remove and return the top element
|
||||
*
|
||||
* @return std::expected<T, StackErr> Top element or error
|
||||
*/
|
||||
std::expected<T, StackErr> pop();
|
||||
|
||||
/**
|
||||
* @brief Get the top element without removing it
|
||||
*
|
||||
* @return std::expected<T, StackErr> Top element or error
|
||||
*/
|
||||
std::expected<T, StackErr> peek();
|
||||
|
||||
/**
|
||||
* @brief Push a new element onto the stack
|
||||
*
|
||||
* @param val Value to push
|
||||
* @return StackErr Result of the operation
|
||||
*/
|
||||
StackErr push(T val);
|
||||
|
||||
/**
|
||||
* @brief Get current size of the stack
|
||||
*
|
||||
* @return uint64_t Number of elements
|
||||
*/
|
||||
uint64_t size();
|
||||
|
||||
/**
|
||||
* @brief Print stack contents to console
|
||||
*/
|
||||
void print();
|
||||
};
|
||||
|
||||
@@ -61,7 +102,7 @@ Stack<T>::~Stack() {
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
StackErr Stack<T>::push(T value) {
|
||||
// If not enough space allocate more space
|
||||
/// Resize if needed
|
||||
if (this->len >= this->cap) {
|
||||
uint64_t new_capacity = this->cap * 2;
|
||||
T *tmp;
|
||||
@@ -90,6 +131,7 @@ StackErr Stack<T>::push(T value) {
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
std::expected<T, StackErr> Stack<T>::pop() {
|
||||
/// Check empty stack
|
||||
if (this->len == 0) {
|
||||
return std::unexpected(StackErr::empty);
|
||||
}
|
||||
@@ -97,6 +139,7 @@ std::expected<T, StackErr> Stack<T>::pop() {
|
||||
T return_val = this->data[this->len - 1];
|
||||
this->len--;
|
||||
|
||||
/// Shrink if too much unused space
|
||||
if (this->cap / 4 > this->len) {
|
||||
uint64_t new_capacity = this->cap / 2;
|
||||
T *tmp;
|
||||
@@ -122,6 +165,7 @@ std::expected<T, StackErr> Stack<T>::pop() {
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
std::expected<T, StackErr> Stack<T>::peek() {
|
||||
/// Check empty stack
|
||||
if (this->len == 0) {
|
||||
return std::unexpected(StackErr::empty);
|
||||
}
|
||||
@@ -149,4 +193,4 @@ void Stack<T>::print() {
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !ST
|
||||
#endif // STACK_H
|
||||
|
||||
Reference in New Issue
Block a user