IndexVectorHasher Struct Reference

`#include <nnet-common.h>`

## Public Member Functions

size_t operator() (const std::vector< Index > &index_vector) const noexcept

## Detailed Description

Definition at line 131 of file nnet-common.h.

## ◆ operator()()

 size_t operator() ( const std::vector< Index > & index_vector ) const
noexcept

Definition at line 388 of file nnet-common.cc.

389  {
390  size_t n1 = 15, n2 = 10; // n1 and n2 are used to extract only a subset of
391  // elements to hash; this makes the hasher faster by
392  // skipping over more elements. Setting n1 large or
393  // n2 to 1 would make the hasher consider all
394  // elements.
395  size_t len = index_vector.size();
396  // all long-ish numbers appearing below are randomly chosen primes.
397  size_t ans = 1433 + 34949 * len;
398  std::vector<Index>::const_iterator iter = index_vector.begin(),
399  end = index_vector.end(), med = end;
400  if (n1 < len)
401  med = iter + n1;
402
403  for (; iter != med; ++iter) {
404  ans += iter->n * 1619;
405  ans += iter->t * 15649;
406  ans += iter->x * 89809;
407  }
408  // after the first n1 values, look only at every n2'th value. this makes the
409  // hashing much faster, and in the kinds of structures that we actually deal
410  // with, we shouldn't get unnecessary hash collisions as a result of this
411  // optimization.
412  for (; iter < end; iter += n2) {
413  ans += iter->n * 1619;
414  ans += iter->t * 15649;
415  ans += iter->x * 89809;
416  // The following if-statement was introduced in order to fix an
417  // out-of-range iterator problem on Windows.
418  if (n2 > len || iter >= end - n2)
419  break;
420  }
421  return ans;
422 }

The documentation for this struct was generated from the following files: