hmap.h

00001 /* 
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. - All rights reserved.  
00003  */
00004 
00005 #ifndef _U_HMAP_H_
00006 #define _U_HMAP_H_
00007 
00008 #include <sys/types.h>
00009 #include <u/libu_conf.h>
00010 #include <u/toolbox/str.h>
00011 
00012 #ifdef __cplusplus
00013 extern "C" {
00014 #endif
00015 
00022 typedef enum {
00023     U_HMAP_ERR_NONE = 0,
00024     U_HMAP_ERR_EXISTS,
00025     U_HMAP_ERR_FAIL
00026 } u_hmap_ret_t;
00027 
00029 typedef enum {
00030     U_HMAP_TYPE_CHAIN = 0,  
00031     U_HMAP_TYPE_LINEAR,     
00032     U_HMAP_TYPE_LAST = U_HMAP_TYPE_LINEAR           
00033 } u_hmap_type_t;
00034 
00035 #define U_HMAP_IS_TYPE(t)   (t <= U_HMAP_TYPE_LAST)
00036 
00038 typedef enum {
00039     U_HMAP_OPTS_OWNSDATA =      0x1,    
00040     U_HMAP_OPTS_NO_OVERWRITE =  0x2,    
00041     U_HMAP_OPTS_HASH_STRONG =   0x4     
00042 } u_hmap_options_t;
00043 
00046 typedef enum {
00047     U_HMAP_OPTS_DATATYPE_POINTER = 0,   
00048     U_HMAP_OPTS_DATATYPE_STRING,        
00049     U_HMAP_OPTS_DATATYPE_OPAQUE,        
00050     U_HMAP_OPTS_DATATYPE_LAST = U_HMAP_OPTS_DATATYPE_OPAQUE
00051 } u_hmap_options_datatype_t;
00052 
00053 #define U_HMAP_IS_DATATYPE(t)   (t <= U_HMAP_OPTS_DATATYPE_LAST)
00054 
00056 typedef enum {
00057 /*
00058  * Legend:
00059  *
00060  *      ()      pointer
00061  *      {}      empty queue
00062  *      []      queue object
00063  *      ->,<-   push/pop direction
00064  *      n       element number
00065  *      t       time
00066  *      a       accesses
00067  *      p       priority
00068  */
00069     U_HMAP_PCY_NONE = 0,    
00071                             /* queue: -> (front) {} (back) */
00072 
00073     U_HMAP_PCY_FIFO,    
00074                         /* queue: -> (front) [n][n-1][n-2].. (back) ->  */
00075 
00076     U_HMAP_PCY_LRU,     
00077                         /* queue: -> (front) [tn][tn-1][tn-2].. (back) -> */
00078 
00079     U_HMAP_PCY_LFU,     
00080                         /* queue: <-> (front) ..[a-2][a-1][a] (back) */
00081 
00082     U_HMAP_PCY_CUSTOM,  
00083                         /* queue: -> (front) [p][p-1][p-2].. (back) -> */
00084 
00085     U_HMAP_PCY_LAST = U_HMAP_PCY_CUSTOM
00086 } u_hmap_pcy_type_t;
00087 
00088 #define U_HMAP_IS_PCY(p)    (p <= U_HMAP_PCY_LAST)
00089 
00090 typedef struct u_hmap_s u_hmap_t;     
00091 typedef struct u_hmap_pcy_s u_hmap_pcy_t;     
00092 typedef struct u_hmap_o_s u_hmap_o_t;     
00093 
00095 struct u_hmap_opts_s;
00096 typedef struct u_hmap_opts_s u_hmap_opts_t;
00097 
00098 /* [u_hmap_easy_*] - simplified interface */
00099 int u_hmap_easy_new (u_hmap_opts_t *opts, u_hmap_t **phmap);
00100 int u_hmap_easy_put (u_hmap_t *hmap, const char *key, const void *val); 
00101 void *u_hmap_easy_get (u_hmap_t *hmap, const char *key); 
00102 int u_hmap_easy_del (u_hmap_t *hmap, const char *key); 
00103 void u_hmap_easy_clear (u_hmap_t *hmap);
00104 void u_hmap_easy_free (u_hmap_t *hmap);
00105 
00106 /* [u_hmap_*] */
00107 int u_hmap_new (u_hmap_opts_t *opts, u_hmap_t **phmap);
00108 int u_hmap_put (u_hmap_t *hmap, u_hmap_o_t *obj, u_hmap_o_t **old);
00109 int u_hmap_get (u_hmap_t *hmap, const void *key, u_hmap_o_t **obj);
00110 int u_hmap_del (u_hmap_t *hmap, const void *key, u_hmap_o_t **obj);
00111 int u_hmap_copy (u_hmap_t *to, u_hmap_t *from);
00112 void u_hmap_clear (u_hmap_t *hmap);
00113 void u_hmap_free (u_hmap_t *hmap);
00114 int u_hmap_foreach (u_hmap_t *hmap, int f(const void *val));
00115 int u_hmap_foreach_keyval (u_hmap_t *hmap, int f(const void *key, 
00116             const void *val));
00117 int u_hmap_foreach_arg (u_hmap_t *hmap, int f(const void *val, 
00118             const void *arg), void *arg);
00119 ssize_t u_hmap_count (u_hmap_t *hmap);
00120 const char *u_hmap_strerror (u_hmap_ret_t);
00121 
00122 /* [u_hmap_o_*] */
00123 u_hmap_o_t *u_hmap_o_new (u_hmap_t *hmap, const void *key, const void *val);
00124 void *u_hmap_o_get_key (u_hmap_o_t *obj);
00125 void *u_hmap_o_get_val (u_hmap_o_t *obj);
00126 void u_hmap_o_free (u_hmap_o_t *obj);
00127 
00128 /* [u_hmap_opts_*] */
00129 int u_hmap_opts_new (u_hmap_opts_t **opts);
00130 void u_hmap_opts_init (u_hmap_opts_t *opts);
00131 /* for both simplified and normal interface */
00132 int u_hmap_opts_set_size (u_hmap_opts_t *opts, int sz);
00133 int u_hmap_opts_set_max (u_hmap_opts_t *opts, int max);
00134 int u_hmap_opts_set_type (u_hmap_opts_t *opts, u_hmap_type_t type);
00135 int u_hmap_opts_set_policy (u_hmap_opts_t *opts, u_hmap_pcy_type_t policy);
00136 int u_hmap_opts_set_policy_cmp (u_hmap_opts_t *opts,
00137         int (*f_pcy_cmp)(void *o1, void *o2));
00138 int u_hmap_opts_set_val_freefunc (u_hmap_opts_t *opts, 
00139         void (*f_free)(void *val));
00140 int u_hmap_opts_set_val_type (u_hmap_opts_t *opts, 
00141         u_hmap_options_datatype_t type);
00142 int u_hmap_opts_set_val_sz (u_hmap_opts_t *opts, size_t sz);
00143 int u_hmap_opts_copy (u_hmap_opts_t *to, u_hmap_opts_t *from);
00144 void u_hmap_opts_free (u_hmap_opts_t *opts);
00145 /* advanced options (invalid for simplified interface) */
00146 int u_hmap_opts_set_option (u_hmap_opts_t *opts, int option);
00147 int u_hmap_opts_unset_option (u_hmap_opts_t *opts, int option);
00148 int u_hmap_opts_set_hashfunc (u_hmap_opts_t *opts, 
00149         size_t (*f_hash)(const void *key, size_t buckets));
00150 int u_hmap_opts_set_compfunc (u_hmap_opts_t *opts, 
00151         int (*f_comp)(const void *k1, const void *k2));
00152 int u_hmap_opts_set_freefunc (u_hmap_opts_t *opts, 
00153         void (*f_free)(u_hmap_o_t *obj));
00154 int u_hmap_opts_set_strfunc (u_hmap_opts_t *opts, 
00155         u_string_t *(*f_str)(u_hmap_o_t *obj));
00156 int u_hmap_opts_set_key_type (u_hmap_opts_t *opts, 
00157         u_hmap_options_datatype_t type);
00158 int u_hmap_opts_set_key_sz (u_hmap_opts_t *opts, size_t sz);
00159 int u_hmap_opts_set_key_freefunc (u_hmap_opts_t *opts, 
00160         void (*f_free)(const void *key));
00161 
00162 /* testing */
00163 void u_hmap_dbg (u_hmap_t *hmap);
00164 void u_hmap_opts_dbg (u_hmap_opts_t *opts);
00165 
00170 void u_hmap_pcy_dbg (u_hmap_t *hmap);
00171 
00172 #ifdef __cplusplus
00173 }
00174 #endif
00175 
00176 #endif /* !_U_HMAP_H_ */

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved