sorted_hash.h

Go to the documentation of this file.
00001 #ifndef _SORTED_HASH_H
00002 #define _SORTED_HASH_H
00003 
00017 #include "common.h"
00018 
00019 __BEGIN_DECLS
00020 
00021 #include "collection.h"
00022 #include "vector.h"
00023 #include "mempool.h"
00024 
00025 struct _cp_sorted_hash;
00026 
00027 #define SH_RED    0
00028 #define SH_BLACK  1
00029 
00030 typedef CPROPS_DLL struct _cp_sh_entry
00031 {
00032     void *key;
00033     void *value;
00034 
00035     unsigned long code; /* hash code */
00036 
00037     /* balance maintainance - color is either 'red' or 'black' */
00038     int color;
00039 
00040     struct _cp_sh_entry *bucket;    /* list of nodes w/ same hash code */
00041     struct _cp_sh_entry *multiple;  /* list of nodes w/ same compare value */
00042     struct _cp_sh_entry *multiple_prev; /* reverse multiple list for removal */
00043 
00044     struct _cp_sh_entry *left;
00045     struct _cp_sh_entry *right;
00046     struct _cp_sh_entry *up;
00047 } cp_sh_entry;
00048 
00049 /* (internal) allocate a new node */
00050 CPROPS_DLL
00051 cp_sh_entry *cp_sh_entry_create(void *key, void *value, cp_mempool *pool);
00052 /* (internal) deallocate a node */
00053 CPROPS_DLL
00054 void cp_sorted_hash_destroy_entry(struct _cp_sorted_hash *owner, 
00055                                   cp_sh_entry *entry);
00056 /* (internal) deallocate a node and its subnodes */
00057 CPROPS_DLL
00058 void cp_sorted_hash_destroy_entry_deep(struct _cp_sorted_hash *owner, 
00059                                        cp_sh_entry *entry);
00060 
00061 /* tree wrapper object */
00062 typedef CPROPS_DLL struct _cp_sorted_hash
00063 {
00064     cp_sh_entry *root;               /* root node */
00065     cp_sh_entry **table;             /* entry table */
00066     int size;                        /* entry table size */
00067     
00068     int items;                       /* item count */
00069 
00070     int mode;                        /* mode flags */
00071     cp_hashfunction hash;            /* hash function */
00072     cp_compare_fn cmp_key;           /* key comparison function */
00073     cp_mapping_cmp_fn cmp_mapping;   /* item comparison function */
00074     cp_copy_fn key_copy;             /* key copy function */
00075     cp_destructor_fn key_dtr;        /* key destructor */
00076     cp_copy_fn value_copy;           /* value copy function */
00077     cp_destructor_fn value_dtr;      /* value destructor */
00078 
00079     cp_lock *lock;
00080     cp_thread txowner;               /* set if a transaction is in progress */
00081     int txtype;                      /* lock type */
00082 
00083     cp_mempool *mempool;             /* optional memory pool */
00084 } cp_sorted_hash;
00085 
00086 /* 
00087  * default create function - equivalent to create_by_option with mode 
00088  * COLLECTION_MODE_NOSYNC
00089  */
00090 CPROPS_DLL
00091 cp_sorted_hash *cp_sorted_hash_create(cp_hashfunction hash, 
00092                                       cp_compare_fn cmp_key, 
00093                                       cp_mapping_cmp_fn cmp_mapping);
00094 /*
00095  * complete parameter create function. Note that setting COLLECTION_MODE_COPY
00096  * without specifying a copy function for either keys or values will result in
00097  * keys or values respectively being inserted by value, with no copying 
00098  * performed. Similarly, setting COLLECTION_MODE_DEEP without specifying a 
00099  * destructor function for keys or values will result in no destructor call
00100  * for keys or values respectively. This allows using the copy/deep mechanisms
00101  * for keys only, values only or both.
00102  */
00103 CPROPS_DLL
00104 cp_sorted_hash *
00105     cp_sorted_hash_create_by_option(int mode, 
00106                                     unsigned long size_hint,
00107                                     cp_hashfunction hash, 
00108                                     cp_compare_fn cmp_key, 
00109                                     cp_mapping_cmp_fn cmp_mapping, 
00110                                     cp_copy_fn key_copy, 
00111                                     cp_destructor_fn key_dtr,
00112                                     cp_copy_fn val_copy, 
00113                                     cp_destructor_fn val_dtr);
00114 
00115 /* 
00116  * recursively destroy the tree structure 
00117  */
00118 CPROPS_DLL
00119 void cp_sorted_hash_destroy(cp_sorted_hash *tree);
00120 /*
00121  * recursively destroy the tree structure with the given destructor functions
00122  */
00123 CPROPS_DLL
00124 void cp_sorted_hash_destroy_custom(cp_sorted_hash *tree, 
00125                                    cp_destructor_fn key_dtr,
00126                                    cp_destructor_fn val_dtr);
00127 
00128 /* insertion function */
00129 CPROPS_DLL
00130 void *cp_sorted_hash_insert(cp_sorted_hash *tree, void *key, void *value);
00131 
00132 /* retrieve the value mapped to the given key */
00133 CPROPS_DLL
00134 void *cp_sorted_hash_get(cp_sorted_hash *tree, void *key);
00135 
00136 /* find the value of the closest key by operator */
00137 CPROPS_DLL
00138 void *cp_sorted_hash_find(cp_sorted_hash *tree, cp_mapping *mapping, cp_op op);
00139 
00140 /* return non-zero if a mapping for 'key' could be found */
00141 CPROPS_DLL
00142 int cp_sorted_hash_contains(cp_sorted_hash *tree, void *key);
00143 
00144 /* delete a mapping */
00145 CPROPS_DLL
00146 void *cp_sorted_hash_delete(cp_sorted_hash *tree, void *key);
00147 
00148 /* 
00149  * perform a pre-order iteration over the tree, calling 'callback' on each 
00150  * node
00151  */
00152 CPROPS_DLL
00153 int cp_sorted_hash_callback_preorder(cp_sorted_hash *tree, 
00154                                      cp_callback_fn callback, 
00155                                      void *prm);
00156 /* 
00157  * perform an in-order iteration over the tree, calling 'callback' on each 
00158  * node
00159  */
00160 CPROPS_DLL
00161 int cp_sorted_hash_callback(cp_sorted_hash *tree, 
00162                             cp_callback_fn callback, 
00163                             void *prm);
00164 /* 
00165  * perform a post-order iteration over the tree, calling 'callback' on each 
00166  * node
00167  */
00168 
00169 CPROPS_DLL
00170 int cp_sorted_hash_callback_postorder(cp_sorted_hash *tree, 
00171                                       cp_callback_fn callback, 
00172                                       void *prm);
00173 
00174 /* return the number of mappings in the tree */
00175 CPROPS_DLL
00176 int cp_sorted_hash_count(cp_sorted_hash *tree);
00177 
00178 /* 
00179  * lock tree for reading or writing as specified by type parameter. 
00180  */
00181 CPROPS_DLL
00182 int cp_sorted_hash_lock(cp_sorted_hash *tree, int type);
00183 /* read lock */
00184 #define cp_sorted_hash_rdlock(tree) (cp_sorted_hash_lock((tree), COLLECTION_LOCK_READ))
00185 /* write lock */
00186 #define cp_sorted_hash_wrlock(tree) (cp_sorted_hash_lock((tree), COLLECTION_LOCK_WRITE))
00187 /* unlock */
00188 CPROPS_DLL
00189 int cp_sorted_hash_unlock(cp_sorted_hash *tree);
00190 
00191 
00192 /* return the table mode indicator */
00193 CPROPS_DLL
00194 int cp_sorted_hash_get_mode(cp_sorted_hash *tree);
00195 /* set mode bits on the tree mode indicator */
00196 CPROPS_DLL
00197 int cp_sorted_hash_set_mode(cp_sorted_hash *tree, int mode);
00198 /* unset mode bits on the tree mode indicator. if unsetting 
00199  * COLLECTION_MODE_NOSYNC and the tree was not previously synchronized, the 
00200  * internal synchronization structure is initalized.
00201  */
00202 CPROPS_DLL
00203 int cp_sorted_hash_unset_mode(cp_sorted_hash *tree, int mode);
00204 
00205 
00207 CPROPS_DLL
00208 void cp_sorted_hash_dump(cp_sorted_hash *tree);
00209 
00210 __END_DECLS
00211 
00214 #endif
00215 

Generated on Mon Dec 5 23:00:22 2011 for cprops by  doxygen 1.4.7