diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-30 06:39:11 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-30 06:39:11 -0500 |
| commit | a486d96236e5fc633a4a47497975104fb9f4ba80 (patch) | |
| tree | b62da629c6b8350b0f2ca6a1f778cbfd1560d35d | |
| parent | aa1a9d44a35e08fe9e52f5ccc8d32c68f65f9225 (diff) | |
| parent | a752fe69537d5d58952d2a7c7e64eb350ace48d9 (diff) | |
Merge pull request #30 from amlel-el-mahrouss/develop
chore: new tests for hash crc32 and improvements.
| -rw-r--r-- | examples/fix_tag_example/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | examples/fix_tag_example/example.cc | 2 | ||||
| -rw-r--r-- | examples/hash_crc32_example/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | examples/hash_crc32_example/example.cc | 12 | ||||
| -rw-r--r-- | include/ocl/hashing/hash.hpp (renamed from include/ocl/crc32/crc32.hpp) | 45 |
5 files changed, 61 insertions, 15 deletions
diff --git a/examples/fix_tag_example/CMakeLists.txt b/examples/fix_tag_example/CMakeLists.txt index e3b9cca..b9a80db 100644 --- a/examples/fix_tag_example/CMakeLists.txt +++ b/examples/fix_tag_example/CMakeLists.txt @@ -8,7 +8,7 @@ project( find_package(Boost REQUIRED COMPONENTS container) -add_executable(FixExample fix.cc) +add_executable(FixExample example.cc) set_property(TARGET FixExample PROPERTY CXX_STANDARD 20) target_include_directories(FixExample PUBLIC ../../include/ocl) diff --git a/examples/fix_tag_example/example.cc b/examples/fix_tag_example/example.cc index ce1f6c8..40cf33e 100644 --- a/examples/fix_tag_example/example.cc +++ b/examples/fix_tag_example/example.cc @@ -1,4 +1,4 @@ -#include <core/error_handler.hpp> +#include <core/handler.hpp> #include <fix/parser.hpp> #include <io/print.hpp> diff --git a/examples/hash_crc32_example/CMakeLists.txt b/examples/hash_crc32_example/CMakeLists.txt new file mode 100644 index 0000000..9758bfa --- /dev/null +++ b/examples/hash_crc32_example/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.15...3.31) + +project( + HashExample + VERSION 1.0 + LANGUAGES CXX) + +find_package(Boost REQUIRED COMPONENTS container) + +add_executable(HashExample example.cc) + +set_property(TARGET HashExample PROPERTY CXX_STANDARD 20) +target_include_directories(HashExample PUBLIC ../../include/ocl) +target_link_libraries(HashExample PRIVATE Boost::container) diff --git a/examples/hash_crc32_example/example.cc b/examples/hash_crc32_example/example.cc new file mode 100644 index 0000000..3f635de --- /dev/null +++ b/examples/hash_crc32_example/example.cc @@ -0,0 +1,12 @@ +#include <hashing/hash.hpp> +#include <io/print.hpp> + +int main(int argc, char** argv) +{ + if (argc != 2) return 1; + + std::hash<ocl::hash_trait> hash{}; + ocl::io::print(hash(argv[1])); + + return 0; +} diff --git a/include/ocl/crc32/crc32.hpp b/include/ocl/hashing/hash.hpp index 0ee1f8c..c64daf4 100644 --- a/include/ocl/crc32/crc32.hpp +++ b/include/ocl/hashing/hash.hpp @@ -1,6 +1,6 @@ /* - * File: crc32.hpp - * Purpose: CRC32 module. + * File: hash.hpp + * Purpose: Hashing module. * Author: Amlal El Mahrouss, * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. */ @@ -9,6 +9,7 @@ #define _OCL_CRC32_HPP #include <core/config.hpp> +#include <unordered_map> #include <cstdint> #include <string> #include <cstddef> @@ -16,13 +17,13 @@ /// @brief Crc32 implementation in C++ /// @author Amlal El Mahrouss (amlal@nekernel.org) -namespace ocl::crc32 +namespace ocl { - namespace detail + struct hash_trait { - inline constexpr const std::uint16_t crc_sz_ = 256U; + static constexpr const std::uint16_t crc_sz_ = 256U; - inline std::uint32_t crc_array_[crc_sz_] = { + static constexpr std::uint32_t crc_array_[crc_sz_] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, @@ -57,7 +58,7 @@ namespace ocl::crc32 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; template <typename buffer_type> - inline std::uint32_t + static std::uint32_t crc32(buffer_type in, size_t len) noexcept { if (!in || *in == 0) @@ -70,13 +71,31 @@ namespace ocl::crc32 return ~crc; } - } // namespace detail + }; - template <typename char_type = char> - inline std::uint32_t hash(const std::basic_string<char_type>& in) + template <typename K, typename V> + using hash_map = std::unordered_map<K, V, std::hash<ocl::hash_trait>>; +} // namespace ocl + +namespace std +{ + template <> + struct hash<ocl::hash_trait> final { - return detail::crc32<const char_type*>(in.c_str(), in.size()); - } -} // namespace ocl::crc32 + hash() = default; + ~hash() = default; + + template <typename T> + inline std::size_t operator()(T* in_) + { + return ocl::hash_trait::crc32<char*>((char*)in_, sizeof(T)); + } + + inline std::size_t operator()(const std::string& in_) + { + return ocl::hash_trait::crc32<const char*>(in_.c_str(), in_.size()); + } + }; +} // namespace std #endif // !_OCL_CRC32_HPP
\ No newline at end of file |
