C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
generic_def.c
Go to the documentation of this file.
1 
6 #include "common.h"
7 #include "generic_def.h"
8 
9 CMPR(char,compare_char)
10 CMPR(int, compare_int)
11 CMPR(float, compare_float)
12 
14 {
15  int res = x - y;
16 
17  if(res < 0) {
18  return eLESS;
19  }
20  if(res > 0) {
21  return eGREAT;
22  }
23  return eEQUAL;
24 }
25 
27 {
28  int res = strcmp((char*)x,(char*)y);
29 
30  if(res < 0) {
31  return eLESS;
32  }
33  if(res > 0) {
34  return eGREAT;
35  }
36  return eEQUAL;
37 }
38 
39 ASSGN(char, assign_char)
40 ASSGN(int, assign_int)
41 ASSGN(float, assign_float)
42 
44 {
45  t_gen tmp = (t_gen)get_mem(1,strlen(x)+1);
46 
47  strcpy(tmp,x);
48 
49  return tmp;
50 }
51 
52 SWAP(char, swap_char)
53 SWAP(int, swap_int)
54 SWAP(float, swap_float)
55 SWAP(char*, swap_string)
57 
58 PRINT_GEN(char, print_char)
59 PRINT_GEN(int, print_int)
60 PRINT_GEN(float, print_float)
62 
63 void print_str(t_gen str)
64 {
65  printf("%s", (char*)str);
66 }
67 
68 t_gen gen_get_idx(t_gen x, int idx1)
69 {
70  t_gen *arr = ((t_gen*)(x));
71  return arr[idx1];
72 }
73 
74 void gen_cpy_idx(t_gen x, int idx1, t_gen data)
75 {
76  t_gen *arr = ((t_gen*)(x));
77  arr[idx1] = data;
78 }
79 
80 void gen_swp_idx(t_gen x, int idx1, int idx2)
81 {
82  t_gen *arr = (t_gen*)(x);
83  t_gen tmp = arr[idx1];
84  arr[idx1] = arr[idx2];
85  arr[idx2] = tmp;
86 }
87 
88 e_cmpr gen_cmpr_idx(t_gen x, int idx1, int idx2)
89 {
90  e_cmpr ret = eEQUAL;
91  t_gen *arr = ((t_gen*)(x));
92  t_gen tmp = (t_gen)(arr[idx1] -arr[idx2]);
93  if (tmp < 0)
94  ret = eLESS;
95  else if (tmp > 0)
96  ret = eGREAT;
97  return ret;
98 }
102 
105 SWP_IDX(float,swap_idx_float)
106 
107 COPY_IDX(char, copy_idx_char)
109 COPY_IDX(float, copy_idx_float)
110 
111 GET_IDX(char, get_idx_char)
112 GET_IDX(int, get_idx_int)
113 GET_IDX(float, get_idx_float)
114 
118 
Top level include containg common headers.
t_gen gen_get_idx(t_gen x, int idx1)
Definition: generic_def.c:68
void gen_swp_idx(t_gen x, int idx1, int idx2)
Definition: generic_def.c:80
t_gen assign_string(char *x)
Definition: generic_def.c:43
void gen_cpy_idx(t_gen x, int idx1, t_gen data)
Definition: generic_def.c:74
void print_str(t_gen str)
Definition: generic_def.c:63
e_cmpr compare_string(t_gen x, t_gen y)
Definition: generic_def.c:26
e_cmpr gen_cmpr_idx(t_gen x, int idx1, int idx2)
Definition: generic_def.c:88
e_cmpr compare_gen(t_gen x, t_gen y)
Definition: generic_def.c:13
Contains Macros that are used for defining psuedo data template functions.
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_float(float)
t_gen assign_char(char)
#define COPY_IDX(T, NAME)
Template function for copying element to a given index of an array for default data types.
Definition: generic_def.h:92
#define CMPR_IDX(T, NAME)
Template function for comparing elemts at given indicies of an array for default data types.
Definition: generic_def.h:70
e_cmpr compare_idx_char(t_gen, int, int)
void print_int(t_gen)
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_int(t_gen, t_gen)
t_gen assign_int(int)
void print_float(t_gen)
#define CMPR(T, NAME)
Template Compare function for default data types.
Definition: generic_def.h:11
e_cmpr compare_char(t_gen, t_gen)
Below routines defined as reference for basic datatypes.
#define GET_IDX_CPY(T, NAME)
Template function for getting element at a given index of an array for default data types.
Definition: generic_def.h:107
#define GET_IDX(T, NAME)
Template function for getting element at a given index of an array for default data types.
Definition: generic_def.h:99
t_gen get_idx_float(t_gen, int)
#define SWP_IDX(T, NAME)
Template function for swaping elemts at given indicies of an array for default data types.
Definition: generic_def.h:83
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)
#define SWAP(T, NAME)
Template Swap function for default data types.
Definition: generic_def.h:33
void swap_string(t_gen, t_gen)
void swap_float(t_gen, t_gen)
e_cmpr compare_idx_int(t_gen, int, int)
#define PRINT_GEN(T, NAME)
Template print function for default data types.
Definition: generic_def.h:63
e_cmpr compare_idx_float(t_gen, int, int)
#define ASSGN(T, NAME)
Template Assign memory function for default data types.
Definition: generic_def.h:24
void swap_idx_float(t_gen, int, int)
void print_gen(t_gen)
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
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
@ eGREAT
Definition: typedefs.h:37
@ eLESS
Definition: typedefs.h:35
@ eEQUAL
Definition: typedefs.h:36