-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpair_char_int.h
93 lines (81 loc) · 1.52 KB
/
pair_char_int.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Pairs like { char: int }
* The key type is char *.
* The value type is int *.
*/
#ifndef PAIRCHARINT_H_
#define PAIRCHARINT_H_
#include <stdlib.h>
#include "pair.h"
/**
* Copies the char key of the pair.
*/
void *char_key_cpy (const_keyT key)
{
char *new_char = malloc (sizeof (char));
*new_char = *((char *) key);
return new_char;
}
/**
* Copies the int value of the pair.
*/
void *int_value_cpy (const_valueT value)
{
int *new_int = malloc (sizeof (int));
*new_int = *((int *) value);
return new_int;
}
/**
* Compares the char key of the pair.
*/
int char_key_cmp (const_keyT key_1, const_keyT key_2)
{
return *(char *) key_1 == *(char *) key_2;
}
/**
* Compares the int value of the pair.
*/
int int_value_cmp (const_valueT val_1, const_valueT val_2)
{
return *(int *) val_1 == *(int *) val_2;
}
/**
* Frees the char key of the pair.
*/
void char_key_free (keyT* key)
{
if (key && *key)
{
free (*key);
*key = NULL;
}
}
/**
* Frees the int value of the pair.
*/
void int_value_free (valueT *val)
{
if (val && *val)
{
free (*val);
*val = NULL;
}
}
/**
* @param elem pointer to a char (keyT of pair_char_int)
* @return 1 if the char represents a digit, else - 0
*/
int is_digit (const_keyT elem)
{
char c = *((char *) elem);
return (c > 47 && c < 58);
}
/**
* doubles the value pointed to by the given pointer
* @param elem pointer to an integer (valT of pair_char_int)
*/
void double_value (valueT elem)
{
*((int *) elem) *= 2;
}
#endif //PAIRCHARINT_H_