xref: /aosp_15_r20/external/e2fsprogs/lib/support/dict.h (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * Dictionary Abstract Data Type
3*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1997 Kaz Kylheku <[email protected]>
4*6a54128fSAndroid Build Coastguard Worker  *
5*6a54128fSAndroid Build Coastguard Worker  * Free Software License:
6*6a54128fSAndroid Build Coastguard Worker  *
7*6a54128fSAndroid Build Coastguard Worker  * All rights are reserved by the author, with the following exceptions:
8*6a54128fSAndroid Build Coastguard Worker  * Permission is granted to freely reproduce and distribute this software,
9*6a54128fSAndroid Build Coastguard Worker  * possibly in exchange for a fee, provided that this copyright notice appears
10*6a54128fSAndroid Build Coastguard Worker  * intact. Permission is also granted to adapt this software to produce
11*6a54128fSAndroid Build Coastguard Worker  * derivative works, as long as the modified versions carry this copyright
12*6a54128fSAndroid Build Coastguard Worker  * notice and additional notices stating that the work has been modified.
13*6a54128fSAndroid Build Coastguard Worker  * This source code may be translated into executable form and incorporated
14*6a54128fSAndroid Build Coastguard Worker  * into proprietary software; there is no requirement for such software to
15*6a54128fSAndroid Build Coastguard Worker  * contain a copyright notice related to this source.
16*6a54128fSAndroid Build Coastguard Worker  *
17*6a54128fSAndroid Build Coastguard Worker  * $Id: dict.h,v 1.22.2.6 2000/11/13 01:36:44 kaz Exp $
18*6a54128fSAndroid Build Coastguard Worker  * $Name: kazlib_1_20 $
19*6a54128fSAndroid Build Coastguard Worker  * The work has been modified.
20*6a54128fSAndroid Build Coastguard Worker  */
21*6a54128fSAndroid Build Coastguard Worker 
22*6a54128fSAndroid Build Coastguard Worker #ifndef DICT_H
23*6a54128fSAndroid Build Coastguard Worker #define DICT_H
24*6a54128fSAndroid Build Coastguard Worker 
25*6a54128fSAndroid Build Coastguard Worker #include <limits.h>
26*6a54128fSAndroid Build Coastguard Worker #ifdef KAZLIB_SIDEEFFECT_DEBUG
27*6a54128fSAndroid Build Coastguard Worker #include "sfx.h"
28*6a54128fSAndroid Build Coastguard Worker #endif
29*6a54128fSAndroid Build Coastguard Worker 
30*6a54128fSAndroid Build Coastguard Worker /*
31*6a54128fSAndroid Build Coastguard Worker  * Blurb for inclusion into C++ translation units
32*6a54128fSAndroid Build Coastguard Worker  */
33*6a54128fSAndroid Build Coastguard Worker 
34*6a54128fSAndroid Build Coastguard Worker #ifdef __cplusplus
35*6a54128fSAndroid Build Coastguard Worker extern "C" {
36*6a54128fSAndroid Build Coastguard Worker #endif
37*6a54128fSAndroid Build Coastguard Worker 
38*6a54128fSAndroid Build Coastguard Worker typedef unsigned long dictcount_t;
39*6a54128fSAndroid Build Coastguard Worker #define DICTCOUNT_T_MAX ULONG_MAX
40*6a54128fSAndroid Build Coastguard Worker 
41*6a54128fSAndroid Build Coastguard Worker /*
42*6a54128fSAndroid Build Coastguard Worker  * The dictionary is implemented as a red-black tree
43*6a54128fSAndroid Build Coastguard Worker  */
44*6a54128fSAndroid Build Coastguard Worker 
45*6a54128fSAndroid Build Coastguard Worker typedef enum { dnode_red, dnode_black } dnode_color_t;
46*6a54128fSAndroid Build Coastguard Worker 
47*6a54128fSAndroid Build Coastguard Worker typedef struct dnode_t {
48*6a54128fSAndroid Build Coastguard Worker #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
49*6a54128fSAndroid Build Coastguard Worker     struct dnode_t *dict_left;
50*6a54128fSAndroid Build Coastguard Worker     struct dnode_t *dict_right;
51*6a54128fSAndroid Build Coastguard Worker     struct dnode_t *dict_parent;
52*6a54128fSAndroid Build Coastguard Worker     dnode_color_t dict_color;
53*6a54128fSAndroid Build Coastguard Worker     const void *dict_key;
54*6a54128fSAndroid Build Coastguard Worker     void *dict_data;
55*6a54128fSAndroid Build Coastguard Worker #else
56*6a54128fSAndroid Build Coastguard Worker     int dict_dummy;
57*6a54128fSAndroid Build Coastguard Worker #endif
58*6a54128fSAndroid Build Coastguard Worker } dnode_t;
59*6a54128fSAndroid Build Coastguard Worker 
60*6a54128fSAndroid Build Coastguard Worker typedef int (*dict_comp_t)(const void *, const void *, const void *);
61*6a54128fSAndroid Build Coastguard Worker typedef dnode_t *(*dnode_alloc_t)(void *);
62*6a54128fSAndroid Build Coastguard Worker typedef void (*dnode_free_t)(dnode_t *, void *);
63*6a54128fSAndroid Build Coastguard Worker 
64*6a54128fSAndroid Build Coastguard Worker typedef struct dict_t {
65*6a54128fSAndroid Build Coastguard Worker #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
66*6a54128fSAndroid Build Coastguard Worker     dnode_t dict_nilnode;
67*6a54128fSAndroid Build Coastguard Worker     dictcount_t dict_nodecount;
68*6a54128fSAndroid Build Coastguard Worker     dictcount_t dict_maxcount;
69*6a54128fSAndroid Build Coastguard Worker     dict_comp_t dict_compare;
70*6a54128fSAndroid Build Coastguard Worker     dnode_alloc_t dict_allocnode;
71*6a54128fSAndroid Build Coastguard Worker     dnode_free_t dict_freenode;
72*6a54128fSAndroid Build Coastguard Worker     void *dict_context;
73*6a54128fSAndroid Build Coastguard Worker     const void *cmp_ctx;
74*6a54128fSAndroid Build Coastguard Worker     int dict_dupes;
75*6a54128fSAndroid Build Coastguard Worker #else
76*6a54128fSAndroid Build Coastguard Worker     int dict_dummmy;
77*6a54128fSAndroid Build Coastguard Worker #endif
78*6a54128fSAndroid Build Coastguard Worker } dict_t;
79*6a54128fSAndroid Build Coastguard Worker 
80*6a54128fSAndroid Build Coastguard Worker typedef void (*dnode_process_t)(dict_t *, dnode_t *, void *);
81*6a54128fSAndroid Build Coastguard Worker 
82*6a54128fSAndroid Build Coastguard Worker typedef struct dict_load_t {
83*6a54128fSAndroid Build Coastguard Worker #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
84*6a54128fSAndroid Build Coastguard Worker     dict_t *dict_dictptr;
85*6a54128fSAndroid Build Coastguard Worker     dnode_t dict_nilnode;
86*6a54128fSAndroid Build Coastguard Worker #else
87*6a54128fSAndroid Build Coastguard Worker     int dict_dummmy;
88*6a54128fSAndroid Build Coastguard Worker #endif
89*6a54128fSAndroid Build Coastguard Worker } dict_load_t;
90*6a54128fSAndroid Build Coastguard Worker 
91*6a54128fSAndroid Build Coastguard Worker extern dict_t *dict_create(dictcount_t, dict_comp_t);
92*6a54128fSAndroid Build Coastguard Worker extern void dict_set_allocator(dict_t *, dnode_alloc_t, dnode_free_t, void *);
93*6a54128fSAndroid Build Coastguard Worker extern void dict_set_cmp_context(dict_t *, const void *);
94*6a54128fSAndroid Build Coastguard Worker extern void dict_destroy(dict_t *);
95*6a54128fSAndroid Build Coastguard Worker extern void dict_free_nodes(dict_t *);
96*6a54128fSAndroid Build Coastguard Worker extern void dict_free(dict_t *);
97*6a54128fSAndroid Build Coastguard Worker extern dict_t *dict_init(dict_t *, dictcount_t, dict_comp_t);
98*6a54128fSAndroid Build Coastguard Worker extern void dict_init_like(dict_t *, const dict_t *);
99*6a54128fSAndroid Build Coastguard Worker extern int dict_verify(dict_t *);
100*6a54128fSAndroid Build Coastguard Worker extern int dict_similar(const dict_t *, const dict_t *);
101*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_lookup(dict_t *, const void *);
102*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_lower_bound(dict_t *, const void *);
103*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_upper_bound(dict_t *, const void *);
104*6a54128fSAndroid Build Coastguard Worker extern void dict_insert(dict_t *, dnode_t *, const void *);
105*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_delete(dict_t *, dnode_t *);
106*6a54128fSAndroid Build Coastguard Worker extern int dict_alloc_insert(dict_t *, const void *, void *);
107*6a54128fSAndroid Build Coastguard Worker extern void dict_delete_free(dict_t *, dnode_t *);
108*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_first(dict_t *);
109*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_last(dict_t *);
110*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_next(dict_t *, dnode_t *);
111*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dict_prev(dict_t *, dnode_t *);
112*6a54128fSAndroid Build Coastguard Worker extern dictcount_t dict_count(dict_t *);
113*6a54128fSAndroid Build Coastguard Worker extern int dict_isempty(dict_t *);
114*6a54128fSAndroid Build Coastguard Worker extern int dict_isfull(dict_t *);
115*6a54128fSAndroid Build Coastguard Worker extern int dict_contains(dict_t *, dnode_t *);
116*6a54128fSAndroid Build Coastguard Worker extern void dict_allow_dupes(dict_t *);
117*6a54128fSAndroid Build Coastguard Worker extern int dnode_is_in_a_dict(dnode_t *);
118*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dnode_create(void *);
119*6a54128fSAndroid Build Coastguard Worker extern dnode_t *dnode_init(dnode_t *, void *);
120*6a54128fSAndroid Build Coastguard Worker extern void dnode_destroy(dnode_t *);
121*6a54128fSAndroid Build Coastguard Worker extern void *dnode_get(dnode_t *);
122*6a54128fSAndroid Build Coastguard Worker extern const void *dnode_getkey(dnode_t *);
123*6a54128fSAndroid Build Coastguard Worker extern void dnode_put(dnode_t *, void *);
124*6a54128fSAndroid Build Coastguard Worker extern void dict_process(dict_t *, void *, dnode_process_t);
125*6a54128fSAndroid Build Coastguard Worker extern void dict_load_begin(dict_load_t *, dict_t *);
126*6a54128fSAndroid Build Coastguard Worker extern void dict_load_next(dict_load_t *, dnode_t *, const void *);
127*6a54128fSAndroid Build Coastguard Worker extern void dict_load_end(dict_load_t *);
128*6a54128fSAndroid Build Coastguard Worker extern void dict_merge(dict_t *, dict_t *);
129*6a54128fSAndroid Build Coastguard Worker 
130*6a54128fSAndroid Build Coastguard Worker #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
131*6a54128fSAndroid Build Coastguard Worker #ifdef KAZLIB_SIDEEFFECT_DEBUG
132*6a54128fSAndroid Build Coastguard Worker #define dict_isfull(D) (SFX_CHECK(D)->dict_nodecount == (D)->dict_maxcount)
133*6a54128fSAndroid Build Coastguard Worker #else
134*6a54128fSAndroid Build Coastguard Worker #define dict_isfull(D) ((D)->dict_nodecount == (D)->dict_maxcount)
135*6a54128fSAndroid Build Coastguard Worker #endif
136*6a54128fSAndroid Build Coastguard Worker #define dict_count(D) ((D)->dict_nodecount)
137*6a54128fSAndroid Build Coastguard Worker #define dict_isempty(D) ((D)->dict_nodecount == 0)
138*6a54128fSAndroid Build Coastguard Worker #define dnode_get(N) ((N)->dict_data)
139*6a54128fSAndroid Build Coastguard Worker #define dnode_getkey(N) ((N)->dict_key)
140*6a54128fSAndroid Build Coastguard Worker #define dnode_put(N, X) ((N)->dict_data = (X))
141*6a54128fSAndroid Build Coastguard Worker #endif
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker #ifdef __cplusplus
144*6a54128fSAndroid Build Coastguard Worker }
145*6a54128fSAndroid Build Coastguard Worker #endif
146*6a54128fSAndroid Build Coastguard Worker 
147*6a54128fSAndroid Build Coastguard Worker #endif
148