Skip to content

Commit

Permalink
📝 (doxygen): Added Doxygen documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
theobori committed Sep 2, 2024
1 parent e4e4374 commit b5f2fa9
Show file tree
Hide file tree
Showing 23 changed files with 525 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ build
.direnv
result
/debug.c
a.py
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
cmake_minimum_required(VERSION 3.10)

project(tinyhash VERSION 0.0.1 LANGUAGES C DESCRIPTION "Simple C hash table implementation.")
project(tinyhash VERSION 0.0.1 LANGUAGES C DESCRIPTION "This is a library containing multiple C implementations of hashmap.")

option(BUILD_TESTS "Build the tests" OFF)
option(BUILD_DOC "Build the documentation" OFF)

add_subdirectory(src)

if (BUILD_TESTS)
add_subdirectory(tests)
endif ()

if (BUILD_DOC)
add_subdirectory(doc)
endif ()
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following CMake build arguments are available to enable or disable options.
| -- | -- | -- |
| `-DBUILD_TESTS` | Compile the test files | **`ON`**
| `-DBUILD_STATIC` | Link as a static library (instead of a shared library) | **`OFF`**
| `-DBUILD_DOC` | Build the documentation | **`OFF`**

## 🤝 Contribute

Expand All @@ -48,6 +49,12 @@ The generated Makefile will contain a special `test` target, so you can run the
make test
```

## 📝 Documentation

Just make sure you run CMake with the `-DBUILD_DOC=ON` flag.

The Makefile `all` target will automatically build the documentation.

## 📎 Some examples

Here is a basic example of how you could use the hashmap.
Expand All @@ -70,7 +77,7 @@ int main(int argc, const char *argv[]) {
bool success;
Person person = {"James", true};

// Create a controller with the separate chaining method
// Create a controller with the open addressing method
th_t th = th_create(TH_OPEN_ADRESSING);

// Insert a new key value pair
Expand Down
23 changes: 20 additions & 3 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
stdenv,
lib,
cmake,
doxygen,
graphviz,
enableStatic ? false,
enableShared ? !enableStatic,
enaleTests ? true,
enableDoc ? true,
}:

assert enableShared || enableStatic;
Expand All @@ -17,16 +20,30 @@ stdenv.mkDerivation (finalAttrs: {

cmakeFlags =
lib.optional enaleTests "-DBUILD_TESTS=ON"
++ lib.optional enableStatic "-DBUILD_STATIC=ON";
++ lib.optional enableStatic "-DBUILD_STATIC=ON"
++ lib.optional enableDoc ''
-DBUILD_DOC=ON
-DDOT_BIN_PATH=${graphviz}/bin/dot
'';

nativeBuildInputs = [ cmake ];

postBuild = ''
buildInputs = [ doxygen ];

postBuild = lib.optionals enaleTests ''
make test
'';

postInstall = lib.optionals enaleTests ''
mkdir $out/doc
for dir in "html" "latex"; do
mv doc/$dir $out/doc/$dir
done
'';

meta = {
description = "Simple C hash table implementation.";
description = "This is a library containing multiple C implementations of hashmap.";
homepage = "https://github.com/theobori/${finalAttrs.pname}";
license = lib.licenses.mit;
};
Expand Down
41 changes: 41 additions & 0 deletions doc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
find_package(Doxygen)


find_program(DOT_BIN NAMES dot)

if(DOT_BIN)
set(DOT_BIN_PATH "${DOT_BIN}" CACHE PATH "Path to dot")
else()
set(DOT_BIN_PATH "" CACHE PATH "Path to dot")
endif()

if(DOXYGEN_FOUND)
message(STATUS "Doxygen found: ${DOXYGEN_EXECUTABLE} -- ${DOXYGEN_VERSION}")

# set Doxygen input and output files.
set(DOXYGEN_INPUT_DIR ${PROJECT_SOURCE_DIR}/src)
set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/doxygen)
set(DOXYGEN_INDEX_FILE ${DOXYGEN_OUTPUT_DIR}/xml/index.xml)
set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(DOXYFILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)


# Generate DoxyFile from the input file.
configure_file(${DOXYFILE_IN} ${DOXYFILE_OUT} @ONLY)

# Create Output directory.
file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR})


# Command for generating doc from Doxygen config file.
add_custom_command(OUTPUT ${DOXYGEN_INDEX_FILE}
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE_OUT}
MAIN_DEPENDENCY ${DOXYFILE_OUT} ${DOXYFILE_IN}
COMMENT "Generating Doxygen documentation"
VERBATIM)


# Create CMake Target for generating doc.
add_custom_target(docs ALL DEPENDS ${DOXYGEN_INDEX_FILE})

endif()
35 changes: 35 additions & 0 deletions doc/Doxyfile.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
PROJECT_NAME = "Tinyhash"
PROJECT_BRIEF = "This is a library containing multiple C implementations of hashmap."
EXTRACT_ALL = YES
DISTRIBUTE_GROUP_DOC = YES
EXTRACT_PRIVATE = YES
EXTRACT_PACKAGE = YES
EXTRACT_STATIC = YES
EXTRACT_ANON_NSPACES = YES
WARN_NO_PARAMDOC = YES
RECURSIVE = YES
SOURCE_BROWSER = YES
REFERENCED_BY_RELATION = YES
REFERENCES_RELATION = YES
REFERENCES_LINK_SOURCE = NO
GENERATE_TREEVIEW = NO

HAVE_DOT = YES
DOT_GRAPH_MAX_NODES = 50
MAX_DOT_GRAPH_DEPTH = 0
DOT_PATH = "@DOT_BIN_PATH@"

CALL_GRAPH = YES
CALLER_GRAPH = YES
CLASS_GRAPH = YES
COLLABORATION_GRAPH = YES
GROUP_GRAPHS = YES
UML_LOOK = YES
TEMPLATE_RELATIONS = YES
INCLUDE_GRAPH = YES
INCLUDED_BY_GRAPH = YES
DIRECTORY_GRAPH = YES
DOT_CLEANUP = YES

INPUT = "@DOXYGEN_INPUT_DIR@" @PROJECT_SOURCE_DIR@/README.md
USE_MDFILE_AS_MAINPAGE = "@PROJECT_SOURCE_DIR@/README.md"
4 changes: 3 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
packages = forEachSupportedSystem (
{ pkgs }:
{
default = pkgs.callPackage ./. { inherit (pkgs) cmake; };
default = pkgs.callPackage ./. { };
}
);

Expand All @@ -31,6 +31,8 @@
packages = with pkgs; [
cmake
clang
doxygen
graphviz
];
};
}
Expand Down
12 changes: 11 additions & 1 deletion src/common/hash.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#include <stdint.h>
#include <stdlib.h>

/**
* @brief Initial value for hash function.
*
*/
#define TH_HASH_INITIAL_VALUE 2166136261u

/**
* @brief Multiplier for hash function.
*
*/
#define TH_HASH_MUL_VALUE 16777619

uint32_t th_hash(uint8_t *bytes, size_t size) {
if (bytes == NULL)
if (bytes == NULL) {
return 0;
}

uint32_t hash = TH_HASH_INITIAL_VALUE;

Expand Down
7 changes: 7 additions & 0 deletions src/common/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#include <stdint.h>
#include <stdio.h>

/**
* @brief Compute an unsigned int from bytes.
*
* @param bytes
* @param size
* @return uint32_t
*/
uint32_t th_hash(uint8_t *bytes, size_t size);

#endif
10 changes: 7 additions & 3 deletions src/common/key.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "key.h"

#include <string.h>

#include "hash.h"
#include "key.h"

th_key_t th_key_create(th_any_t data, size_t size) {
return (th_key_t){
Expand All @@ -13,8 +12,13 @@ th_key_t th_key_create(th_any_t data, size_t size) {
}

bool th_key_is_equal(th_key_t *first, th_key_t *second) {
if (first->size != second->size)
if (first == NULL || second == NULL) {
return false;
}

if (first->size != second->size) {
return false;
}

return memcmp(first->data, second->data, first->size) == 0;
}
20 changes: 20 additions & 0 deletions src/common/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,34 @@

#include "types.h"

/**
* @brief Represent an entry key.
*
*/
typedef struct {
uint32_t hash;
size_t size;
th_any_t data;
} th_key_t;

/**
* @brief Create a key struct from data and size, it will automatically
* compute its hash.
*
* @param data
* @param size
* @return th_key_t
*/
th_key_t th_key_create(th_any_t data, size_t size);

/**
* @brief Key comparator function.
*
* @param first
* @param second
* @return true
* @return false
*/
bool th_key_is_equal(th_key_t *first, th_key_t *second);

#endif
4 changes: 4 additions & 0 deletions src/common/table.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef __TINYHASH_COMMON_TABLE_H__
#define __TINYHASH_COMMON_TABLE_H__

/**
* @brief Generate the next capacity value absed on a previous one.
*
*/
#define TH_TABLE_NEXT_CAPACITY(capacity) (capacity) == 0 ? 8 : (capacity) * 2

#endif
8 changes: 8 additions & 0 deletions src/common/types.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#ifndef __TINYHASH_COMMON_TYPES_H__
#define __TINYHASH_COMMON_TYPES_H__

/**
* @brief Represent any type of data.
*
*/
typedef void *th_any_t;

/**
* @brief Represents any table.
*
*/
typedef void *th_generic_table_t;

#endif
4 changes: 4 additions & 0 deletions src/open_addressing/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "../common/hash.h"
#include "../common/key.h"

/**
* @brief Represent an entry within a bucket.
*
*/
typedef struct th_oa_entry_s {
th_key_t *key;
th_any_t value;
Expand Down
Loading

0 comments on commit b5f2fa9

Please sign in to comment.