00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __HTDATA_H__
00022 #define __HTDATA_H__
00023
00024 #include "common.h"
00025
00026 #define ATOM_HT_DATA_UINT MAGICD("DAT\x00")
00027 #define ATOM_HT_DATA_UINT32 MAGICD("DAT\x01")
00028 #define ATOM_HT_DATA_MEM MAGICD("DAT\x02")
00029
00030 #define ATOM_HT_STREE MAGICD("DAT\x10")
00031 #define ATOM_HT_CLIST MAGICD("DAT\x11")
00032
00033 #define ATOM_COMPARE_KEYS_HT_DATA MAGICD("DAT\x20")
00034 #define ATOM_COMPARE_KEYS_INT MAGICD("DAT\x21")
00035 #define ATOM_COMPARE_KEYS_UINT MAGICD("DAT\x22")
00036
00037 typedef int (*compare_keys_func_ptr)(Object *key_a, Object *key_b);
00038
00039
00040
00041
00042 class ht_data_uint: public Object {
00043 public:
00044 uint value;
00045
00046 ht_data_uint(uint v = 0);
00047
00048 virtual int load(ht_object_stream *s);
00049 virtual void store(ht_object_stream *s);
00050 virtual OBJECT_ID object_id() const;
00051 };
00052
00053
00054
00055
00056 class ht_data_uint32: public Object {
00057 public:
00058 uint32 value;
00059
00060 ht_data_uint32(uint32 v = 0);
00061
00062 virtual int load(ht_object_stream *s);
00063 virtual void store(ht_object_stream *s);
00064 virtual OBJECT_ID object_id() const;
00065 };
00066
00067
00068
00069
00070 class ht_data_ptr: public Object {
00071 public:
00072 const void *value;
00073
00074 ht_data_ptr(const void *v=0);
00075 };
00076
00077
00078
00079
00080 class ht_data_mem: public Object {
00081 public:
00082 void *value;
00083 uint size;
00084
00085 ht_data_mem(const void *v = 0, uint size = 0);
00086 virtual ~ht_data_mem();
00087
00088 virtual int load(ht_object_stream *s);
00089 virtual void store(ht_object_stream *s);
00090 virtual OBJECT_ID object_id() const;
00091 };
00092
00093
00094
00095
00096 struct ht_tree_node {
00097 Object *key;
00098 Object *value;
00099 ht_tree_node *left, *right;
00100 };
00101
00102 class ht_tree: public Object {
00103 public:
00104 compare_keys_func_ptr compare_keys;
00105
00106 void init(compare_keys_func_ptr compare_keys);
00107 virtual void done();
00108 virtual void destroy();
00109
00110 virtual void balance();
00111 virtual uint count();
00112 virtual bool del(Object *key);
00113 virtual Object *enum_next(Object **value, Object *prevkey);
00114 virtual Object *enum_prev(Object **value, Object *nextkey);
00115 virtual Object *get(Object *key);
00116 virtual Object *get_insert(Object *key);
00117 virtual bool insert(Object *key, Object *value);
00118 virtual void set_compare_keys(compare_keys_func_ptr new_compare_keys);
00119 };
00120
00121
00122
00123
00124 class ht_stree: public ht_tree {
00125 public:
00126 ht_tree_node *root;
00127 uint node_count;
00128
00129 void enum_next_i(ht_tree_node *node, Object *prevkey, ht_tree_node **retv);
00130 void enum_prev_i(ht_tree_node *node, Object *nextkey, ht_tree_node **retv);
00131 void free_all(ht_tree_node *node);
00132 void free_skeleton(ht_tree_node *node);
00133 ht_tree_node *get_leftmost_node(ht_tree_node *node);
00134 bool get_node_and_parent(Object *key, ht_tree_node **node, ht_tree_node **parent_node, int *direction);
00135 ht_tree_node *get_node_i(Object *key);
00136 ht_tree_node *get_rightmost_node(ht_tree_node *node);
00137 void insert_ltable(ht_tree_node **node, ht_tree_node **start, ht_tree_node **end);
00138 virtual void populate_ltable(ht_tree_node ***ltable, ht_tree_node *node);
00139 public:
00140 void init(compare_keys_func_ptr compare_keys);
00141 virtual void done();
00142 virtual void destroy();
00143
00144 virtual void balance();
00145 virtual uint count();
00146 virtual bool del(Object *key);
00147 virtual void empty();
00148 virtual Object *enum_next(Object **value, Object *prevkey);
00149 virtual Object *enum_prev(Object **value, Object *nextkey);
00150 virtual Object *get(Object *key);
00151 virtual bool insert(Object *key, Object *value);
00152 virtual int load(ht_object_stream *s);
00153 virtual OBJECT_ID object_id() const;
00154 virtual void store(ht_object_stream *s);
00155 virtual void set_compare_keys(compare_keys_func_ptr new_compare_keys);
00156 };
00157
00158
00159
00160
00161
00162 #define DEFAULT_MAX_UB_DELETE 500
00163 #define DEFAULT_MAX_UB_INSERT 500
00164
00165 class ht_dtree: public ht_stree {
00166 protected:
00167 uint dead_node_count;
00168 uint ub_delete, max_ub_delete;
00169 uint ub_insert, max_ub_insert;
00170
00171 void hardcount(uint *nc, uint *dnc);
00172 virtual void populate_ltable(ht_tree_node ***ltable, ht_tree_node *node);
00173 virtual void populate_ltable_free_dead_nodes(ht_tree_node ***ltable, ht_tree_node *node);
00174 public:
00175 void init(compare_keys_func_ptr compare_keys, uint _max_ub_delete=DEFAULT_MAX_UB_DELETE, uint _max_ub_insert=DEFAULT_MAX_UB_INSERT);
00176 virtual void done();
00177
00178 virtual uint count();
00179 virtual bool del(Object *key);
00180 virtual Object *enum_next(Object **value, Object *prevkey);
00181 virtual Object *enum_prev(Object **value, Object *nextkey);
00182 virtual bool insert(Object *key, Object *value);
00183 virtual void set_compare_keys(compare_keys_func_ptr new_compare_keys);
00184 };
00185
00186
00187
00188
00189 #define LIST_UNDEFINED 0xffffffff
00190
00191 class ht_list: public Object {
00192 protected:
00193 compare_keys_func_ptr compare_keys;
00194 public:
00195 void init(compare_keys_func_ptr compare_keys=0);
00196 virtual void done();
00197 virtual void destroy();
00198
00199 virtual void append(Object *data);
00200 virtual uint count();
00201 void copy_to(uint i, uint count, ht_list *destlist);
00202 virtual ht_list *cut(uint i, uint count);
00203 virtual bool del(uint i);
00204 bool del_multiple(uint i, uint count);
00205 virtual void empty();
00206 virtual uint find(Object *data);
00207 virtual Object *get(uint i);
00208 virtual void insert(Object *data);
00209 virtual void insert_after(Object *data, uint i);
00210 virtual void insert_before(Object *data, uint i);
00211 virtual void move(uint source, uint dest);
00212 virtual void move_multiple(uint source, uint dest, uint count);
00213 virtual void prepend(Object *data);
00214 virtual Object *remove(uint i);
00215 bool remove_multiple(uint i, uint count);
00216 virtual bool set(uint i, Object *data);
00217 virtual bool sort();
00218 };
00219
00220
00221
00222
00223
00224 class ht_clist: public ht_list {
00225 protected:
00226 Object **items;
00227 uint c_size, c_entry_count;
00228 uint enum_pos;
00229
00230 void extend_list();
00231 void do_free(uint i);
00232 void do_remove(uint i);
00233
00234 public:
00235 void init(compare_keys_func_ptr compare_keys=0);
00236 virtual void done();
00237 virtual void destroy();
00238
00239 virtual void append(Object *data);
00240 virtual uint count();
00241 virtual ht_list *cut(uint i, uint count);
00242 virtual bool del(uint i);
00243 virtual Object *duplicate();
00244 virtual void empty();
00245 virtual uint find(Object *data);
00246 virtual Object *get(uint i);
00247 virtual void insert(Object *data);
00248 virtual void insert_after(Object *data, uint i);
00249 virtual void insert_before(Object *data, uint i);
00250 virtual int load(ht_object_stream *s);
00251 virtual void move(uint source, uint dest);
00252 virtual void move_multiple(uint source, uint dest, uint count);
00253 virtual OBJECT_ID object_id() const;
00254 virtual void prepend(Object *data);
00255 virtual Object *remove(uint i);
00256 virtual bool set(uint i, Object *data);
00257 virtual bool sort();
00258 virtual void store(ht_object_stream *s);
00259 };
00260
00261
00262
00263
00264 class ht_sorted_list: public ht_clist {
00265 public:
00266 void init(compare_keys_func_ptr compare_keys);
00267 virtual void done();
00268
00269 virtual void append(Object *data);
00270 virtual uint find(Object *data);
00271 virtual void insert(Object *data);
00272 virtual void insert_after(Object *data, uint i);
00273 virtual void insert_before(Object *data, uint i);
00274 virtual void move(uint source, uint dest);
00275 virtual void move_multiple(uint source, uint dest, uint count);
00276 virtual void prepend(Object *data);
00277 virtual bool set(uint i, Object *data);
00278 };
00279
00280
00281
00282
00283 class ht_stack: public ht_clist {
00284 public:
00285
00286 Object *pop();
00287 void push(Object *data);
00288 };
00289
00290
00291
00292
00293 class ht_queue: public ht_clist {
00294 public:
00295
00296 void enqueue(Object *data);
00297 Object *dequeue();
00298
00299 Object *pop();
00300 void push(Object *data);
00301 };
00302
00303 int compare_keys_ht_data(Object *key_a, Object *key_b);
00304 int compare_keys_int(Object *key_a, Object *key_b);
00305 int compare_keys_uint(Object *key_a, Object *key_b);
00306
00307
00308
00309
00310
00311 #define CS_SETSIZE 256
00312
00313 typedef struct char_set {
00314 unsigned char char_bits [((CS_SETSIZE) + 7) / 8];
00315 } char_set;
00316
00317 #define CS_SET(n, p) ((p)->char_bits[(n) / 8] |= (1 << ((n) & 7)))
00318 #define CS_CLR(n, p) ((p)->char_bits[(n) / 8] &= ~(1 << ((n) & 7)))
00319 #define CS_ISSET(n, p) ((p)->char_bits[(n) / 8] & (1 << ((n) & 7)))
00320 #define CS_ZERO(p) memset ((void *)(p), 0, sizeof (*(p)))
00321
00322
00323
00324
00325
00326 #define BITMAP(a0, a1, a2, a3, a4, a5, a6, a7) (((a0)<<0) | ((a1)<<1) | ((a2)<<2) | ((a3)<<3) | ((a4)<<4) | ((a5)<<5) | ((a6)<<6) | ((a7)<<7))
00327
00328 #define BITBIT(bitmap, p) ((bitmap)>>(p)&1)
00329
00330
00331
00332
00333
00334 struct int_hash {
00335 int value;
00336 char *desc;
00337 };
00338
00339 char *matchhash(int value, int_hash *hash_table);
00340
00341
00342
00343
00344
00345 bool init_data();
00346
00347
00348
00349
00350
00351 void done_data();
00352
00353 #endif