C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
generic_def.h
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include "typedefs.h"
8 #include "memory_manager.h"
9 
11 #define CMPR(T, NAME) e_cmpr NAME(t_gen x, t_gen y)\
12  { \
13  e_cmpr ret = eEQUAL;\
14  T tmp = (*((T*)(x))) - (*((T*)(y)));\
15  if (tmp < 0) \
16  ret = eLESS;\
17  else if (tmp > 0)\
18  ret = eGREAT;\
19  return ret;\
20  }
21 
22 
24 #define ASSGN(T, NAME) t_gen NAME(T y) \
25  {\
26  t_gen x = get_mem(1, sizeof(T));\
27  *((T*)x) = y;\
28  return x;\
29  }
30 
31 
33 #define SWAP(T, NAME) void NAME(t_gen x, t_gen y) \
34  {\
35  T tmp = *((T*)x);\
36  *((T*)x) = *((T*)y);\
37  *((T*)y) = tmp;\
38  }
39 
43 #define FMT_SPF(T) _Generic((T),\
44  char: "%c", \
45  signed char: "%hhd", \
46  unsigned char: "%hhu", \
47  signed short: "%hd", \
48  unsigned short: "%hu", \
49  signed int: "%d", \
50  unsigned int: "%u", \
51  long int: "%ld", \
52  unsigned long int: "%lu", \
53  long long int: "%lld", \
54  unsigned long long int: "%llu", \
55  float: "%f", \
56  double: "%f", \
57  long double: "%Lf", \
58  char *: "%s", \
59  void *: "%p")
60 
61 
63 #define PRINT_GEN(T, NAME) void NAME(t_gen x) \
64  { \
65  T y;\
66  printf(FMT_SPF(y), *((T*)x)); \
67  }
68 
70 #define CMPR_IDX(T, NAME) e_cmpr NAME(t_gen x, int idx1, int idx2)\
71  { \
72  e_cmpr ret = eEQUAL;\
73  T *arr = ((T*)(x));\
74  T tmp = arr[idx1] -arr[idx2];\
75  if (tmp < 0) \
76  ret = eLESS;\
77  else if (tmp > 0)\
78  ret = eGREAT;\
79  return ret;\
80  }
81 
83 #define SWP_IDX(T, NAME) void NAME(t_gen x, int idx1, int idx2)\
84  { \
85  T *arr = ((T*)(x));\
86  T tmp = arr[idx1] ;\
87  arr[idx1] = arr[idx2];\
88  arr[idx2] = tmp;\
89  }
90 
92 #define COPY_IDX(T, NAME) void NAME(t_gen x, int idx1, t_gen data)\
93  { \
94  T *arr = ((T*)(x));\
95  arr[idx1] = *((T*)(data));\
96  }
97 
99 #define GET_IDX(T, NAME) t_gen NAME(t_gen x, int idx1)\
100  { \
101  T *arr = ((T*)(x));\
102  t_gen tmp = &arr[idx1] ;\
103  return tmp;\
104  }
105 
107 #define GET_IDX_CPY(T, NAME) t_gen NAME(t_gen x, int idx1)\
108  { \
109  T *arr = ((T*)(x));\
110  t_gen tmp = get_mem(1, sizeof(T));\
111  *(T*)tmp = arr[idx1] ;\
112  return tmp;\
113  }
114 
121 
125 t_gen assign_string(char*);
127 
133 
137 void print_str(t_gen);
139 
143 
144 void swap_idx_char(t_gen,int,int);
145 void swap_idx_int(t_gen,int,int);
146 void swap_idx_float(t_gen,int,int);
147 
151 
155 
159 
160 e_cmpr gen_cmpr_idx(t_gen x, int idx1, int idx2);
161 void gen_swp_idx(t_gen x, int idx1, int idx2);
162 void gen_cpy_idx(t_gen x, int idx1, t_gen data);
163 t_gen gen_get_idx(t_gen x, int idx1);
t_gen get_idx_int(t_gen, int)
void swap_idx_char(t_gen, int, int)
void copy_idx_int(t_gen, int, t_gen)
t_gen assign_gen(t_gen)
t_gen assign_float(float)
t_gen assign_char(char)
t_gen gen_get_idx(t_gen x, int idx1)
Definition: generic_def.c:68
e_cmpr compare_idx_char(t_gen, int, int)
void gen_swp_idx(t_gen x, int idx1, int idx2)
Definition: generic_def.c:80
void print_int(t_gen)
e_cmpr compare_string(t_gen, t_gen)
Definition: generic_def.c:26
t_gen get_idx_int_cpy(t_gen, int)
t_gen get_idx_char_cpy(t_gen, int)
void copy_idx_char(t_gen, int, t_gen)
e_cmpr compare_gen(t_gen, t_gen)
Definition: generic_def.c:13
e_cmpr compare_int(t_gen, t_gen)
t_gen assign_string(char *)
Definition: generic_def.c:43
void gen_cpy_idx(t_gen x, int idx1, t_gen data)
Definition: generic_def.c:74
t_gen assign_int(int)
void print_float(t_gen)
e_cmpr gen_cmpr_idx(t_gen x, int idx1, int idx2)
Definition: generic_def.c:88
e_cmpr compare_char(t_gen, t_gen)
Below routines defined as reference for basic datatypes.
t_gen get_idx_float(t_gen, int)
void swap_int(t_gen, t_gen)
void print_char(t_gen)
void swap_gen(t_gen, t_gen)
void swap_char(t_gen, t_gen)
e_cmpr compare_float(t_gen, t_gen)
t_gen get_idx_float_cpy(t_gen, int)
void copy_idx_float(t_gen, int, t_gen)
t_gen get_idx_char(t_gen, int)
void swap_idx_int(t_gen, int, int)
void swap_string(t_gen, t_gen)
void swap_float(t_gen, t_gen)
e_cmpr compare_idx_int(t_gen, int, int)
e_cmpr compare_idx_float(t_gen, int, int)
void swap_idx_float(t_gen, int, int)
void print_gen(t_gen)
void print_str(t_gen)
Definition: generic_def.c:63
Contains decalration of structures for maintaing all mem alloc and frees.
Contains typedefs for all necessary data type and function pointers.
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
e_cmpr
Custom Compare function return type.
Definition: typedefs.h:34