libcstl
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1/*!
2 * @file
3 */
4
5#ifndef CSTL_COMMON_H
6#define CSTL_COMMON_H
7
8#define _CSTL_TOKCAT(A, B) A ## B
9#define CSTL_TOKCAT(A, B) _CSTL_TOKCAT(A, B)
10
11#include <stddef.h>
12
13/*!
14 * @defgroup lowlevel Low level containers
15 */
16/*!
17 * @defgroup highlevel High level containers
18 */
19
20/*!
21 * @brief Enumeration indicating the desired sort algorithm
22 */
23typedef enum
24{
25 /*! @brief Quicksort */
27 /*! @brief Randomized quicksort */
29 /*! @brief Median-of-three quicksort */
31 /*! @brief Heapsort */
33
34 /*! @brief Unspecified default algorithm */
37
38/*!
39 * @brief Function type for comparing (in)equality of two objects
40 *
41 * @param[in] obj1 A pointer to an object
42 * @param[in] obj2 A pointer to an object
43 * @param[in] priv A pointer to private data belonging to the callee
44 *
45 * @retval <0 @p obj1 compares as less than @p obj2
46 * @retval 0 @p obj1 compares as equal to @p obj2
47 * @retval >0 @p obj1 compares as greater than @p obj2
48 */
50 const void * obj1, const void * obj2, void * priv);
51
52/*!
53 * @brief Type for visit callbacks from objects supporting foreach
54 *
55 * @param[in] obj A pointer to the object being visited
56 * @param[in] priv A pointer to private data belonging to the callee
57 *
58 * For functions supporting the foreach functionality, this type of
59 * function will be used when the callee is allowed to modify the
60 * object being passed to the function.
61 *
62 * @retval 0 The foreach function should visit the next object
63 * @retval Nonzero The foreach function should stop visiting objects
64 */
65typedef int cstl_visit_func_t(void * obj, void * priv);
66
67/*!
68 * @brief Type for @a const visit callbacks from objects supporting foreach
69 *
70 * @param[in] obj A pointer to the object being visited
71 * @param[in] priv A pointer to private data belonging to the callee
72 *
73 * For functions supporting the foreach functionality, this type of
74 * function will be used when the callee is @a not allowed to modify the
75 * object being passed to the function.
76 *
77 * @retval 0 The foreach function should visit the next object
78 * @retval Nonzero The foreach function should stop visiting objects
79 */
80typedef int cstl_const_visit_func_t(const void * obj, void * priv);
81
82/*!
83 * @brief Type for functions called to construct, clear, or destroy an object
84 *
85 * @param[in] obj A pointer to the object being visited
86 * @param[in] priv A pointer to private data belonging to the callee
87 *
88 * Exactly how the callee should react to a call of this type is
89 * context-specific. For construction, generally, the object has been
90 * allocated, and the purpose of the call is to initialize the object.
91 * The clear/destroy distinction is not always clear and depends very
92 * much on the context. In some cases, it simply means that the memory
93 * is no longer meant to hold the particular object, and the callee
94 * should clear/free any data *held* by the object. In other cases,
95 * it may mean to free the object itself (or both).
96 */
97typedef void cstl_xtor_func_t(void * obj, void * priv);
98
99/*!
100 * @brief Type of function called to swap two objects
101 *
102 * @param[in,out] a A pointer to an object to be swapped with @p b
103 * @param[in,out] b A pointer to an object to be swapped with @p a
104 * @param[in] t A pointer to temporary/scratch space
105 * @param[in] len The number of bytes pointed to by @p t, which is equal
106 * to the number of bytes pointed to by @p a and @p b, as well.
107 *
108 * The library deals with @p void pointers, and therefore cannot swap objects
109 * any more efficiently than by copying them with the aid of scratch memory
110 * to avoid overwriting data. The use of @p void pointers also means that the
111 * library can't know what members are within the objects and update them if
112 * necessary when they are moved to a new location. In places where this
113 * occurs, the API allows callers to specify a @p swap function to handle
114 * these issues. The callee is free to ignore the @p t and @p len parameters.
115 *
116 * @see cstl_swap()
117 */
118typedef void cstl_swap_func_t(void * a, void * b, void * t, size_t len);
119
120#include <stdint.h>
121#include <string.h>
122
123/*!
124 * @brief Swap values at two memory locations via use of a third
125 *
126 * @param[in,out] x A pointer to the first value to be swapped
127 * @param[in,out] y A pointer to the second value to be swapped
128 * @param[out] t Scratch space that may be used to facilitate the swap
129 * @param[in] sz The number of bytes pointed to by @p x, @p y, and @p t
130 *
131 * The values pointed to by @p x and @p y are swapped as if by @p memcpy().
132 * The space at @p t may be used as a temporary/scratch space to facilitate
133 * the swap. The space pointed to by @p x, @p y, and @p t must be at least
134 * @p sz bytes in length.
135 */
136static inline
137void cstl_swap(void * const x, void * const y, void * const t, const size_t sz)
138{
139#ifndef NO_DOC
140#define EXCH(TYPE, A, B, T) \
141 do { \
142 *(TYPE *)T = *(TYPE *)A; \
143 *(TYPE *)A = *(TYPE *)B; \
144 *(TYPE *)B = *(TYPE *)T; \
145 } while (0)
146#endif
147
148 switch (sz) {
149 case sizeof(uint8_t): EXCH(uint8_t, x, y, t); break;
150 case sizeof(uint16_t): EXCH(uint16_t, x, y, t); break;
151 case sizeof(uint32_t): EXCH(uint32_t, x, y, t); break;
152 case sizeof(uint64_t): EXCH(uint64_t, x, y, t); break;
153 default:
154 memcpy(t, x, sz);
155 memcpy(x, y, sz);
156 memcpy(y, t, sz);
157 break;
158 }
159
160#undef EXCH
161}
162
163/*!
164 * @brief Find the last (highest order) bit set
165 *
166 * @return Zero-based index of the highest order set bit
167 * @retval -1 No bits are set, i.e. the input value is zero
168 */
169int cstl_fls(unsigned long);
170
171/*!
172 * @brief Return the maximum of two values
173 *
174 * @param[in] T The type of the values
175 * @param[in] A An input to the comparison
176 * @param[in] B An(other) input to the comparison
177 *
178 * @return The value of the maximum of the two inputs
179 */
180#define CSTL_MAX_T(T, A, B) (((T)A >= (T)B) ? (T)A : (T)B)
181
182#endif
cstl_sort_algorithm_t
Enumeration indicating the desired sort algorithm.
Definition common.h:24
@ CSTL_SORT_ALGORITHM_QUICK_M
Median-of-three quicksort.
Definition common.h:30
@ CSTL_SORT_ALGORITHM_QUICK
Quicksort.
Definition common.h:26
@ CSTL_SORT_ALGORITHM_HEAP
Heapsort.
Definition common.h:32
@ CSTL_SORT_ALGORITHM_DEFAULT
Unspecified default algorithm.
Definition common.h:35
@ CSTL_SORT_ALGORITHM_QUICK_R
Randomized quicksort.
Definition common.h:28
static void cstl_swap(void *const x, void *const y, void *const t, const size_t sz)
Swap values at two memory locations via use of a third.
Definition common.h:137
int cstl_fls(unsigned long)
Find the last (highest order) bit set.
Definition common.c:7
void cstl_xtor_func_t(void *obj, void *priv)
Type for functions called to construct, clear, or destroy an object.
Definition common.h:97
int cstl_visit_func_t(void *obj, void *priv)
Type for visit callbacks from objects supporting foreach.
Definition common.h:65
void cstl_swap_func_t(void *a, void *b, void *t, size_t len)
Type of function called to swap two objects.
Definition common.h:118
int cstl_const_visit_func_t(const void *obj, void *priv)
Type for const visit callbacks from objects supporting foreach.
Definition common.h:80
int cstl_compare_func_t(const void *obj1, const void *obj2, void *priv)
Function type for comparing (in)equality of two objects.
Definition common.h:49