| | 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 | */ |
| | 244 | template< |
| | 245 | class NEXT, |
| | 246 | size_t stepsize, |
| | 247 | size_t maxsteps |
| | 248 | > |
| | 249 | struct 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 | |
| | 267 | template< |
| | 268 | class NEXT, |
| | 269 | class polytype, |
| | 270 | size_t maxsteps, |
| | 271 | // XXX Check if this technique shouldnt be use everywhere else where some float would be a nice idea |
| | 272 | size_t c1_n, |
| | 273 | size_t c2_n, |
| | 274 | size_t c1_d = 1, |
| | 275 | size_t c2_d = 1 |
| | 276 | > |
| | 277 | struct 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 | |
| | 297 | template< |
| | 298 | class NEXT, |
| | 299 | class OTHERHASH, |
| | 300 | size_t maxsteps |
| | 301 | > |
| | 302 | struct 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 | |
| | 322 | template< |
| | 323 | class NEXT, |
| | 324 | class OTHERHASH |
| | 325 | > |
| | 326 | struct 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 | |