Ignore:
Timestamp:
Nov 9, 2005 3:37:19 PM (18 years ago)
Author:
bennylp
Message:

Rework pjlib++

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/main/pjlib/include/pj++/hash.hpp

    r29 r36  
    11/* $Id$ 
    2  * 
    32 */ 
    43#ifndef __PJPP_HASH_H__ 
     
    65 
    76#include <pj++/types.hpp> 
     7#include <pj++/pool.hpp> 
    88#include <pj/hash.h> 
    99 
    10 class PJ_Hash_Table 
     10// 
     11// Hash table. 
     12// 
     13class Pj_Hash_Table : public Pj_Object 
    1114{ 
    1215public: 
     16    // 
     17    // Hash table iterator. 
     18    // 
    1319    class iterator 
    1420    { 
    1521    public: 
    16         iterator() {} 
    17         explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i) : ht_(h), it_(i) {} 
    18         iterator(const iterator &rhs) : ht_(rhs.ht_), it_(rhs.it_) {} 
    19         void operator++() { it_ = pj_hash_next(ht_, it_); } 
    20         bool operator==(const iterator &rhs) { return ht_ == rhs.ht_ && it_ == rhs.it_; } 
    21         iterator & operator=(const iterator &rhs) { ht_=rhs.ht_; it_=rhs.it_; return *this; } 
     22        iterator()  
     23        { 
     24        } 
     25        explicit iterator(pj_hash_table_t *h, pj_hash_iterator_t *i)  
     26        : ht_(h), it_(i)  
     27        { 
     28        } 
     29        iterator(const iterator &rhs)  
     30        : ht_(rhs.ht_), it_(rhs.it_)  
     31        { 
     32        } 
     33        void operator++()  
     34        {  
     35            it_ = pj_hash_next(ht_, it_);  
     36        } 
     37        bool operator==(const iterator &rhs)  
     38        {  
     39            return ht_ == rhs.ht_ && it_ == rhs.it_;  
     40        } 
     41        iterator & operator=(const iterator &rhs)  
     42        {  
     43            ht_=rhs.ht_; it_=rhs.it_;  
     44            return *this;  
     45        } 
    2246    private: 
    2347        pj_hash_table_t *ht_; 
     
    2549        pj_hash_iterator_t *it_; 
    2650 
    27         friend class PJ_Hash_Table; 
     51        friend class Pj_Hash_Table; 
    2852    }; 
    2953 
    30     static PJ_Hash_Table *create(PJ_Pool *pool, unsigned size) 
     54    // 
     55    // Construct hash table. 
     56    // 
     57    Pj_Hash_Table(Pj_Pool *pool, unsigned size) 
    3158    { 
    32         return (PJ_Hash_Table*) pj_hash_create(pool->pool_(), size); 
     59        table_ = pj_hash_create(pool->pool_(), size); 
    3360    } 
    3461 
    35     static pj_uint32_t calc(pj_uint32_t initial_hval, const void *key, unsigned keylen) 
     62    // 
     63    // Destroy hash table. 
     64    // 
     65    ~Pj_Hash_Table() 
     66    { 
     67    } 
     68 
     69    // 
     70    // Calculate hash value. 
     71    // 
     72    static pj_uint32_t calc( pj_uint32_t initial_hval,  
     73                             const void *key,  
     74                             unsigned keylen = PJ_HASH_KEY_STRING) 
    3675    { 
    3776        return pj_hash_calc(initial_hval, key, keylen); 
    3877    } 
    3978 
    40     pj_hash_table_t *hash_table_() 
     79    // 
     80    // Return pjlib compatible hash table object. 
     81    // 
     82    pj_hash_table_t *pj_hash_table_t_() 
    4183    { 
    42         return (pj_hash_table_t*)this; 
     84        return table_; 
    4385    } 
    4486 
    45     void *get(const void *key, unsigned keylen) 
     87    // 
     88    // Get the value associated with the specified key. 
     89    // 
     90    void *get(const void *key, unsigned keylen = PJ_HASH_KEY_STRING) 
    4691    { 
    47         return pj_hash_get(this->hash_table_(), key, keylen); 
     92        return pj_hash_get(table_, key, keylen); 
    4893    } 
    4994 
    50     void set(PJ_Pool *pool, const void *key, unsigned keylen, void *value) 
     95    // 
     96    // Associate a value with a key. 
     97    // Set the value to NULL to delete the key from the hash table. 
     98    // 
     99    void set(Pj_Pool *pool,  
     100             const void *key,  
     101             void *value, 
     102             unsigned keylen = PJ_HASH_KEY_STRING) 
    51103    { 
    52         pj_hash_set(pool->pool_(), this->hash_table_(), key, keylen, value); 
     104        pj_hash_set(pool->pool_(), table_, key, keylen, value); 
    53105    } 
    54106 
     107    // 
     108    // Get number of items in the hash table. 
     109    // 
    55110    unsigned count() 
    56111    { 
    57         return pj_hash_count(this->hash_table_()); 
     112        return pj_hash_count(table_); 
    58113    } 
    59114 
     115    // 
     116    // Iterate hash table. 
     117    // 
    60118    iterator begin() 
    61119    { 
    62         iterator it(this->hash_table_(), NULL); 
    63         it.it_ = pj_hash_first(this->hash_table_(), &it.it_val_); 
     120        iterator it(table_, NULL); 
     121        it.it_ = pj_hash_first(table_, &it.it_val_); 
    64122        return it; 
    65123    } 
    66124 
     125    // 
     126    // End of items. 
     127    // 
    67128    iterator end() 
    68129    { 
    69         return iterator(this->hash_table_(), NULL); 
     130        return iterator(table_, NULL); 
    70131    } 
     132 
     133private: 
     134    pj_hash_table_t *table_; 
    71135}; 
    72136 
     137 
    73138#endif  /* __PJPP_HASH_H__ */ 
     139 
Note: See TracChangeset for help on using the changeset viewer.