From b2dbcb55e2832c373fecb4033a3ed77e5dbc77aa Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Mon, 21 Oct 2024 12:49:43 -0500 Subject: Add vector and option. --- CMakeLists.txt | 11 +- cmake/CheckGit.cmake | 14 +- cmake/fud_config.hpp.in | 16 + cmake/fud_version.hpp.in | 15 - include/fud_algorithm.hpp | 36 +- include/fud_array.hpp | 10 +- include/fud_memory.hpp | 16 + include/fud_option.hpp | 229 +++++++++ include/fud_span.hpp | 10 +- include/fud_status.hpp | 66 +-- include/fud_vector.hpp | 628 ++++++++++++++++++++++++- source/fud_string_view.cpp | 2 +- source/libfud.cpp | 8 +- test/CMakeLists.txt | 2 + test/test_common.hpp | 6 + test/test_fud.cpp | 27 +- test/test_option.cpp | 54 +++ test/test_utf8.cpp | 1125 ++++++++++++-------------------------------- test/test_vector.cpp | 138 ++++++ 19 files changed, 1502 insertions(+), 911 deletions(-) create mode 100644 cmake/fud_config.hpp.in delete mode 100644 cmake/fud_version.hpp.in create mode 100644 include/fud_option.hpp create mode 100644 test/test_option.cpp create mode 100644 test/test_vector.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f0e9aff..5a41872 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,12 +40,17 @@ target_link_libraries(fud ${SQLite3_LIBRARIES}) set_target_properties( fud PROPERTIES - CXX_STANDARD 23 + CXX_STANDARD 20 C_STANDARD 23 CXX_EXTENSIONS OFF C_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON) +if (DEFINED FUD_BOUNDS_CHECKING) +else() + set(FUD_BOUNDS_CHECKING true) +endif() + if (FUD_TEST) add_subdirectory(test) # set(CVG_FLAGS -fsanitize=address -fsanitize=undefined --coverage) @@ -98,6 +103,7 @@ set(FUD_HEADERS "include/fud_directory.hpp" "include/fud_fud_type_traits.hpp" "include/fud_memory.hpp" + "include/fud_option.hpp" "include/fud_permissions.hpp" "include/fud_result.hpp" "include/fud_span.hpp" @@ -109,6 +115,7 @@ set(FUD_HEADERS "include/fud_utf8.hpp" "include/fud_utf8_iterator.hpp" "include/fud_vector.hpp" + "${CMAKE_CURRENT_BINARY_DIR}/fud_config.hpp" ) set_target_properties(fud PROPERTIES PUBLIC_HEADER "${FUD_HEADERS}") @@ -121,5 +128,5 @@ install(TARGETS fud include(cmake/CheckGit.cmake) CheckGitSetup(GIT_HASH) -configure_file(cmake/fud_version.hpp.in include/fud_version.hpp @ONLY) +configure_file(cmake/fud_config.hpp.in include/fud_config.hpp @ONLY) add_dependencies(fud AlwaysCheckGit) diff --git a/cmake/CheckGit.cmake b/cmake/CheckGit.cmake index f7d8bce..46736e7 100644 --- a/cmake/CheckGit.cmake +++ b/cmake/CheckGit.cmake @@ -7,8 +7,8 @@ if (NOT DEFINED post_configure_dir) set(post_configure_dir ${CMAKE_BINARY_DIR}/include) endif () -set(pre_configure_file ${pre_configure_dir}/fud_version.hpp.in) -set(post_configure_file ${post_configure_dir}/fud_version.hpp) +set(pre_configure_file ${pre_configure_dir}/fud_config.hpp.in) +set(post_configure_file ${post_configure_dir}/fud_config.hpp) function(CheckGitWrite git_hash) file(WRITE ${CMAKE_BINARY_DIR}/git-state.txt ${git_hash}) @@ -37,15 +37,15 @@ function(CheckGitVersion git_hash) file(MAKE_DIRECTORY ${post_configure_dir}) endif () - # if (NOT EXISTS ${post_configure_dir}/fud_version.h) - # file(COPY ${pre_configure_dir}/fud_version.h DESTINATION ${post_configure_dir}) + # if (NOT EXISTS ${post_configure_dir}/fud_config.h) + # file(COPY ${pre_configure_dir}/fud_config.h DESTINATION ${post_configure_dir}) # endif() if (NOT DEFINED GIT_HASH_CACHE) set(GIT_HASH_CACHE "INVALID") endif () - # Only update the fud_version.cpp if the hash has changed. This will + # Only update the fud_config.cpp if the hash has changed. This will # prevent us from rebuilding the project more than we need to. if (NOT ${GIT_HASH} STREQUAL ${GIT_HASH_CACHE} OR NOT EXISTS ${post_configure_file}) # Set che GIT_HASH_CACHE variable the next build won't have @@ -70,8 +70,8 @@ function(CheckGitSetup top_git_hash) BYPRODUCTS ${post_configure_file} ) - add_library(fud_version INTERFACE ${CMAKE_BINARY_DIR}/include/fud_version.hpp) - add_dependencies(fud_version AlwaysCheckGit) + add_library(fud_config INTERFACE ${CMAKE_BINARY_DIR}/include/fud_config.hpp) + add_dependencies(fud_config AlwaysCheckGit) CheckGitVersion(git_hash) set(${top_git_hash} ${git_hash} PARENT_SCOPE) diff --git a/cmake/fud_config.hpp.in b/cmake/fud_config.hpp.in new file mode 100644 index 0000000..e83e9c3 --- /dev/null +++ b/cmake/fud_config.hpp.in @@ -0,0 +1,16 @@ +#ifndef FUD_VERSION_HPP +#define FUD_VERSION_HPP + +#include + +namespace fud { + +constexpr uint8_t FudVersionMajor = @PROJECT_VERSION_MAJOR@; +constexpr uint8_t FudVersionMinor = @PROJECT_VERSION_MINOR@; +constexpr uint8_t FudVersionPatch = @PROJECT_VERSION_PATCH@; +constexpr const char FudGitHash[] = "@GIT_HASH@"; +static constexpr bool fudBoundsChecking = @FUD_BOUNDS_CHECKING@; + +} // namespace fud + +#endif diff --git a/cmake/fud_version.hpp.in b/cmake/fud_version.hpp.in deleted file mode 100644 index 5cce0e4..0000000 --- a/cmake/fud_version.hpp.in +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef FUD_VERSION_HPP -#define FUD_VERSION_HPP - -#include - -namespace fud { - -constexpr uint8_t FudVersionMajor = @PROJECT_VERSION_MAJOR@; -constexpr uint8_t FudVersionMinor = @PROJECT_VERSION_MINOR@; -constexpr uint8_t FudVersionPatch = @PROJECT_VERSION_PATCH@; -constexpr const char GitHash[] = "@GIT_HASH@"; - -} // namespace fud - -#endif diff --git a/include/fud_algorithm.hpp b/include/fud_algorithm.hpp index e3d5d3b..0ad71d5 100644 --- a/include/fud_algorithm.hpp +++ b/include/fud_algorithm.hpp @@ -18,11 +18,11 @@ #ifndef FUD_ALGORITHM_HPP #define FUD_ALGORITHM_HPP +#include "fud_option.hpp" #include "fud_span.hpp" #include #include -#include #include namespace fud { @@ -48,30 +48,41 @@ class Iota { { } - constexpr std::optional operator()() noexcept + constexpr Iota(const Iota& rhs) noexcept = default; + + constexpr Iota(Iota&& rhs) noexcept = default; + + ~Iota() noexcept = default; + + Iota& operator=(const Iota& rhs) = default; + + Iota& operator=(Iota&& rhs) = default; + + constexpr Option operator()() noexcept { auto value = m_value; if (m_increment > 0) { if (m_limit - m_increment < m_value) { - return std::nullopt; + return NullOpt; } } else { if (m_limit + m_increment + 1 >= m_value) { - return std::nullopt; + return NullOpt; } } m_value += m_increment; return value; } - void set(T value) { + void set(T value) + { m_value = value; } private: T m_value; - const T m_increment; - const T m_limit; + T m_increment; + T m_limit; }; template @@ -91,18 +102,19 @@ Span mapTo(Span input, Span output, Func&& mapFunc) output[idx] = std::forward(mapFunc)(input[idx]); } - return input; + return output; } -template +template auto map(Span input, Func&& mapFunc, Builder&& builder) -> decltype(std::forward(builder)()) { + using Output = decltype(std::forward(builder)()); Output output{std::forward(builder)()}; for (auto idx = 0; idx < input.size() && idx < output.size(); ++idx) { output[idx] = std::forward(mapFunc)(input[idx]); } - return input; + return output; } template @@ -132,7 +144,7 @@ bool allOf(Generator&& generator, Func&& predicate) { bool result = true; while (auto val = std::forward(generator)()) { - result = result && std::forward(predicate)(*val); + result = result && std::forward(predicate)(val.value()); } return result; } @@ -152,7 +164,7 @@ bool anyOf(Generator&& generator, Func&& predicate) { bool result = false; while (auto val = std::forward(generator)()) { - result = result || std::forward(predicate)(*val); + result = result || std::forward(predicate)(val.value()); } return result; } diff --git a/include/fud_array.hpp b/include/fud_array.hpp index 807621a..dcbd54a 100644 --- a/include/fud_array.hpp +++ b/include/fud_array.hpp @@ -18,9 +18,10 @@ #ifndef FUD_ARRAY_HPP #define FUD_ARRAY_HPP -#include - #include "fud_memory.hpp" +#include "fud_span.hpp" + +#include namespace fud { @@ -106,6 +107,11 @@ struct Array { constexpr bool operator==(const Array&) const noexcept = default; constexpr auto operator<=>(const Array& other) const noexcept = default; + + Span span() + { + return Span{data(), Size}; + } }; } // namespace fud diff --git a/include/fud_memory.hpp b/include/fud_memory.hpp index 97328a9..6ce6312 100644 --- a/include/fud_memory.hpp +++ b/include/fud_memory.hpp @@ -57,6 +57,22 @@ constexpr void setMemory(Container& container, const T& value) } } +template