Skip to content

Commit

Permalink
[enhancement] formatted and added Hash directory to cmake (TheAlgor…
Browse files Browse the repository at this point in the history
…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
kvedala and github-actions authored Jul 29, 2020
1 parent 3c1b585 commit ff2e7a3
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
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 }'
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ endif()

## Add subdirectories containing CMakeLists.txt
# to configure and compile files in the respective folders
add_subdirectory(hash)
add_subdirectory(misc)
add_subdirectory(sorting)
add_subdirectory(graphics)
Expand Down
8 changes: 5 additions & 3 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@
* [Djikstra](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/djikstra.c)

## Hash
* [Hash](https://github.com/TheAlgorithms/C/blob/master/hash/hash.c)
* [Hash](https://github.com/TheAlgorithms/C/blob/master/hash/hash.h)
* [Test Program](https://github.com/TheAlgorithms/C/blob/master/hash/test_program.c)
* [Hash Adler32](https://github.com/TheAlgorithms/C/blob/master/hash/hash_adler32.c)
* [Hash Crc32](https://github.com/TheAlgorithms/C/blob/master/hash/hash_crc32.c)
* [Hash Djb2](https://github.com/TheAlgorithms/C/blob/master/hash/hash_djb2.c)
* [Hash Sdbm](https://github.com/TheAlgorithms/C/blob/master/hash/hash_sdbm.c)
* [Hash Xor8](https://github.com/TheAlgorithms/C/blob/master/hash/hash_xor8.c)

## Leetcode
* Src
Expand Down
20 changes: 20 additions & 0 deletions hash/CMakeLists.txt
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} )
3 changes: 1 addition & 2 deletions hash/README.md
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)
82 changes: 0 additions & 82 deletions hash/hash.c

This file was deleted.

49 changes: 0 additions & 49 deletions hash/hash.h

This file was deleted.

54 changes: 54 additions & 0 deletions hash/hash_adler32.c
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;
}
62 changes: 62 additions & 0 deletions hash/hash_crc32.c
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;
}
50 changes: 50 additions & 0 deletions hash/hash_djb2.c
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;
}
Loading

0 comments on commit ff2e7a3

Please sign in to comment.