From 51600344ddcb78e1d75ec1e9e9e08307ef4c1706 Mon Sep 17 00:00:00 2001 From: LaEntropiaa Date: Tue, 17 Mar 2026 20:33:42 -0600 Subject: [PATCH] added basic, based on my own ADT c implementation of a queue --- .gitignore | 149 ++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 33 ++++++++++ include/queue.h | 93 +++++++++++++++++++++++++++ include/utils.h | 0 src/main.cpp | 7 +++ src/utils.cpp | 0 test/CMakeLists.txt | 38 +++++++++++ test/test_queue.cpp | 0 8 files changed, 320 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/queue.h create mode 100644 include/utils.h create mode 100644 src/main.cpp create mode 100644 src/utils.cpp create mode 100644 test/CMakeLists.txt create mode 100644 test/test_queue.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6129d0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,149 @@ +##Doxygen +docs +Doxyfile +images + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Linker files +*.ilk + +# Debugger Files +*.pdb + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll +*.so.* + + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# Build directories +build/ +Build/ +build-*/ +out +out/Debug + +# CMake generated files +CMakeFiles/ +CMakeCache.txt +cmake_install.cmake +Makefile +install_manifest.txt +compile_commands.json + +# Temporary files +*.tmp +*.log +*.bak +*.swp + +# vcpkg +vcpkg_installed/ + +# debug information files +*.dwo + +# test output & cache +Testing/ +.cache/Doxygen +docs +Doxyfile +images + +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Linker files +*.ilk + +# Debugger Files +*.pdb + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll +*.so.* + + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# Build directories +build/ +Build/ +build-*/ + +# CMake generated files +CMakeFiles/ +CMakeCache.txt +cmake_install.cmake +Makefile +install_manifest.txt +compile_commands.json + +# Temporary files +*.tmp +*.log +*.bak +*.swp + +# vcpkg +vcpkg_installed/ + +# debug information files +*.dwo + +# test output & cache +Testing/ +.cache/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6be6428 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.16) + +project(QueueProject + VERSION 1.0 + LANGUAGES CXX +) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +add_compile_options( + -Wall + -Wextra + -Wpedantic +) + + +add_executable(main_exec + src/main.cpp + src/utils.cpp +) + +target_include_directories(main_exec + PRIVATE + ${PROJECT_SOURCE_DIR}/include +) + +enable_testing() + +add_subdirectory(test) diff --git a/include/queue.h b/include/queue.h new file mode 100644 index 0000000..f614070 --- /dev/null +++ b/include/queue.h @@ -0,0 +1,93 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#include +#include +#include +#include +#include + +#define DEFAULT_QUEUE_SIZE 4 + +enum class QueueErr { + ok, + bad_alloc, + empty, +}; + +template +requires std::formattable +class Queue { +private: + uint64_t len; + uint64_t cap; + uint64_t head; + uint64_t tail; + T *data; +public: + Queue(); + Queue(uint64_t size); + + ~Queue(); + + QueueErr enqueue(T value); + + std::expected peek(); + + std::expected unqueue(); + + void display(); +}; + +template +requires std::formattable +Queue::Queue() { + this->cap = 0; + this->len = 0; + this->head = 0; + this->tail = 0; + this->data = new T[DEFAULT_QUEUE_SIZE]; +} + +template +requires std::formattable +Queue::Queue(uint64_t size) { + this->cap = 0; + this->len = 0; + this->head = 0; + this->tail = 0; + this->data = new T[size]; + +} + + +template +requires std::formattable +QueueErr Queue::enqueue(T value) { + if (this->len >= this->cap) { + uint64_t new_cap = this->cap * 2; + T *tmp; + + try { + T *tmp = new T[new_cap]; + } catch(const std::bad_alloc& e) { + return QueueErr::bad_alloc; + } + + for (uint64_t i = 0; i < this->len; i++) { + uint64_t index = (this->tail) % this->cap; + tmp[i] = this->data[index]; + } + + delete[] this->data; + this->data = tmp; + this->cap = new_cap; + this->tail = 0; + this->head = this->len; + } + + this->data[this->head] = value; + this->len++; + this->head = (this->head + 1) % this->cap; +} +#endif // !QUEUE_H diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..5f2739b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,7 @@ +#include +#include + +int main(void) { + std::println("Hola"); + return EXIT_SUCCESS; +} diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..e69de29 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..bad7678 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,38 @@ +include(FetchContent) + +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.5.3 +) + +FetchContent_MakeAvailable(Catch2) + +add_executable(test_queue + test_queue.cpp +) + +target_include_directories(test_queue + PRIVATE + ${PROJECT_SOURCE_DIR}/include +) + +target_link_libraries(test_queue + PRIVATE + Catch2::Catch2WithMain +) + +target_compile_options(test_queue PRIVATE + -fsanitize=address + -fno-omit-frame-pointer + -g +) + +target_link_options(test_queue PRIVATE + -fsanitize=address +) + +include(CTest) +include(Catch) + +catch_discover_tests(test_queue) diff --git a/test/test_queue.cpp b/test/test_queue.cpp new file mode 100644 index 0000000..e69de29