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

Add hashmap, hashset, Open Addressing / Linked hash tables #23

Merged
merged 30 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2d7cbf9
Add hash_set.h API
nakira974 Feb 26, 2024
7488968
Add get_random
nakira974 Feb 27, 2024
e4f1dca
Update tests and comments
nakira974 Feb 27, 2024
3a0b6e0
Add lhtbl.h for Linked hash tables API
nakira974 Feb 27, 2024
3e241b3
fixe method prefix
nakira974 Feb 27, 2024
612181c
Add htbl_fun.h into lhtbl.h
nakira974 Feb 27, 2024
3201d60
Update htbl impl
nakira974 Feb 28, 2024
cb9dde9
Fixed LinkedHashTable_Test.h and linked hash tables
nakira974 Feb 28, 2024
e001b53
update .gitignore
nakira974 Feb 28, 2024
1f724e8
Add hashmap.h
nakira974 Feb 28, 2024
d1f9a99
Add hashmap.c
nakira974 Feb 28, 2024
9a68005
reformat sources
nakira974 Feb 28, 2024
5f2b88b
Add hash_utils
nakira974 Feb 28, 2024
1fd1489
Fixed hashmaps
nakira974 Feb 29, 2024
e5ac912
Merge branch 'branches/features/hashmap' into branches/dev
nakira974 Feb 29, 2024
3bb665d
Rename some functions, created Set_Test.h, fixed DLinkedList_Test.h a…
nakira974 Feb 29, 2024
269d029
Add API definitions for hashset and correctly renamed CLinkedList
nakira974 Feb 29, 2024
0c26492
Add SCD operations for hashset, and create destroy implementations
nakira974 Feb 29, 2024
8e2105d
migrated to standard C 99 for universal support
nakira974 Feb 29, 2024
af1195a
Add hashset union, difference and intersection impl, fixed hashint co…
nakira974 Mar 1, 2024
b4c9a95
Removed bad impl in hashset
nakira974 Mar 2, 2024
143d74d
Fixed hashmap remove and pointers issues, add HashSet_Test.h
nakira974 Mar 2, 2024
729e75d
Fixed HashSet_Test.h basic test, and hashset hash handle pointer issue
nakira974 Mar 2, 2024
f22d4e4
Fixed hashset impl and unit tests
nakira974 Mar 3, 2024
f0fcac9
Add ohtbl.h API
nakira974 Mar 5, 2024
55651b5
Add ohtbl.c impl
nakira974 Mar 5, 2024
9999124
Add OAHashTable_Test.h and renamed some method #12
nakira974 Mar 5, 2024
7dc2b65
Merge branch 'trunk' into branches/dev
nakira974 Mar 5, 2024
a353ceb
Add stddef in hash_utils for liux build
nakira974 Mar 5, 2024
d0de96b
Merge remote-tracking branch 'origin/branches/dev' into branches/dev
nakira974 Mar 5, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/bin
/thrid-party/
/mingw32_x86_build/
/third-party/
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif ()
cmake_minimum_required(VERSION 3.27)
project(collections_commons C)

set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD 99)
if (CMAKE_GENERATOR MATCHES "Visual Studio")
set(CMAKE_ROOT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build/)
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ This C/C++ shared library provides a collection of commonly used data structures
hash maps, and array lists. It allows you to efficiently manage and manipulate collections of data in your C/C++ projects.

## Features

