-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnc_pair.h
115 lines (103 loc) · 4.19 KB
/
nc_pair.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nc_dictionary.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/13 14:16:42 by marvin #+# #+# */
/* Updated: 2023/05/13 14:16:42 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef NC_PAIR_H
# define NC_PAIR_H
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <limits.h>
# include <unistd.h>
# include <ctype.h>
/**
* @brief A struct that holds a key and a value
*
* @param key The key of the pair
* @param value The value of the pair
*/
typedef struct s_pair
{
void *key;
void *value;
}t_pair;
/**
* @brief Creates a new pair pointer with the given key and value
*
* @param key The key to assign
* @param value The value to assign
* @return t_pair* A point to the new pair struct
* @return NULL If allocation fails
*/
t_pair *nc_pair_new(void *key, void *value);
/**
* @brief Duplicates the given pair into a newly allocated pair. If the
* keycpy and valcpy functions are provided, they will be used to duplicate
* the key and value respectively. If they are NULL, the key and value will
* be assigned directly to the new pair.
*
* @param pair The pair to copy
* @param keycpy The function to copy the key
* @param valcpy The function to copy the value
* @return t_pair* A pointer to the new pair struct
* @return NULL If allocation fails
*/
t_pair *nc_pair_copy(t_pair *pair, void *(*keycpy)(), void *(*valcpy)());
/**
* @brief Swaps the key and value of the given pair into a newly allocated
* pair. It uses the keycpy and valcpy functions to duplicate the key and
* value respectively.
*
* @note If the keycpy and valcpy functions are NULL, the key and value will
* be assigned directly to the new pair.
* @param pair The pair to swap
* @param keycpy The function to copy the key
* @param valcpy The function to copy the value
* @return t_pair* A pointer to the new pair struct
* @return NULL If allocation fails
*/
t_pair *nc_pair_swap(t_pair *pair, void *(*keycpy)(), void *(*valcpy)());
/**
* @brief Converts the given pair into a string using the provided keystr and
* valistr functions.
*
* @param pair The pair to convert
* @param keystr The function to convert the key to a string
* @param valistr The function to convert the value to a string
* @return char* A pointer to the new string
* @return NULL If allocation fails
*/
char *nc_pair_tostring(t_pair *pair, char *(*keystr)(), char *(*valstr)());
/**
* @brief Frees the given pair using the provided keydel and valdel functions.
* @note If the keydel and valdel functions are NULL, the key and value will
* be freed (treated as primitive types that cannot be freed).
* @param pair The pair to free
* @param keydel The function to delete the key
* @param valdel The function to delete the value
*/
void nc_pair_delete(t_pair *pair, void (*keydel)(), void (*valdel)());
/**
* @brief Clears the key and values of a pair, without freeing the pair itself
*
* @param pair The pair to clear
* @param keydel The function to delete the key
* @param valdel The function to delete the value
*/
void nc_pair_clear(t_pair *pair, void (*keydel)(), void (*valdel)());
/**
* @brief Prints the given pair using the provided keystr and valistr functions.
*
* @param pair The pair to print
* @param keystr The function to convert the key to a string
* @param valistr The function to convert the value to a string
*/
void nc_pair_print(t_pair *pair, char *(*keystr)(), char *(*valstr)());
#endif