Show
Ignore:
Timestamp:
03/08/10 20:03:58 (6 months ago)
Author:
plasmahh
Message:
  • added some draft ideas
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/containers/include/hash_table.h

    r3143 r3144  
    229229}; 
    230230#endif 
     231 
     232// all of them should get a _templ variant that gets the NEXT parameter, and 
     233// one without, then we can use an mpl list of all of them that we chain 
     234// together with the NEXT ones. 
     235/** 
     236 * Tries to resolve collisions by linear probing, that is if slot[hash % mask] 
     237 * is occupied, the algorithm will use a loop for(i=1 to maxsteps) try 
     238 * slot[(hash+(i*stepsize) % mask] as the next slot. 
     239 * @tparam NEXT the type that is being used next to resolve collisions 
     240 * @tparam stepsize the amount to add in each iteration 
     241 * @tparam maxsteps the maximum number of iterations this resolver will try 
     242 * until giving up and passing work to the next one 
     243 */ 
     244template< 
     245class NEXT, 
     246size_t stepsize, 
     247size_t maxsteps 
     248> 
     249struct resolver_linear_probing 
     250{ 
     251        // DRAFT!!! 
     252        template<class ITEM_TYPE, class TABLE_TYPE, class INSERTER, class SLOT_SELECTOR> 
     253        bool operator()( size_t hsum, size_t mask, ITEM_TYPE& it, TABLE_TYPE& table ) 
     254        { 
     255                for( size_t i = 1; i <= maxsteps; ++i ) 
     256                { 
     257                        size_t hvalue = hsum + stepsize * i; 
     258                        size_t idx = SLOT_SELECTOR()(hvalue,mask); 
     259                        if( ! INSERTER::occupied(idx,table) ) 
     260                        { 
     261                                return INSERTER::insert(it,idx,table); 
     262                        } 
     263                } 
     264        } 
     265}; 
     266 
     267template< 
     268class NEXT, 
     269class polytype, 
     270size_t maxsteps, 
     271           // XXX Check if this technique shouldnt be use everywhere else where some float would be a nice idea 
     272size_t c1_n, 
     273size_t c2_n, 
     274size_t c1_d = 1, 
     275size_t c2_d = 1 
     276> 
     277struct resolver_quadratic_probing 
     278{ 
     279        // DRAFT!!! 
     280        template<class ITEM_TYPE, class TABLE_TYPE, class INSERTER, class SLOT_SELECTOR> 
     281        bool operator()( size_t hsum, size_t mask, ITEM_TYPE& it, TABLE_TYPE& table ) 
     282        { 
     283                polytype c1 = c1_n / c1_d; 
     284                polytype c2 = c2_n / c2_d; 
     285                for( size_t i = 1; i <= maxsteps; ++i ) 
     286                { 
     287                        size_t hvalue = hsum + c1 * i + c2 * i * i; 
     288                        size_t idx = SLOT_SELECTOR()(hvalue,mask); 
     289                        if( ! INSERTER::occupied(idx,table) ) 
     290                        { 
     291                                return INSERTER::insert(it,idx,table); 
     292                        } 
     293                } 
     294        } 
     295}; 
     296 
     297template< 
     298class NEXT, 
     299class OTHERHASH, 
     300size_t maxsteps 
     301> 
     302struct resolver_double_hashing 
     303{ 
     304        // DRAFT!!! 
     305        template<class ITEM_TYPE, class TABLE_TYPE, class INSERTER, class SLOT_SELECTOR> 
     306        bool operator()( size_t hsum, size_t mask, ITEM_TYPE& it, TABLE_TYPE& table ) 
     307        { 
     308                OTHERHASH hasher; 
     309                size_t extrahash = hasher(it); 
     310                for( size_t i = 1; i <= maxsteps; ++i ) 
     311                { 
     312                        size_t hvalue = hsum + i * extrahash; 
     313                        size_t idx = SLOT_SELECTOR()(hvalue,mask); 
     314                        if( ! INSERTER::occupied(idx,table) ) 
     315                        { 
     316                                return INSERTER::insert(it,idx,table); 
     317                        } 
     318                } 
     319        } 
     320}; 
     321 
     322template< 
     323class NEXT, 
     324class OTHERHASH 
     325> 
     326struct resolver_second_hashing 
     327{ 
     328        // DRAFT!!! 
     329        template<class ITEM_TYPE, class TABLE_TYPE, class INSERTER, class SLOT_SELECTOR> 
     330        bool operator()( size_t hsum, size_t mask, ITEM_TYPE& it, TABLE_TYPE& table ) 
     331        { 
     332                OTHERHASH hasher; 
     333                size_t hvalue = hasher(it); 
     334                size_t idx = SLOT_SELECTOR()(hvalue,mask); 
     335                if( ! INSERTER::occupied(idx,table) ) 
     336                { 
     337                        return INSERTER::insert(it,idx,table); 
     338                } 
     339        } 
     340}; 
     341 
     342 
    231343 
    232344struct slot_selector_modulo