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