Changeset 602

Show
Ignore:
Timestamp:
07/22/08 07:18:37 (4 months ago)
Author:
weyrick
Message:

hash

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rphp/runtime/include/pHash.h

    r601 r602  
    3434// custom key type 
    3535namespace rphp { 
    36     typedef boost::variant< pInt, pBString, pUString > hKeyVar; 
    37     typedef enum { hKeyInt, hKeyBStr, hKeyUStr  } hKeyType; 
     36    typedef boost::variant< pInt, /*pBString,*/ pUString > hKeyVar; 
     37    typedef enum { hKeyInt, /*hKeyBStr,*/ hKeyUStr  } hKeyType; 
    3838} 
    3939 
     
    5353            return static_cast<std::size_t>(k); 
    5454        } 
    55          
     55 
     56        /* 
    5657        std::size_t operator()(const pBString &k) const { 
    5758            return boost::hash_value(k); 
    5859        } 
     60        */ 
    5961 
    6062        std::size_t operator()(const pUString &k) const { 
     
    7274            return hKeyInt; 
    7375        } 
    74          
     76 
     77        /* 
    7578        hKeyType operator()(const pBString &k) const { 
    7679            return hKeyBStr; 
    7780        } 
     81        */ 
    7882 
    7983        hKeyType operator()(const pUString &k) const { 
     
    9195        h_container(const pUString k, pVarP d) : pData(d), key(k) { } 
    9296         
    93         h_container(const pBString k, pVarP d) : pData(d), key(k) { } 
     97//        h_container(const pBString k, pVarP d) : pData(d), key(k) { } 
    9498 
    9599        h_container(const pInt k, pVarP d) : pData(d), key(k) { } 
     
    138142            // modifiers 
    139143            void insert(const pUString &key, pVarP data); 
     144            //void insert(const pBString &key, pVarP data); 
    140145            void insert(const pInt &key, pVarP data); 
    141146            void insertNext(pVarP data); 
     147             
     148            size_type remove(const pUString &key); 
     149            //void remove(const pBString &key); 
     150            size_type remove(const pInt &key); 
    142151 
    143             // size 
     152            // queries 
    144153            const size_type getSize() { return hashData.size(); } 
     154            const bool keyExists(const pUString &key); 
     155            //const bool keyExists(const pBString &key); 
     156            const bool keyExists(const pInt &key); 
    145157 
    146158            // dump of contents 
     
    148160 
    149161            // lookup 
    150             pVarP operator[] ( const pUString &key ) { 
    151                 stableHash::iterator k = hashData.find(key); 
    152                 if (k == hashData.end()) 
    153                     return pVarP(); 
    154                 else 
    155                     return (*k).pData; 
    156             } 
    157             pVarP operator[] ( const pInt &key ) { 
    158                 stableHash::iterator k = hashData.find(key); 
    159                 if (k == hashData.end()) 
    160                     return pVarP(); 
    161                 else 
    162                     return (*k).pData; 
    163             } 
     162            pVarP operator[] (const pUString &key); 
     163            //pVarP operator[] (const pBString &key); 
     164            pVarP operator[] (const pInt &key); 
    164165 
    165166 
  • trunk/rphp/runtime/pHash.cpp

    r599 r602  
    3434    } 
    3535 
     36    /* 
     37    void pHash::insert(const pBString &key, pVarP data) { 
     38        // TODO check numeric string, set maxIntKey accordingly 
     39        hashData.insert(h_container(key, data)); 
     40    } 
     41    */ 
     42 
    3643    void pHash::insert(const pInt &key, pVarP data) { 
    3744        if (key > maxIntKey) 
     
    4350        hashData.insert(h_container(maxIntKey++, data)); 
    4451    } 
     52 
     53    pHash::size_type pHash::remove(const pUString &key) { 
     54        return hashData.erase(key); 
     55    } 
    4556     
     57    pHash::size_type pHash::remove(const pInt &key) { 
     58        return hashData.erase(key); 
     59    } 
     60 
     61     
     62    // query 
     63    const bool pHash::keyExists(const pUString &key) { 
     64        stableHash::iterator k = hashData.find(key); 
     65        return (k != hashData.end()); 
     66    } 
     67    /* 
     68    const bool pHash::keyExists(const pBString &key) { 
     69        stableHash::iterator k = hashData.find(key); 
     70        return (k != hashData.end()); 
     71    } 
     72    */ 
     73    const bool pHash::keyExists(const pInt &key) { 
     74        stableHash::iterator k = hashData.find(key); 
     75        return (k != hashData.end()); 
     76    } 
     77     
     78    // lookup 
     79    pVarP pHash::operator[] ( const pUString &key ) { 
     80        stableHash::iterator k = hashData.find(key); 
     81        if (k == hashData.end()) 
     82            return pVarP(); 
     83        else 
     84            return (*k).pData; 
     85    } 
     86 
     87    /* 
     88    pVarP pHash::operator[] ( const pBString &key ) { 
     89        stableHash::iterator k = hashData.find(key); 
     90        if (k == hashData.end()) 
     91            return pVarP(); 
     92        else 
     93            return (*k).pData; 
     94    } 
     95    */ 
     96     
     97    pVarP pHash::operator[] ( const pInt &key ) { 
     98        stableHash::iterator k = hashData.find(key); 
     99        if (k == hashData.end()) 
     100            return pVarP(); 
     101        else 
     102            return (*k).pData; 
     103    } 
     104 
     105    // output 
     106    std::ostream& operator << (std::ostream& os, const rphp::pHash& h) 
     107    { 
     108        return os << "php_hash:" << std::endl; 
     109    } 
     110 
    46111    void pHash::varDump() { 
    47112 
     
    65130    } 
    66131 
    67     std::ostream& operator << (std::ostream& os, const rphp::pHash& h) 
    68     { 
    69         return os << "php_hash:" << std::endl; 
    70     } 
    71  
     132     
    72133} /* namespace rphp */ 
    73134 
  • trunk/rphp/runtime/test/phashTestCase.cpp

    r600 r602  
    1313 
    1414    rphp::pVarP lu; 
     15 
     16    // ** INSERT ** 
    1517 
    1618    // string key 
     
    2931    CPPUNIT_ASSERT( testHash.getSize() == 3 ); 
    3032 
    31     //testHash.varDump(); 
    32  
     33    // ** LOOKUP ** 
     34     
    3335    // string key 
    3436    lu = testHash["var-test"]; 
     
    4749    CPPUNIT_ASSERT( lu.get() == 0 ); 
    4850 
     51    // ** REMOVE ** 
     52    rphp::pHash::size_type rc = testHash.remove("var-test"); 
     53    CPPUNIT_ASSERT( rc == 1 ); 
     54    CPPUNIT_ASSERT( testHash.getSize() == 2 ); 
     55     
     56    rc = testHash.remove("doesn't exist"); 
     57    CPPUNIT_ASSERT( rc == 0 ); 
     58    CPPUNIT_ASSERT( testHash.getSize() == 2 ); 
     59     
     60    testHash.varDump(); 
     61     
    4962} 
    5063