added basic, based on my own ADT c implementation of a queue
This commit is contained in:
149
.gitignore
vendored
Normal file
149
.gitignore
vendored
Normal file
@@ -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/
|
||||||
33
CMakeLists.txt
Normal file
33
CMakeLists.txt
Normal file
@@ -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)
|
||||||
93
include/queue.h
Normal file
93
include/queue.h
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#ifndef QUEUE_H
|
||||||
|
#define QUEUE_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <new>
|
||||||
|
#include <print>
|
||||||
|
#include <format>
|
||||||
|
#include <expected>
|
||||||
|
|
||||||
|
#define DEFAULT_QUEUE_SIZE 4
|
||||||
|
|
||||||
|
enum class QueueErr {
|
||||||
|
ok,
|
||||||
|
bad_alloc,
|
||||||
|
empty,
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
requires std::formattable<T, char>
|
||||||
|
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<T, QueueErr> peek();
|
||||||
|
|
||||||
|
std::expected<T, QueueErr> unqueue();
|
||||||
|
|
||||||
|
void display();
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
requires std::formattable<T, char>
|
||||||
|
Queue<T>::Queue() {
|
||||||
|
this->cap = 0;
|
||||||
|
this->len = 0;
|
||||||
|
this->head = 0;
|
||||||
|
this->tail = 0;
|
||||||
|
this->data = new T[DEFAULT_QUEUE_SIZE];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
requires std::formattable<T, char>
|
||||||
|
Queue<T>::Queue(uint64_t size) {
|
||||||
|
this->cap = 0;
|
||||||
|
this->len = 0;
|
||||||
|
this->head = 0;
|
||||||
|
this->tail = 0;
|
||||||
|
this->data = new T[size];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
requires std::formattable<T, char>
|
||||||
|
QueueErr Queue<T>::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
|
||||||
0
include/utils.h
Normal file
0
include/utils.h
Normal file
7
src/main.cpp
Normal file
7
src/main.cpp
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <cstdlib>
|
||||||
|
#include <print>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
std::println("Hola");
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
0
src/utils.cpp
Normal file
0
src/utils.cpp
Normal file
38
test/CMakeLists.txt
Normal file
38
test/CMakeLists.txt
Normal file
@@ -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)
|
||||||
0
test/test_queue.cpp
Normal file
0
test/test_queue.cpp
Normal file
Reference in New Issue
Block a user