Skip to content

Commit ff2e7a3

Browse files
kvedalagithub-actions
and
github-actions
authored
[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>
1 parent 3c1b585 commit ff2e7a3

13 files changed

+295
-157
lines changed

.clang-tidy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Checks: '-*,google-*,clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.*,openmp-*,performance-*,portability-*,modernize-*'
3-
WarningsAsErrors: '*,-google-readability-*,-google-explicit-constructor,-modernize-*,modernize-avoid-c-arrays,-google-explicit-constructor,-performance-move-const-arg,-performance-noexcept-move-constructor,'
3+
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,'
44
HeaderFilterRegex: ''
55
AnalyzeTemporaryDtors: false
66
FormatStyle: '{ BasedOnStyle: Google, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 80, AccessModifierOffset: -4 }'

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ endif()
4848

4949
## Add subdirectories containing CMakeLists.txt
5050
# to configure and compile files in the respective folders
51+
add_subdirectory(hash)
5152
add_subdirectory(misc)
5253
add_subdirectory(sorting)
5354
add_subdirectory(graphics)

DIRECTORY.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@
114114
* [Djikstra](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/djikstra.c)
115115

116116
## Hash
117-
* [Hash](https://github.com/TheAlgorithms/C/blob/master/hash/hash.c)
118-
* [Hash](https://github.com/TheAlgorithms/C/blob/master/hash/hash.h)
119-
* [Test Program](https://github.com/TheAlgorithms/C/blob/master/hash/test_program.c)
117+
* [Hash Adler32](https://github.com/TheAlgorithms/C/blob/master/hash/hash_adler32.c)
118+
* [Hash Crc32](https://github.com/TheAlgorithms/C/blob/master/hash/hash_crc32.c)
119+
* [Hash Djb2](https://github.com/TheAlgorithms/C/blob/master/hash/hash_djb2.c)
120+
* [Hash Sdbm](https://github.com/TheAlgorithms/C/blob/master/hash/hash_sdbm.c)
121+
* [Hash Xor8](https://github.com/TheAlgorithms/C/blob/master/hash/hash_xor8.c)
120122

121123
## Leetcode
122124
* Src

hash/CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".c" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
12+
if(OpenMP_C_FOUND)
13+
target_link_libraries(${testname} OpenMP::OpenMP_C)
14+
endif()
15+
if(MATH_LIBRARY)
16+
target_link_libraries(${testname} ${MATH_LIBRARY})
17+
endif()
18+
install(TARGETS ${testname} DESTINATION "bin/hash")
19+
20+
endforeach( testsourcefile ${APP_SOURCES} )

hash/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Hash algorithms
22

3-
Overview files **hash.h** and **hash.c**
43
* sdbm
54
* djb2
65
* xor8 (8 bit)
76
* adler_32 (32 bit)
8-
* crc32 (32 bit)
7+
* crc32 (32 bit)

hash/hash.c

-82
This file was deleted.

hash/hash.h

-49
This file was deleted.

hash/hash_adler32.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @addtogroup hash Hash algorithms
3+
* @{
4+
* @file hash_adler32.c
5+
* @author [Christian Bender](https://github.com/christianbender)
6+
* @brief 32-bit [Adler hash](https://en.wikipedia.org/wiki/Adler-32) algorithm
7+
*/
8+
#include <assert.h>
9+
#include <inttypes.h>
10+
#include <stdio.h>
11+
12+
/**
13+
* @brief 32-bit Adler algorithm implementation
14+
*
15+
* @param s NULL terminated ASCII string to hash
16+
* @return 32-bit hash result
17+
*/
18+
uint32_t adler32(const char* s)
19+
{
20+
uint32_t a = 1;
21+
uint32_t b = 0;
22+
const uint32_t MODADLER = 65521;
23+
24+
size_t i = 0;
25+
while (s[i] != '\0')
26+
{
27+
a = (a + s[i]) % MODADLER;
28+
b = (b + a) % MODADLER;
29+
i++;
30+
}
31+
return (b << 16) | a;
32+
}
33+
34+
/**
35+
* @brief Test function for ::adler32
36+
* \returns None
37+
*/
38+
void test_adler32()
39+
{
40+
assert(adler32("Hello World") == 403375133);
41+
assert(adler32("Hello World!") == 474547262);
42+
assert(adler32("Hello world") == 413860925);
43+
assert(adler32("Hello world!") == 487130206);
44+
printf("Tests passed\n");
45+
}
46+
47+
/** @} */
48+
49+
/** Main function */
50+
int main()
51+
{
52+
test_adler32();
53+
return 0;
54+
}

hash/hash_crc32.c

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @addtogroup hash Hash algorithms
3+
* @{
4+
* @file hash_crc32.c
5+
* @author [Christian Bender](https://github.com/christianbender)
6+
* @brief 32-bit [CRC
7+
* hash](https://en.wikipedia.org/wiki/Cyclic_redundancy_check#CRC-32_algorithm)
8+
* algorithm
9+
*/
10+
#include <assert.h>
11+
#include <inttypes.h>
12+
#include <stdio.h>
13+
14+
/**
15+
* @brief 32-bit CRC algorithm implementation
16+
*
17+
* @param s NULL terminated ASCII string to hash
18+
* @return 32-bit hash result
19+
*/
20+
uint32_t crc32(const char* s)
21+
{
22+
uint32_t crc = 0xffffffff;
23+
size_t i = 0;
24+
while (s[i] != '\0')
25+
{
26+
uint8_t byte = s[i];
27+
crc = crc ^ byte;
28+
for (uint8_t j = 8; j > 0; --j)
29+
{
30+
crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
31+
}
32+
33+
i++;
34+
}
35+
return crc ^ 0xffffffff;
36+
}
37+
38+
/**
39+
* @brief Test function for ::crc32
40+
* \returns None
41+
*/
42+
void test_crc32()
43+
{
44+
assert(crc32("Hello World") == 1243066710);
45+
assert(crc32("Hello World!") == 472456355);
46+
assert(crc32("Hello world") == 2346098258);
47+
assert(crc32("Hello world!") == 461707669);
48+
// printf("%" PRIu32 "\n", crc32("Hello World"));
49+
// printf("%" PRIu32 "\n", crc32("Hello World!"));
50+
// printf("%" PRIu32 "\n", crc32("Hello world"));
51+
// printf("%" PRIX32 "\n", crc32("Hello world!"));
52+
printf("Tests passed\n");
53+
}
54+
55+
/** @} */
56+
57+
/** Main function */
58+
int main()
59+
{
60+
test_crc32();
61+
return 0;
62+
}

hash/hash_djb2.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @addtogroup hash Hash algorithms
3+
* @{
4+
* @file hash_djb2.c
5+
* @author [Christian Bender](https://github.com/christianbender)
6+
* @brief [DJB2 hash algorithm](http://www.cse.yorku.ca/~oz/hash.html)
7+
*/
8+
#include <assert.h>
9+
#include <inttypes.h>
10+
#include <stdio.h>
11+
12+
/**
13+
* @brief DJB2 algorithm implementation
14+
*
15+
* @param s NULL terminated string to hash
16+
* @return 64-bit hash result
17+
*/
18+
uint64_t djb2(const char* s)
19+
{
20+
uint64_t hash = 5381; /* init value */
21+
size_t i = 0;
22+
while (s[i] != '\0')
23+
{
24+
hash = ((hash << 5) + hash) + s[i];
25+
i++;
26+
}
27+
return hash;
28+
}
29+
30+
/**
31+
* Test function for ::djb2
32+
* \returns none
33+
*/
34+
void test_djb2(void)
35+
{
36+
assert(djb2("Hello World") == 13827776004929097857);
37+
assert(djb2("Hello World!") == 13594750393630990530);
38+
assert(djb2("Hello world") == 13827776004967047329);
39+
assert(djb2("Hello world!") == 13594750394883323106);
40+
printf("Tests passed\n");
41+
}
42+
43+
/** @} */
44+
45+
/** Main function */
46+
int main()
47+
{
48+
test_djb2();
49+
return 0;
50+
}

0 commit comments

Comments
 (0)