- [x] Event / Event Bus for creating commands system
- [x] Linked lists implementations (Simple / Double Chained and Circular) for storing and traversing data in a dynamic manner
- [x] Hash map and Hash set implementation for fast key-value lookups and storage and traversing data in a dynamic maner
Expand Down
62 changes: 38 additions & 24 deletions headers/clist.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extern "C" {
#endif



/**
* @brief Data structure for circular linked list element
*/
Expand All @@ -42,7 +41,7 @@ typedef struct CLinkedElement {
* @brief Data structure for a circular linked list
*/

typedef struct ClinkedList {
typedef struct CLinkedList {
/**
* @brief Current size of the list
*/
Expand All @@ -53,9 +52,9 @@ typedef struct ClinkedList {
*
* @param val1 Left value to compare
* @param val2 Right value to compare
* @return true if left is equals to right, otherwise false
* @return true if left is equals to right, false otherwise
*/
int (*match)(const void *val1, const void *val2);
bool (*equals)(const void *val1, const void *val2);

/**
* @brief Destroy handle
Expand Down Expand Up @@ -83,8 +82,8 @@ void clist_create(CLinkedList *list, void (*destroy)(void *value));

/**
* @brief Destroy the specified list, after the call no other further operations will be permit
* @param list Reference of the list to destroy otherwise false
* @complexity O(n) where n is the number of elements in the current list
* @param list Reference of the list to destroy false otherwise
* @complexity O(n) where n is the number of hashtable in the current list
*/
void clist_destroy(CLinkedList *list);

Expand All @@ -94,7 +93,7 @@ void clist_destroy(CLinkedList *list);
* @param element Reference element of the current list to add after
* @param value A generic data to add after the element parameter
* @complexity O(1)
* @return true if the element was added to the current list, otherwise false
* @return true if the element was added to the current list, false otherwise
*
*/
bool clist_add(CLinkedList *list, CLinkedElement *element, const void *value);
Expand All @@ -105,61 +104,78 @@ bool clist_add(CLinkedList *list, CLinkedElement *element, const void *value);
* @param element Element of the list to be removed
* @param value Output pointer on the value of the deleted list element reference
* @complexity O(1)
* @return true if the element was correctly removed, otherwise false
* @return true if the element was correctly removed, false otherwise
*/
bool clist_remove(CLinkedList *list, CLinkedElement *element, void **value);

/**
* @brief Returns a random element from the given list
* @param list List to return a random element from
* @param random_element Reference to a random element
* @return true if a random element has been returned, false otherwise
*/
CLinkedElement *clist_getRandom(CLinkedList *list);

/**
* @brief Replace a specified element from the given list with the specified value
* @param list List where to replace the element value
* @param element Element to replace the value
* @param value Value to replace
* @return true if the given element's value was replaces, false otherwise
*/
bool clist_replace(CLinkedList *list, CLinkedElement *element, void **value);

/* ----- MACRO C++ COMPATIBILITY -----*/
#ifdef __cplusplus
/***
* Inline function that evaluates the number of elements inside the specified list
* Inline function that evaluates the number of hashtable inside the specified list
* @return The current element count of the current list
* @complexity O(1)
*/
inline int clist_size(CLinkedList *list){
inline int clist_size(CLinkedList *list) {
return list->size;
};
} ;

/***
* Inline function that evaluates the first element of the specified list
* @return The first element of the current list
* @complexity O(1)
*/
inline CLinkedElement* clist_first(CLinkedList * list){
inline CLinkedElement *clist_first(CLinkedList *list) {
return list->head;
};
} ;

/***
* Inline function that evaluates if the specified element is the first element of the specified list
* @return true if the element is the first of the current list, otherwise false
* @return true if the element is the first of the current list, false otherwise
* @complexity O(1)
*/
inline bool clist_is_first(CLinkedList * list, CLinkedElement *element){
inline bool clist_isFirst(CLinkedList *list, CLinkedElement *element) {
return (list)->head == element;
};
} ;

/***
* Inline function that evaluates the value of a list element
* @return The value stored inside a list element
* @complexity O(1)
*/
inline void *clist_value(CLinkedElement *element){
inline void *clist_value(CLinkedElement *element) {
return ((element)->value);
};
} ;

/***
* Inline function that evaluates the next element of the current list element
* @return The reference to the next element of the current list element
* @complexity O(1)
*/
inline CLinkedElement *clist_next(CLinkedElement *element){
inline CLinkedElement *clist_next(CLinkedElement *element) {
return (element)->next;
}

/* ----- C MACRO -----*/
#else
/**
* @brief Macro that evaluates the number of elements inside the specified list
* @brief Macro that evaluates the number of hashtable inside the specified list
* @return The current element count of the current list
* @complexity O(1)
*/
Expand All @@ -174,10 +190,10 @@ inline CLinkedElement *clist_next(CLinkedElement *element){

/**
* @brief Macro that evaluates if the specified element is the first element of the specified list
* @return true if the element is the first of the current list, otherwise false
* @return true if the element is the first of the current list, false otherwise
* @complexity O(1)
*/
#define list_is_first(list, element) ((element) == (list)->head ? true : false )
#define list_isFirst(list, element) ((element) == (list)->head ? true : false )

/**
* @brief Macro that evaluates the value of a list element
Expand All @@ -199,6 +215,4 @@ inline CLinkedElement *clist_next(CLinkedElement *element){
#endif




#endif //COLLECTIONS_COMMONS_CLIST_H
88 changes: 51 additions & 37 deletions headers/dlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ extern "C" {
#endif



/**
* @brief Data structure definition for a double chained linked list generic element
*/
Expand Down Expand Up @@ -53,9 +52,9 @@ typedef struct DLinkedList {
*
* @param val1 Left value to compare
* @param val2 Right value to compare
* @return true if left is equals to right, otherwise false
* @return true if left is equals to right, false otherwise
*/
int (*match)(const void *val1, const void *val2);
bool (*equals)(const void *val1, const void *val2);

/**
* @brief Destroy handle
Expand Down Expand Up @@ -90,8 +89,8 @@ void dlist_create(DLinkedList *list, void( *destroy)(void *value));

/**
* @brief Destroy the specified list, after the call no other further operations will be permit
* @param list Reference of the list to destroy otherwise false
* @complexity O(n) where n is the number of elements in the current list
* @param list Reference of the list to destroy false otherwise
* @complexity O(n) where n is the number of hashtable in the current list
*/
void dlist_destroy(DLinkedList *list);

Expand All @@ -101,7 +100,7 @@ void dlist_destroy(DLinkedList *list);
* @param element Reference element of the current list to add after
* @param value A generic data to add after the element parameter
* @complexity O(1)
* @return true if the element was added to the current list, otherwise false
* @return true if the element was added to the current list, false otherwise
*
*/
bool dlist_add(DLinkedList *list, DLinkedElement *element, const void *value);
Expand All @@ -112,84 +111,100 @@ bool dlist_add(DLinkedList *list, DLinkedElement *element, const void *value);
* @param element Reference element of the current list to add after
* @param value A generic data to add after the element parameter
* @complexity O(1)
* @return true if the element was added to the current list, otherwise false
* @return true if the element was added to the current list, false otherwise
*
*/
bool dlist_add_before(DLinkedList *list, DLinkedElement *element, const void *value);
bool dlist_addBefore(DLinkedList *list, DLinkedElement *element, const void *value);

/**
* @brief Remove a given element from the current list, then returns a pointer on the value of the deleted element
* @param list Reference of the list to remove an element
* @param element Element of the list to be removed
* @param value Output pointer on the value of the deleted list element reference
* @brief Remove a given entry from the current list, then returns a pointer on the value of the deleted entry
* @param list Reference of the list to remove an entry
* @param entry Element of the list to be removed
* @param value Output pointer on the value of the deleted list entry reference
* @complexity O(1)
* @return true if the element was correctly removed, otherwise false
* @return true if the entry was correctly removed, false otherwise
*/
bool dlist_remove(DLinkedList *list, DLinkedElement *entry, void **value);

/**
* @brief Returns a random element from the given list
* @param list List to return a random element from
* @param value Reference to a random element
* @return true if a random element has been returned, false otherwise
*/
bool dlist_remove(DLinkedList *list, DLinkedElement *element, void **value);
DLinkedElement *dlist_getRandom(DLinkedList *list);

/**
* @brief Replace a specified element from the given list with the specified value
* @param list List where to replace the element value
* @param element Element to replace the value
* @param value Value to replace
* @return true if the given element's value was replaces, false otherwise
*/
bool dlist_replace(DLinkedList *list, DLinkedElement *element, void **value);
/* ----- MACRO C++ COMPATIBILITY -----*/
#ifdef __cplusplus

/***
* @brief Inline function that evaluates the number of elements inside the specified list
* @brief Inline function that evaluates the number of hashtable inside the specified list
* @return The current element count of the current list
* @complexity O(1)
*/
inline int dlist_size(DLinkedList *list){
inline int dlist_size(DLinkedList *list) {
return list->size;
};
} ;

/***
* @brief Inline function that evaluates the first element of the specified list
* @return The first element of the current list
* @complexity O(1)
*/
inline DLinkedElement* dlist_first(DLinkedList * list){
inline DLinkedElement *dlist_first(DLinkedList *list) {
return list->head;
};
} ;

/***
* @brief Inline function that evaluates the last element of the specified list
* @return The last element of the current list
* @complexity O(1)
*/
inline DLinkedElement *dlist_last(DLinkedList * list){
inline DLinkedElement *dlist_last(DLinkedList *list) {
return list->tail;
};
} ;

/***
* @brief Inline function that evaluates if the specified element is the first element of the specified list
* @return true if the element is the first of the current list, otherwise false
* @return true if the element is the first of the current list, false otherwise
* @complexity O(1)
*/
inline bool dlist_is_first(DLinkedList * list, DLinkedElement *element){
inline bool dlist_isFirst(DLinkedList *list, DLinkedElement *element) {
return (element)->previous == nullptr;
};
} ;

/***
* @brief Inline function that evaluates if the specified element is the last element of the specified list
* @return true if the element is the last of the current list, otherwise false
* @return true if the element is the last of the current list, false otherwise
* @complexity O(1)
*/
inline bool dlist_is_last(DLinkedList * list, DLinkedElement *element){
inline bool dlist_isLast(DLinkedList *list, DLinkedElement *element) {
return (element)->next == nullptr;
};
} ;

/***
* @brief Inline function that evaluates the value of a list element
* @return The value stored inside a list element
* @complexity O(1)
*/
inline void *dlist_value(DLinkedElement *element){
inline void *dlist_value(DLinkedElement *element) {
return ((element)->value);
};
} ;

/***
* @brief Inline function that evaluates the next element of the current list element
* @return The reference to the next element of the current list element
* @complexity O(1)
*/
inline DLinkedElement *dlist_next(DLinkedElement *element){
inline DLinkedElement *dlist_next(DLinkedElement *element) {
return (element)->next;
}

Expand All @@ -198,14 +213,14 @@ inline DLinkedElement *dlist_next(DLinkedElement *element){
* @return The reference to the next element of the current list element
* @complexity O(1)
*/
inline DLinkedElement *dlist_previous(DLinkedElement *element){
inline DLinkedElement *dlist_previous(DLinkedElement *element) {
return (element)->previous;
}

/* ----- C MACRO -----*/
#else
/***
* @brief Macro that evaluates the number of elements inside the specified list
* @brief Macro that evaluates the number of hashtable inside the specified list
* @return The current element count of the current list
* @complexity O(1)
*/
Expand All @@ -227,17 +242,17 @@ inline DLinkedElement *dlist_previous(DLinkedElement *element){

/***
* @brief Macro that evaluates if the specified element is the first element of the specified list
* @return true if the element is the first of the current list, otherwise false
* @return true if the element is the first of the current list, false otherwise
* @complexity O(1)
*/
#define dlist_is_first(list, element) ((element)->previous == NULL ? true : false )
#define dlist_isFirst(list, element) ((element)->previous == NULL ? true : false )

/***
* @brief Macro that evaluates if the specified element is the last element of the specified list
* @return true if the element is the last of the current list, otherwise false
* @return true if the element is the last of the current list, false otherwise
* @complexity O(1)
*/
#define dlist_is_last(list, element) ((element)->next == NULL ? true : false )
#define dlist_isLast(list, element) ((element)->next == NULL ? true : false )

/***
* @brief Macro that evaluates the value of a list element
Expand Down Expand Up @@ -267,5 +282,4 @@ inline DLinkedElement *dlist_previous(DLinkedElement *element){
#endif



#endif //COLLECTIONS_COMMONS_DLIST_H
Loading
Loading