forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enhancement] formatted and added
Hash
directory to cmake (TheAlgor…
…ithms#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
13 changed files
with
295 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.