data_structures
type_int.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* type_int.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/16 22:27:52 by unite #+# #+# */
10 /* Updated: 2020/09/04 22:54:42 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "types.h"
15 
16 void *type_int_copy(const void *i)
17 {
18  int *copy;
19 
20  if (!i || !(copy = malloc(sizeof(int))))
21  return (NULL);
22  *copy = *(int *)i;
23  return (copy);
24 }
25 
26 int type_int_cmp(const void *i1, const void *i2)
27 {
28  if (!i1 && !i2)
29  return (0);
30  if (!i1)
31  return (-1);
32  if (!i2)
33  return (1);
34  if (*(int *)i1 < *(int *)i2)
35  return (-1);
36  if (*(int *)i1 > *(int *)i2)
37  return (1);
38  return (0);
39 }
40 
41 size_t type_int_hash(const void *i, size_t M)
42 {
43  if (!i)
44  return (0);
45  return ((size_t)ptr2int(i) % M);
46 }
47 
48 static const t_type g_type_int_struct = {
49  .name = "int",
50  .copy = &type_int_copy,
51  .del = &free,
52  .cmp = &type_int_cmp,
53  .hash = &type_int_hash
54 };
55 
56 const t_type *g_type_int = &g_type_int_struct;
s_type::name
char * name
The name of a datatype as a string.
Definition: types.h:49
types.h
s_type
A full representation of a data type, used to achieve polymorphism in the implementation of data stru...
Definition: types.h:47
g_type_int
const t_type * g_type_int
A representation of the int data type.
Definition: type_int.c:56
ptr2int
int ptr2int(const void *ptr)
Converts int* to int
Definition: ptr2data.c:16