cdcontainers  0.1.1
Library of data containers and collections for C programming language.
common.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2017 Maksim Andrianov
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_COMMON_H
22 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_COMMON_H
23 
24 #include <cdcontainers/casts.h>
25 #include <cdcontainers/hash.h>
26 
27 #include <stdbool.h>
28 #include <stddef.h>
29 
30 #define CDC_END CDC_FROM_INT(UINTPTR_MAX)
31 
32 #define CDC_MAX(a, b) ((a) > (b) ? (a) : (b))
33 
34 #define CDC_MIN(a, b) ((a) < (b) ? (a) : (b))
35 
36 #define CDC_ABS(x) ((x < 0) ? -(x) : x)
37 
38 #define CDC_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
39 
40 #define CDC_SWAP(T, x, y) \
41  do { \
42  T tmp = x; \
43  x = y; \
44  y = tmp; \
45  } while (0)
46 
47 #define CDC_INIT_STRUCT \
48  { \
49  0, \
50  }
51 
52 #define CDC_STATIC_ASSERT(COND, MSG) \
53  typedef char cdc_static_assertion_##MSG[(COND) ? 1 : -1]
54 
55 typedef void (*cdc_free_fn_t)(void *);
56 typedef int (*cdc_unary_pred_fn_t)(const void *);
57 typedef int (*cdc_binary_pred_fn_t)(const void *, const void *);
58 typedef void (*cdc_copy_fn_t)(void *, const void *);
59 
60 struct cdc_pair {
61  void *first;
62  void *second;
63 };
64 
71 struct cdc_data_info {
95  size_t size;
101  size_t __cnt;
102 };
103 
104 static inline size_t cdc_up_to_pow2(size_t x)
105 {
106  x = x - 1;
107  x |= x >> 1;
108  x |= x >> 2;
109  x |= x >> 4;
110  x |= x >> 8;
111  x |= x >> 16;
112  return x + 1;
113 }
114 
116  // Forward iterator category.
118  // Bidirectional iterator category.
120  // Random-access iterator category.
122 };
123 
124 // Short names
125 #ifdef CDC_USE_SHORT_NAMES
126 typedef struct cdc_pair pair_t;
127 typedef struct cdc_data_info data_info_t;
128 #endif
129 
130 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_COMMON_H
cdc_hash_fn_t hash
Definition: common.h:93
void * second
Definition: common.h:62
This file contains different utilities for hashing. The ideas of algorithms were borrowed from the bo...
Definition: common.h:119
void * first
Definition: common.h:61
Definition: common.h:117
cdc_copy_fn_t cp
Definition: common.h:94
void(* cdc_free_fn_t)(void *)
Definition: common.h:55
size_t(* cdc_hash_fn_t)(void const *)
Definition: hash.h:38
cdc_iterator_type
Definition: common.h:115
Definition: common.h:60
int(* cdc_binary_pred_fn_t)(const void *, const void *)
Definition: common.h:57
int(* cdc_unary_pred_fn_t)(const void *)
Definition: common.h:56
cdc_binary_pred_fn_t cmp
cmp - callback less or greater.
Definition: common.h:85
cdc_free_fn_t dfree
dfree - callback free data.
Definition: common.h:78
size_t __cnt
__cnt
Definition: common.h:101
Definition: common.h:121
static size_t cdc_up_to_pow2(size_t x)
Definition: common.h:104
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
void(* cdc_copy_fn_t)(void *, const void *)
Definition: common.h:58
size_t size
Definition: common.h:95
cdc_binary_pred_fn_t eq
eq - callback equil.
Definition: common.h:92