Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[enhancement] formatted and added `Hash` directory to cmake (#580)
* added hash folder to CMAKE build * split sdbm code from hash.c to independent program * update readme file * docs + vartype fix * split djb2 code from hash.c to independent program * fix function reference * split xor8 code from hash.c to independent program * split adler32 code from hash.c to independent program * remove additional author * split crc32 code from hash.c to independent program * remove redundant files * interpret large numbers as specific types * disable eror clang-diagnostic-implicitly-unsigned-literal * force use constants * updating DIRECTORY.md * clang-tidy fixes for 606e5d4 * added return in function doc to enable doc Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
- Loading branch information
Showing
with
295 additions
and 157 deletions.
- +1 −1 .clang-tidy
- +1 −0 CMakeLists.txt
- +5 −3 DIRECTORY.md
- +20 −0 hash/CMakeLists.txt
- +1 −2 hash/README.md
- +0 −82 hash/hash.c
- +0 −49 hash/hash.h
- +54 −0 hash/hash_adler32.c
- +62 −0 hash/hash_crc32.c
- +50 −0 hash/hash_djb2.c
- +50 −0 hash/hash_sdbm.c
- +51 −0 hash/hash_xor8.c
- +0 −20 hash/test_program.c
| @@ -1,6 +1,6 @@ | ||
| --- | ||
| Checks: '-*,google-*,clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,openmp-*,performance-*,portability-*,modernize-*' | ||
| WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-google-explicit-constructor,-performance-move-const-arg,-performance-noexcept-move-constructor,' | ||
| WarningsAsErrors: '*,-clang-diagnostic-implicitly-unsigned-literal,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-google-explicit-constructor,-performance-move-const-arg,-performance-noexcept-move-constructor,' | ||
| HeaderFilterRegex: '' | ||
| AnalyzeTemporaryDtors: false | ||
| FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 80, AccessModifierOffset: -4 }' |
| @@ -0,0 +1,20 @@ | ||
| # If necessary, use the RELATIVE flag, otherwise each source file may be listed | ||
| # with full pathname. RELATIVE may makes it easier to extract an executable name | ||
| # automatically. | ||
| file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c ) | ||
| # file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c ) | ||
| # AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES) | ||
| foreach( testsourcefile ${APP_SOURCES} ) | ||
| # I used a simple string replace, to cut off .cpp. | ||
| string( REPLACE ".c" "" testname ${testsourcefile} ) | ||
| add_executable( ${testname} ${testsourcefile} ) | ||
|
|
||
| if(OpenMP_C_FOUND) | ||
| target_link_libraries(${testname} OpenMP::OpenMP_C) | ||
| endif() | ||
| if(MATH_LIBRARY) | ||
| target_link_libraries(${testname} ${MATH_LIBRARY}) | ||
| endif() | ||
| install(TARGETS ${testname} DESTINATION "bin/hash") | ||
|
|
||
| endforeach( testsourcefile ${APP_SOURCES} ) |
| @@ -1,8 +1,7 @@ | ||
| # Hash algorithms | ||
|
|
||
| Overview files **hash.h** and **hash.c** | ||
| * sdbm | ||
| * djb2 | ||
| * xor8 (8 bit) | ||
| * adler_32 (32 bit) | ||
| * crc32 (32 bit) | ||
| * crc32 (32 bit) |
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * @addtogroup hash Hash algorithms | ||
| * @{ | ||
| * @file hash_adler32.c | ||
| * @author [Christian Bender](https://github.com/christianbender) | ||
| * @brief 32-bit [Adler hash](https://en.wikipedia.org/wiki/Adler-32) algorithm | ||
| */ | ||
| #include <assert.h> | ||
| #include <inttypes.h> | ||
| #include <stdio.h> | ||
|
|
||
| /** | ||
| * @brief 32-bit Adler algorithm implementation | ||
| * | ||
| * @param s NULL terminated ASCII string to hash | ||
| * @return 32-bit hash result | ||
| */ | ||
| uint32_t adler32(const char* s) | ||
| { | ||
| uint32_t a = 1; | ||
| uint32_t b = 0; | ||
| const uint32_t MODADLER = 65521; | ||
|
|
||
| size_t i = 0; | ||
| while (s[i] != '\0') | ||
| { | ||
| a = (a + s[i]) % MODADLER; | ||
| b = (b + a) % MODADLER; | ||
| i++; | ||
| } | ||
| return (b << 16) | a; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Test function for ::adler32 | ||
| * \returns None | ||
| */ | ||
| void test_adler32() | ||
| { | ||
| assert(adler32("Hello World") == 403375133); | ||
| assert(adler32("Hello World!") == 474547262); | ||
| assert(adler32("Hello world") == 413860925); | ||
| assert(adler32("Hello world!") == 487130206); | ||
| printf("Tests passed\n"); | ||
| } | ||
|
|
||
| /** @} */ | ||
|
|
||
| /** Main function */ | ||
| int main() | ||
| { | ||
| test_adler32(); | ||
| return 0; | ||
| } |
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * @addtogroup hash Hash algorithms | ||
| * @{ | ||
| * @file hash_crc32.c | ||
| * @author [Christian Bender](https://github.com/christianbender) | ||
| * @brief 32-bit [CRC | ||
| * hash](https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm) | ||
| * algorithm | ||
| */ | ||
| #include <assert.h> | ||
| #include <inttypes.h> | ||
| #include <stdio.h> | ||
|
|
||
| /** | ||
| * @brief 32-bit CRC algorithm implementation | ||
| * | ||
| * @param s NULL terminated ASCII string to hash | ||
| * @return 32-bit hash result | ||
| */ | ||
| uint32_t crc32(const char* s) | ||
| { | ||
| uint32_t crc = 0xffffffff; | ||
| size_t i = 0; | ||
| while (s[i] != '\0') | ||
| { | ||
| uint8_t byte = s[i]; | ||
| crc = crc ^ byte; | ||
| for (uint8_t j = 8; j > 0; --j) | ||
| { | ||
| crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1))); | ||
| } | ||
|
|
||
| i++; | ||
| } | ||
| return crc ^ 0xffffffff; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Test function for ::crc32 | ||
| * \returns None | ||
| */ | ||
| void test_crc32() | ||
| { | ||
| assert(crc32("Hello World") == 1243066710); | ||
| assert(crc32("Hello World!") == 472456355); | ||
| assert(crc32("Hello world") == 2346098258); | ||
| assert(crc32("Hello world!") == 461707669); | ||
| // printf("%" PRIu32 "\n", crc32("Hello World")); | ||
| // printf("%" PRIu32 "\n", crc32("Hello World!")); | ||
| // printf("%" PRIu32 "\n", crc32("Hello world")); | ||
| // printf("%" PRIX32 "\n", crc32("Hello world!")); | ||
| printf("Tests passed\n"); | ||
| } | ||
|
|
||
| /** @} */ | ||
|
|
||
| /** Main function */ | ||
| int main() | ||
| { | ||
| test_crc32(); | ||
| return 0; | ||
| } |
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * @addtogroup hash Hash algorithms | ||
| * @{ | ||
| * @file hash_djb2.c | ||
| * @author [Christian Bender](https://github.com/christianbender) | ||
| * @brief [DJB2 hash algorithm](http://www.cse.yorku.ca/~oz/hash.html) | ||
| */ | ||
| #include <assert.h> | ||
| #include <inttypes.h> | ||
| #include <stdio.h> | ||
|
|
||
| /** | ||
| * @brief DJB2 algorithm implementation | ||
| * | ||
| * @param s NULL terminated string to hash | ||
| * @return 64-bit hash result | ||
| */ | ||
| uint64_t djb2(const char* s) | ||
| { | ||
| uint64_t hash = 5381; /* init value */ | ||
| size_t i = 0; | ||
| while (s[i] != '\0') | ||
| { | ||
| hash = ((hash << 5) + hash) + s[i]; | ||
| i++; | ||
| } | ||
| return hash; | ||
| } | ||
|
|
||
| /** | ||
| * Test function for ::djb2 | ||
| * \returns none | ||
| */ | ||
| void test_djb2(void) | ||
| { | ||
| assert(djb2("Hello World") == 13827776004929097857); | ||
| assert(djb2("Hello World!") == 13594750393630990530); | ||
| assert(djb2("Hello world") == 13827776004967047329); | ||
| assert(djb2("Hello world!") == 13594750394883323106); | ||
| printf("Tests passed\n"); | ||
| } | ||
|
|
||
| /** @} */ | ||
|
|
||
| /** Main function */ | ||
| int main() | ||
| { | ||
| test_djb2(); | ||
| return 0; | ||
| } |
Oops, something went wrong.

