Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To create a hash function, you can use <a href="http://en.cppreference.com/w/cpp/utility/hash" rel="nofollow">std::hash</a>, which is defined for integers. Then, you can combine them "as the boost guys does" (because doing a good hash is something non trivial) as explained here : <a href="http://en.cppreference.com/w/cpp/utility/hash" rel="nofollow">http://en.cppreference.com/w/cpp/utility/hash</a>.</p> <p>Here is a hash_combine method :</p> <pre><code>inline void hash_combine(std::size_t&amp; seed, std::size_t v) { seed ^= v + 0x9e3779b9 + (seed &lt;&lt; 6) + (seed &gt;&gt; 2); } </code></pre> <p>So the "guideline" is more or less what's is shown on cppreference.</p> <p>You CAN'T be sure your hash function is colision free. Colision free means that you do not loose data (or you restrict yourself to a small set of possibilities for your class). If any int32 value is allowed for each fields, a collision free hash is a monstrously big index, and it won't fit in a small table. Let unordered_map take care of collisions, and combine std::hash hash as explained above.</p> <p>In you case, it will look something like</p> <pre><code>std::size_t hash() const { std::size_t h1 = std::hash&lt;CEditLine*&gt;()(editLine); //Your int32 type is probably a typedef of a hashable type. Otherwise, // you'll have to static_cast&lt;&gt; it to a type supported by std::hash. std::size_t h2 = std::hash&lt;int32&gt;()(nDisplayLine); std::size_t h3 = std::hash&lt;int32&gt;()(start); std::size_t h4 = std::hash&lt;int32&gt;()(len); std::size_t hash = 0; hash_combine(hash, h1); hash_combine(hash, h2); hash_combine(hash, h3); hash_combine(hash, h4); return hash; } </code></pre> <p>Then, you can specialize the std::hash operator for your class.</p> <pre><code>namespace std { template&lt;&gt; struct hash&lt;CacheKey&gt; { public: std::size_t operator()(CacheKey const&amp; s) const { return s.hash(); } }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload