Changeset 589

Show
Ignore:
Timestamp:
07/14/08 09:01:38 (4 months ago)
Author:
weyrick
Message:

hash work. move to unicode keys.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/rphp/runtime/rphp_hash.cpp

    r586 r589  
    1919#include "rphp_hash.h" 
    2020 
     21// hash function for rphp::ustring 
     22namespace icu_3_8 { 
     23    std::size_t hash_value(rphp::ustring const& s) { 
     24        return static_cast<std::size_t>(s.hashCode()); 
     25    } 
     26} 
     27 
    2128namespace rphp { 
    2229 
    23 void phash::insert(const bstring &key, pvar data) { 
     30void phash::insert(const ustring &key, pvar *data) { 
    2431 
    25     hashData->insert(_dataContainer(key, data)); 
     32    hashData.insert(h_container(key, data)); 
    2633 
    2734} 
     
    3037     
    3138 
    32     std::cout << "array(" << hashData->size() << ") {" << std::endl; 
     39    std::cout << "array(" << hashData.size() << ") {" << std::endl; 
    3340 
    34     seq_index& ot = get<1>(*hashData); 
     41    seq_index& ot = get<1>(hashData); 
    3542 
    3643    for (seq_index::iterator it = ot.begin(); it!=ot.end(); it++) { 
    37         std::cout << "   ['" << (*it).key << "'] => " << (*it).data << std::endl; 
     44        std::cout << "   ['" << (*it).key << "'] => " << *(*it).data << std::endl; 
    3845    } 
    3946 
  • trunk/rphp/runtime/rphp_hash.h

    r586 r589  
    2626#include <iostream> 
    2727 
    28 #include "cowptr.h" 
    2928#include "rphp_pvar.h" 
    3029 
     
    3231using namespace boost::multi_index; 
    3332 
     33// hash function for rphp::ustring 
     34namespace icu_3_8 { 
     35    std::size_t hash_value(rphp::ustring const& s); 
     36} 
     37 
    3438namespace rphp { 
    3539 
    36 struct _dataContainer { 
     40// container we store in our stable hash 
     41struct h_container { 
    3742 
    38     pvar data; 
    39     bstring key; 
     43    pvar *data; 
     44    const ustring key; 
    4045    bool isNumKey; 
    4146 
    42     _dataContainer(bstring k, pvar d) : data(d), key(k), isNumKey(false) { 
     47    h_container(const ustring k, pvar *d) : data(d), key(k), isNumKey(false) { 
    4348        // TODO: set isNumKey based on isNumeric check on key 
    4449        // we will need this to emulate their numeric keys 
     
    4954// a boost.multiindex that stores data with two indexes: hashed and sequenced 
    5055typedef multi_index_container< 
    51   _dataContainer, 
     56  // the container structure we store for each item in the hash 
     57  h_container, 
     58  // index definitions: hash and sequence 
    5259  indexed_by< 
    53     hashed_unique< member<_dataContainer, bstring, &_dataContainer::key> >, 
     60    hashed_unique< member<h_container, const ustring, &h_container::key> >, 
    5461    sequenced<> 
    5562  > 
     
    6168class phash { 
    6269    private: 
    63         CowPtr< stableHash > hashData; 
     70        stableHash hashData; 
    6471    public: 
    6572         
    66         phash() : hashData(new stableHash()) { std::cout << "creating fresh php_hash" << std::endl; } 
    67 /* 
     73        phash() { std::cout << "creating fresh phash" << std::endl; } 
     74 
    6875        phash(phash const& p) { 
    6976            std::cout << "phash copy construct" << std::endl; 
    7077            hashData = p.hashData; 
    71         }  
    72 */ 
    73         void insert(const bstring &key, pvar data); 
     78        } 
     79 
     80        void insert(const ustring &key, pvar *data); 
    7481 
    7582        void varDump(); 
    7683         
    77         int getSize() const { return hashData->size(); } 
     84        int getSize() const { return hashData.size(); } 
    7885         
    79         ~phash() { std::cout << "destorying php_hash" << std::endl; } 
     86        pvar* operator[] ( const ustring &key ) { 
     87            stableHash::iterator k = hashData.find(key); 
     88            if (k == hashData.end()) 
     89                return NULL; 
     90            else 
     91                return (*k).data; 
     92        } 
     93 
     94         
     95        ~phash() { std::cout << "destroying phash" << std::endl; } 
    8096 
    8197}; 
  • trunk/rphp/runtime/rphp_pvar.h

    r585 r589  
    5959 
    6060// base variant that represents a php variable 
    61 typedef boost::variant< p3state/*int*/, pint/*long*/, pfloat/*double*/, bstring, ustring, boost::recursive_wrapper< phash >/*, pobject*/> pvarBase; 
     61typedef boost::variant< p3state/*int*/, pint/*long*/, pfloat/*double*/, bstring, ustring, phash/*, pobject*/> pvarBase; 
    6262 
    6363// reference to a pvarBase, i.e. a container for pvar variables 
     
    6666 
    6767// full pvar definition: a variant that can hold a base type or a reference 
    68 typedef boost::variant< p3state/*int*/, pint/*long*/, pfloat/*double*/, bstring, ustring, boost::recursive_wrapper< phash >, /*pobject,*/ pvarRef> pvar; 
     68typedef boost::variant< p3state/*int*/, pint/*long*/, pfloat/*double*/, bstring, ustring, phash, /*pobject,*/ pvarRef> pvar; 
    6969 
    7070// associated enum for checking type 
  • trunk/rphp/runtime/test/phashTestCase.cpp

    r588 r589  
    1111void phashTestCase::basic() 
    1212{ 
    13     /* 
    14   CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, 1.1, 0.05 ); 
    15   CPPUNIT_ASSERT( 1 == 0 ); 
    16   CPPUNIT_ASSERT( 1 == 1 ); 
    17     */ 
    1813 
    19     rphp::pvar u
     14    rphp::pvar* int1 = new rphp::pvar(rphp::pint(971))
    2015 
    2116    // php hash 
    2217    rphp::phash h; 
    23     h.insert("var-test", rphp::pint(971)); 
    24     rphp::pvar hole = rphp::pfloat(1.234); 
    25     h.insert("var-test2", hole); 
    26     std::cout << h; 
     18    h.insert("var-test", int1); 
     19 
     20    //std::cout << *(h["var-test"]) << std::endl; 
     21    rphp::pvar* int2 = h["var-test"]; 
     22 
     23    CPPUNIT_ASSERT( pvar_getVal_int(*int1) == pvar_getVal_int(*int2) ); 
     24 
    2725    h.varDump(); 
    28     u = h; 
    29     std::cout << u << std::endl;     
    30      
     26 
    3127} 
    3228 
    3329/* 
    34 void phashTestCase::anotherExample() 
     30void phashTestCase::setUp() 
    3531{ 
    36   CPPUNIT_ASSERT (1 == 2); 
     32 
     33  m_value1 = 2.0; 
     34  m_value2 = 3.0; 
     35 
    3736} 
    3837*/ 
    39 void phashTestCase::setUp() 
    40 { 
    41     /* 
    42   m_value1 = 2.0; 
    43   m_value2 = 3.0; 
    44     */ 
    45 } 
    4638 
    47 /* 
    48 void phashTestCase::testAdd() 
    49 { 
    50   double result = m_value1 + m_value2; 
    51   CPPUNIT_ASSERT( result == 6.0 ); 
    52 } 
    53  
    54  
    55 void phashTestCase::testEquals() 
    56 { 
    57   long* l1 = new long(12); 
    58   long* l2 = new long(12); 
    59  
    60   CPPUNIT_ASSERT_EQUAL( 12, 12 ); 
    61   CPPUNIT_ASSERT_EQUAL( 12L, 12L ); 
    62   CPPUNIT_ASSERT_EQUAL( *l1, *l2 ); 
    63  
    64   delete l1; 
    65   delete l2; 
    66  
    67   CPPUNIT_ASSERT( 12L == 12L ); 
    68   CPPUNIT_ASSERT_EQUAL( 12, 13 ); 
    69   CPPUNIT_ASSERT_DOUBLES_EQUAL( 12.0, 11.99, 0.5 ); 
    70 } 
    71 */ 
  • trunk/rphp/runtime/test/phashTestCase.h

    r588 r589  
    1414  CPPUNIT_TEST_SUITE( phashTestCase ); 
    1515  CPPUNIT_TEST( basic ); 
    16   /* 
    17   CPPUNIT_TEST( anotherExample ); 
    18   CPPUNIT_TEST( testAdd ); 
    19   CPPUNIT_TEST( testEquals ); 
    20   */ 
    2116  CPPUNIT_TEST_SUITE_END(); 
    2217 
     
    2722*/ 
    2823 
     24/* 
    2925public: 
    3026  void setUp(); 
     27*/ 
    3128 
    3229protected: 
    3330  void basic(); 
    34   /* 
    35   void anotherExample(); 
    36   void testAdd(); 
    37   void testEquals(); 
    38   */ 
    3931}; 
    4032 
  • trunk/rphp/runtime/test/pvarTestCase.cpp

    r588 r589  
    138138 
    139139    // php hash 
     140    /* 
    140141    rphp::phash h; 
    141142    h.insert("var-test", rphp::pint(971)); 
     
    148149 
    149150    result = boost::apply_visitor( my_visitor(), u ); 
    150  
     151    */ 
    151152    //// 
    152153