Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demo structure for restructuring data_structures #1283

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 60 additions & 62 deletions CMakeLists.txt
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
cmake_minimum_required(VERSION 3.6)
project(Algorithms_in_C
LANGUAGES C
VERSION 1.0.0
DESCRIPTION "Set of algorithms implemented in C."
)
project(
Algorithms_in_C
LANGUAGES C
VERSION 1.0.0
DESCRIPTION "Set of algorithms implemented in C.")

# Set compilation standards
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED YES)

if(MSVC)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
# add_compile_options(/Za)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
# add_compile_options(/Za)
endif(MSVC)

# check for math library
# addresses a bug when linking on OSX
# check for math library addresses a bug when linking on OSX
find_library(MATH_LIBRARY m)

# Optional flag - can be set by user
# Default "ON"
# Optional flag - can be set by user Default "ON"
option(USE_OPENMP "flag to use OpenMP for multithreading" ON)
if(USE_OPENMP)
find_package(OpenMP)
if (OpenMP_C_FOUND)
message(STATUS "Building with OpenMP Multithreading.")
else()
message(STATUS "No OpenMP found, no multithreading.")
endif()
find_package(OpenMP)
if(OpenMP_C_FOUND)
message(STATUS "Building with OpenMP Multithreading.")
else()
message(STATUS "No OpenMP found, no multithreading.")
endif()
endif()

## Check for some required header files
# Check for some required header files
include(CheckIncludeFile)
include(CheckSymbolExists)
check_include_file(stdbool.h HAS_STDBOOL_H)
check_include_file(inttypes.h HAS_INTTYPES_H)
check_include_file(complex.h HAS_COMPLEX_H)
if(HAS_COMPLEX_H)
check_symbol_exists(complex complex.h HAS_COMPLEX_TYPE)
check_symbol_exists(complex complex.h HAS_COMPLEX_TYPE)
endif(HAS_COMPLEX_H)
if (NOT HAS_STDBOOL_H)
message(FATAL_ERROR "Missing required header: 'stdbool.h'")
if(NOT HAS_STDBOOL_H)
message(FATAL_ERROR "Missing required header: 'stdbool.h'")
endif()
if (NOT HAS_INTTYPES_H)
message(FATAL_ERROR "Missing required header: 'inttypes.h'")
if(NOT HAS_INTTYPES_H)
message(FATAL_ERROR "Missing required header: 'inttypes.h'")
endif()

## Add subdirectories containing CMakeLists.txt
# to configure and compile files in the respective folders
# Add subdirectories containing CMakeLists.txt to configure and compile files in
# the respective folders
add_subdirectory(data_structures)
add_subdirectory(developer_tools)
add_subdirectory(hash)
add_subdirectory(misc)
Expand All @@ -67,50 +66,49 @@ add_subdirectory(math)
add_subdirectory(cipher)
add_subdirectory(dynamic_programming)

## Configure Doxygen documentation system
# Configure Doxygen documentation system
cmake_policy(SET CMP0054 NEW)
cmake_policy(SET CMP0057 NEW)
find_package(Doxygen OPTIONAL_COMPONENTS dot dia)
if(DOXYGEN_FOUND)
set(DOXYGEN_GENERATE_MAN NO)
set(DOXYGEN_USE_MATHJAX YES)
set(DOXYGEN_GENERATE_HTML YES)
# set(DOXYGEN_HTML_TIMESTAMP YES)
set(DOXYGEN_EXTRACT_STATIC YES)
set(DOXYGEN_INLINE_SOURCES YES)
set(DOXYGEN_CREATE_SUBDIRS YES)
set(DOXYGEN_GENERATE_TREEVIEW YES)
set(DOXYGEN_JAVADOC_AUTOBRIEF YES)
set(DOXYGEN_STRIP_CODE_COMMENTS NO)
set(DOXYGEN_ENABLE_PREPROCESSING YES)
set(DOXYGEN_EXT_LINKS_IN_WINDOW YES)
set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES)
set(DOXYGEN_CLANG_ASSISTED_PARSING YES)
set(DOXYGEN_FILE_PATTERNS *.c *.h *.md)
set(DOXYGEN_MATHJAX_EXTENSIONS TeX/AMSmath TeX/AMSsymbols)
set(DOXYGEN_TAGFILES "doc/cppreference-doxygen-web.tag.xml=http://en.cppreference.com/w/")
set(DOXYGEN_MATHJAX_RELPATH "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML")
if(Doxygen_dot_FOUND)
set(DOXYGEN_HAVE_DOT YES)
set(DOXYGEN_CALL_GRAPH YES)
set(DOXYGEN_INTERACTIVE_SVG YES)
set(DOXYGEN_DOT_IMAGE_FORMAT "svg")
endif()
if(OPENMP_FOUND)
set(DOXYGEN_PREDEFINED "_OPENMP=1")
endif()
if(GLUT_FOUND)
set(DOXYGEN_PREDEFINED ${DOXYGEN_PREDEFINED} "GLUT_FOUND=1")
endif()
set(DOXYGEN_GENERATE_MAN NO)
set(DOXYGEN_USE_MATHJAX YES)
set(DOXYGEN_GENERATE_HTML YES)
# set(DOXYGEN_HTML_TIMESTAMP YES)
set(DOXYGEN_EXTRACT_STATIC YES)
set(DOXYGEN_INLINE_SOURCES YES)
set(DOXYGEN_CREATE_SUBDIRS YES)
set(DOXYGEN_GENERATE_TREEVIEW YES)
set(DOXYGEN_JAVADOC_AUTOBRIEF YES)
set(DOXYGEN_STRIP_CODE_COMMENTS NO)
set(DOXYGEN_ENABLE_PREPROCESSING YES)
set(DOXYGEN_EXT_LINKS_IN_WINDOW YES)
set(DOXYGEN_OPTIMIZE_OUTPUT_FOR_C YES)
set(DOXYGEN_CLANG_ASSISTED_PARSING YES)
set(DOXYGEN_FILE_PATTERNS *.c *.h *.md)
set(DOXYGEN_MATHJAX_EXTENSIONS TeX/AMSmath TeX/AMSsymbols)
set(DOXYGEN_TAGFILES
"doc/cppreference-doxygen-web.tag.xml=http://en.cppreference.com/w/")
set(DOXYGEN_MATHJAX_RELPATH
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML"
)
if(Doxygen_dot_FOUND)
set(DOXYGEN_HAVE_DOT YES)
set(DOXYGEN_CALL_GRAPH YES)
set(DOXYGEN_INTERACTIVE_SVG YES)
set(DOXYGEN_DOT_IMAGE_FORMAT "svg")
endif()
if(OPENMP_FOUND)
set(DOXYGEN_PREDEFINED "_OPENMP=1")
endif()
if(GLUT_FOUND)
set(DOXYGEN_PREDEFINED ${DOXYGEN_PREDEFINED} "GLUT_FOUND=1")
endif()

