00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef COMMON_SYMTAB_H
00021 #define COMMON_SYMTAB_H
00022
00023 #include <common/mem.h>
00024 #include <common/nstring.h>
00025
00026 class string_list_ty;
00027 class nstring_list;
00028
00040 class symtab_ty
00041 {
00042 public:
00047 ~symtab_ty();
00048
00057 symtab_ty(int suggested_size = 5);
00058
00063 size_t size() const { return hash_load; }
00064
00069 bool empty() const { return (hash_load == 0); }
00070
00078 void clear(void);
00079
00095 void *query(string_ty *key) const;
00096
00111 void *query(const nstring &key) const;
00112
00124 void *query(const nstring_list &keys) const;
00125
00141 string_ty *query_fuzzy(string_ty *key) const;
00142
00157 nstring query_fuzzy(const nstring &key) const;
00158
00176 void assign(string_ty *key, void *value);
00177
00194 void assign(const nstring &key, void *value);
00195
00212 void assign_push(string_ty *key, void *value);
00213
00229 void assign_push(const nstring &key, void *value);
00230
00244 void remove(string_ty *key);
00245
00258 void remove(const nstring &key);
00259
00273 void dump(const char *caption) const;
00274
00290 void keys(string_list_ty *result) const;
00291
00306 void keys(nstring_list &result) const;
00307
00308 typedef void (*callback_t)(const symtab_ty *stp, const nstring &key,
00309 void *data, void *arg);
00310
00323 void walk(callback_t func, void *arg) const;
00324
00325 typedef void (*reaper_t)(void *);
00326
00332 void set_reap(reaper_t func) { reap = func; }
00333
00338 bool valid() const;
00339
00340 private:
00355 void split(void);
00356
00360 reaper_t reap;
00361
00362 struct row_t
00363 {
00364 row_t() : data(0), overflow(0) { }
00365 nstring key;
00366 void *data;
00367 row_t *overflow;
00368 };
00369
00375 row_t **hash_table;
00376
00381 str_hash_ty hash_modulus;
00382
00389 str_hash_ty hash_mask;
00390
00395 str_hash_ty hash_load;
00396
00400 symtab_ty(const symtab_ty &);
00401
00405 symtab_ty &operator=(const symtab_ty &);
00406
00407 friend class symtab_iterator;
00408 };
00409
00410 inline symtab_ty *
00411 symtab_alloc(int n)
00412 {
00413
00414 return new symtab_ty(n);
00415 }
00416
00417 inline void
00418 symtab_free(symtab_ty *stp)
00419 {
00420
00421 delete stp;
00422 }
00423
00424 inline void *
00425 symtab_query(const symtab_ty *stp, string_ty *key)
00426 {
00427
00428 return stp->query(key);
00429 }
00430
00431 inline string_ty *
00432 symtab_query_fuzzy(const symtab_ty *stp, string_ty *key)
00433 {
00434
00435 return stp->query_fuzzy(key);
00436 }
00437
00438 inline void
00439 symtab_assign(symtab_ty *stp, string_ty *key, void *value)
00440 {
00441
00442 stp->assign(key, value);
00443 }
00444
00445 inline DEPRECATED void
00446 symtab_assign_push(symtab_ty *stp, string_ty *key, void *value)
00447 {
00448 stp->assign_push(key, value);
00449 }
00450
00451 inline void
00452 symtab_delete(symtab_ty *stp, string_ty *key)
00453 {
00454
00455 stp->remove(key);
00456 }
00457
00458 inline DEPRECATED void
00459 symtab_dump(const symtab_ty *stp, const char *caption)
00460 {
00461 stp->dump(caption);
00462 }
00463
00464 inline DEPRECATED void
00465 symtab_walk(const symtab_ty *stp, symtab_ty::callback_t func, void *arg)
00466 {
00467 stp->walk(func, arg);
00468 }
00469
00470 inline DEPRECATED void
00471 symtab_keys(const symtab_ty *stp, string_list_ty *result)
00472 {
00473 stp->keys(result);
00474 }
00475
00478 #endif // COMMON_SYMTAB_H