This source file includes following definitions.
- dict_set_malloc
- dict_set_free
- dict_int_cmp
- dict_uint_cmp
- dict_long_cmp
- dict_ulong_cmp
- dict_ptr_cmp
- dict_str_cmp
- dict_destroy
- dict_itor_destroy
1
2
3
4
5
6
7
8
9
10 #include <stdlib.h>
11
12 #include "dict.h"
13 #include "dict_private.h"
14
15 dict_malloc_func _dict_malloc = malloc;
16 dict_free_func _dict_free = free;
17
18 dict_malloc_func
19 dict_set_malloc(dict_malloc_func func)
20 {
21 dict_malloc_func old = _dict_malloc;
22 _dict_malloc = func ? func : malloc;
23 return old;
24 }
25
26 dict_free_func
27 dict_set_free(dict_free_func func)
28 {
29 dict_free_func old = _dict_free;
30 _dict_free = func ? func : free;
31 return old;
32 }
33
34
35
36
37
38 int
39 dict_int_cmp(const void *k1, const void *k2)
40 {
41 const int *a = (int*)k1, *b = (int*)k2;
42
43 return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
44 }
45
46 int
47 dict_uint_cmp(const void *k1, const void *k2)
48 {
49 const unsigned int *a = (unsigned int*)k1, *b = (unsigned int*)k2;
50
51 return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
52 }
53
54 int
55 dict_long_cmp(const void *k1, const void *k2)
56 {
57 const long *a = (long*)k1, *b = (long*)k2;
58
59 return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
60 }
61
62 int
63 dict_ulong_cmp(const void *k1, const void *k2)
64 {
65 const unsigned long *a = (unsigned long*)k1, *b = (unsigned long*)k2;
66
67 return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
68 }
69
70 int
71 dict_ptr_cmp(const void *k1, const void *k2)
72 {
73 return (k1 > k2) - (k1 < k2);
74 }
75
76 int
77 dict_str_cmp(const void *k1, const void *k2)
78 {
79 const char *a = (char*)k1, *b = (char*)k2;
80 char p, q;
81
82 for (;;) {
83 p = *a++; q = *b++;
84 if (p == 0 || p != q)
85 break;
86 }
87 return (p > q) - (p < q);
88 }
89
90 void
91 dict_destroy(dict *dct, int del)
92 {
93 ASSERT(dct != NULL);
94
95 dct->_destroy(dct->_object, del);
96 FREE(dct);
97 }
98
99 void
100 dict_itor_destroy(dict_itor *itor)
101 {
102 ASSERT(itor != NULL);
103
104 itor->_destroy(itor->_itor);
105 FREE(itor);
106 }