1 /* libcomps - C alternative to yum.comps library
2  * Copyright (C) 2013 Jindrich Luza
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to  Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
17  * USA
18  */
19 
20 #ifndef COMPS_BRADIX_H
21 #define COMPS_BRADIX_H
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #include "comps_hslist.h"
26 
27 typedef struct {
28     void *key;
29     unsigned is_leaf;
30     COMPS_HSList *subnodes;
31     void *data;
32     void (*key_destroy)(void *key);
33     void (*data_destructor)(void*);
34 } COMPS_BRTreeData;
35 
36 typedef struct {
37     COMPS_HSList *subnodes;
38     void* (*data_constructor)(void*);
39     void* (*data_cloner)(void*);
40     void (*data_destructor)(void*);
41 
42     void* (*key_clone)(void *key, unsigned int len);
43     void (*key_destroy)(void *key);
44     unsigned int (*key_cmp)(void *key1, void *key2,
45                             unsigned int offset1,
46                             unsigned int offset2,
47                             unsigned int len,
48                             char *ended);
49     void* (*subkey)(void *key, unsigned int offset, unsigned int len);
50     unsigned int (*key_len)(void *key);
51     void* (*key_concat)(void*, void*);
52 } COMPS_BRTree;
53 
54 typedef struct {
55     void *key;
56     void *data;
57     void (*key_destroy)(void *key);
58 } COMPS_BRTreePair;
59 
60 COMPS_BRTreeData * __comps_brtree_data_create(COMPS_BRTree *rt, void *key,
61                                                    unsigned int keylen,
62                                                    void *data);
63 COMPS_HSList* __comps_brtree_all(COMPS_BRTree * rt, char pairorkey);
64 
65 void comps_brtree_data_destroy(COMPS_BRTreeData * rtd);
66 void comps_brtree_data_destroy_v(void * rtd);
67 
68 COMPS_BRTreeData * comps_brtree_data_create(COMPS_BRTree *rt, void *key,
69                                           void *data);
70 COMPS_BRTreeData * comps_brtree_data_create_n(COMPS_BRTree *rt, void *key,
71                                             unsigned int len,
72                                             void *data);
73 
74 COMPS_BRTree * comps_brtree_create(void* (*data_constructor)(void*),
75                                    void* (*data_cloner)(void*),
76                                    void (*data_destructor)(void*),
77                                    void* (*key_clone)(void*, unsigned int),
78                                    void (*key_destroy)(void*),
79                                    unsigned int (*key_cmp)(void*, void*,
80                                                            unsigned int,
81                                                            unsigned int,
82                                                            unsigned int, char*),
83                                    unsigned int (*key_len)(void*),
84                                    void* (*subkey)(void*,
85                                                    unsigned int,
86                                                    unsigned int),
87                                    void* (*key_concat)(void*, void*));
88 
89 void comps_brtree_destroy(COMPS_BRTree * rt);
90 
91 void comps_brtree_set(COMPS_BRTree *rt, void *key, void *data);
92 void comps_brtree_set_n(COMPS_BRTree *rt, void *key, unsigned int len,
93                        void *data);
94 
95 void* comps_brtree_get(COMPS_BRTree * rt, void * key);
96 void** comps_brtree_getp(COMPS_BRTree *brt, void *key);
97 void comps_brtree_unset(COMPS_BRTree * rt, void * key);
98 void comps_brtree_clear(COMPS_BRTree * rt);
99 
100 void comps_brtree_values_walk(COMPS_BRTree *rt, void* udata,
101                                                void (*walk_f)(void*, void*));
102 COMPS_HSList * comps_brtree_values(COMPS_BRTree *rt);
103 COMPS_HSList* comps_brtree_keys(COMPS_BRTree * rt);
104 COMPS_HSList* comps_brtree_pairs(COMPS_BRTree * rt);
105 COMPS_BRTree * comps_brtree_clone(COMPS_BRTree * rt);
106 
107 COMPS_BRTreePair * comps_brtree_pair_create(char * key, void * data,
108                                           void (*data_destructor(void*)));
109 void comps_brtree_pair_destroy(COMPS_BRTreePair * pair);
110 void comps_brtree_pair_destroy_v(void * pair);
111 
112 COMPS_HSList* __comps_brtree_all(COMPS_BRTree * rt, char pairorkey);
113 
114 #endif
115