data_structures
type_str.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* type_str.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/16 22:21:44 by unite #+# #+# */
10 /* Updated: 2020/09/05 19:15:31 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "types.h"
15 
16 void *type_str_copy(const void *str)
17 {
18  if (!str)
19  return (NULL);
20  return (ds_strdup(str));
21 }
22 
23 int type_str_cmp(const void *str1, const void *str2)
24 {
25  return (ds_strcmp(str1, str2));
26 }
27 
28 size_t type_str_hash(const void *str, size_t M)
29 {
30  size_t hash;
31  char *s;
32 
33  hash = 0;
34  s = (char *)str;
35  while (*s)
36  {
37  hash = ((hash << 6) - hash + *s) % M;
38  s++;
39  }
40  return (hash);
41 }
42 
43 static const t_type g_type_str_struct = {
44  .name = "str",
45  .copy = &type_str_copy,
46  .del = &free,
47  .cmp = &type_str_cmp,
48  .hash = &type_str_hash,
49 };
50 
51 const t_type *g_type_str = &g_type_str_struct;
s_type::name
char * name
The name of a datatype as a string.
Definition: types.h:49
types.h
ds_strcmp
int ds_strcmp(const char *s1, const char *s2)
Replicates strcmp from libc.
Definition: ds_strcmp.c:20
g_type_str
const t_type * g_type_str
A representation of the char* data type.
Definition: type_str.c:51
s_type
A full representation of a data type, used to achieve polymorphism in the implementation of data stru...
Definition: types.h:47
ds_strdup
char * ds_strdup(const char *s1)
Replicates behaviour of strdup from libc.
Definition: ds_strdup.c:20