doxygen_add_docs(
doc
${PROJECT_SOURCE_DIR}
COMMENT "Generate documentation"
)
doxygen_add_docs(doc ${PROJECT_SOURCE_DIR} COMMENT "Generate documentation")
endif()

## Enable tool to generate binary distribution files
# Enable tool to generate binary distribution files
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
3 changes: 3 additions & 0 deletions data_structures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_subdirectory(include)
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(src)
add_subdirectory(test)
7 changes: 7 additions & 0 deletions data_structures/include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file(
GLOB APP_HEADERS
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
**/*.h *.h)

# message(STATUS "APP_HEADERS: ${APP_HEADERS}")
set_property(GLOBAL PROPERTY APP_HEADERS "${HEAD_LIST}")
76 changes: 76 additions & 0 deletions data_structures/include/forehead/forehead.h
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef fore_head_h
#define fore_head_h

/**
* @file forehead.h
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved
* @brief This header must be included in every sub-header file.
* @details
* 1. forehead.h included the necessary header files which needed by every
* sub-header file.
* 2. forehead.h defined some macros for debugging.
* @note These macro functions will be heavily used in the code (in situations
* such as exception handling, test cases, and termination functions)!
* @author danielhu19 (https://github.com/danielhu19)
*/

#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved

/* @deprecated: use Macro functions below to debug */
// #define STATUS int
// #define SUCCESS 1
// #define ERROR 0
// #define INFEASIBLE -1
// #define OVERFLOW -2

/* The following three macros are originated from NJU-ICS:2020 for debugging. */
// https://stackoverflow.com/questions/26053959/what-does-va-args-in-a-macro-mean
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved
#define _LOG(...) \
do \
{ \
printf(__VA_ARGS__); \
} while (0);

// https://stackoverflow.com/questions/3585846/color-text-in-terminal-applications-in-unix
/**
* Log() is an enhanced version of printf(), specifically designed for
yaoyhu marked this conversation as resolved.
Show resolved Hide resolved
* outputting debugging information, while also including the source file, line
* number, and function where the Log() is used. This is particularly useful for
* identifying the relevant location in the code when there is a large amount of
* debug information being output.
*/
#define LOG(format, ...) \
_LOG("\33[1;34m[%s,%d,%s] " format "\33[0m\n", __FILE_NAME__, __LINE__, \
__func__, ##__VA_ARGS__)

/**
* Assert() is an upgraded version of assert() that allows for outputting
* additional information before the assertion failure when the test condition
* is false.
*/
#define Assert(cond, ...) \
do \
{ \
if (!(cond)) \
{ \
fflush(stdout); \
fprintf(stderr, "\33[1;31m"); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\33[0m\n"); \
assert(cond); \
} \
} while (0)

/**
* The panic() function is used to output information and terminate the program,
* effectively functioning as an unconditional assertion failure.
*/
#define panic(...) Assert(0, __VA_ARGS__)

#endif
38 changes: 38 additions & 0 deletions data_structures/include/list/sqlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef sqlist_h_
#define sqlist_h_

#define LISTINIT 100 // initial size of sequential list
#define ElemType int // Element type

#include <forehead/forehead.h>

/* @Obsolete: static allocation */
// #define MAX 50
// typedef struct{
// ElemType data[MAX];
// int length;
// }sqlist;

/* dynamic allocation */
typedef struct
{
ElemType* data; // data array
int length; // current length
int size; // maximum size
} sqlist;

/* Operations */

char* sqlist_to_string(sqlist* sl);
extern void sqlist_init(sqlist* sl);
extern int sqlist_create(sqlist* sl, int num);
extern int sqlist_insert(sqlist* sl, int ith, ElemType e);
extern int sqlist_delete(sqlist* sl, int ith, ElemType* e);
extern int* sqlist_locate(sqlist* sl, ElemType e,
int (*compare)(ElemType, ElemType));
extern int sqlist_search(sqlist* sl, int ith, ElemType* e);
extern int compare(ElemType x, ElemType y);

/* TODO:algorithms of sqlist */

#endif
11 changes: 11 additions & 0 deletions data_structures/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
file(
GLOB APP_SOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
**/*.c *.c)

# message(STATUS "APP_SOURCES: ${APP_SOURCES}")
get_property(APP_HEADERS GLOBAL PROPERTY APP_HEADERS)

add_library(data_structures ${APP_SOURCES} ${APP_HEADERS})
target_include_directories(data_structures
PUBLIC ${CMAKE_SOURCE_DIR}/data_structures/include)
Loading