Friday 15 March 2013

c++ - How can I use a custom type for keys in a boost::unordered_map? -


I am currently using the implementation of the hash map boost, and I want to implement a custom type For the key I am trying to do, I have four unsigned integers which I want to combine in a 128-bit datatype to use as a key.

I have created a straight with a 32-bit integer array of four elements, which acts as my storage, to be honest, I'm not sure how Boost's Hush map works , So I'm not sure what I'm doing here, but I follow Boost documentation () to promote: hash, and I've created a hash function, as well as a custom comparison operator.

I have this type of header defined in my header: This is my code:

  #ifndef INT128_H_ #define INT128_H_ // Custom 128-bit datatype is a weak hash operation The results are used to store and compare. Struct int128 {unsigned full storage [4]; / * Assignment operation that takes 32-bit integer array of four elements. This works a lower and less painful operation of values. * / Zero operator = (Suspicious unsigned int input [4]) {for (int i = 0; i <4; i ++) Storage [i] = input [i]; }}; Bull Operator == (int128 const & amp; o1, int128 const & amp; o2) {if1.storage [0] == o2.storage [0] & amp; O1.storage [1] == o2.storage [1] & amp; Amp; O1.storage [2] == o2.storage [2] & amp; O1.storage [3] == o2.storage [3]) True; return false; } // hash function to work int128 with hash boost. Std :: size_t hash_value (int128 const & input) {boost :: hash & lt; Unsigned long & gt; Aquatic machine; Unsigned long time = input Storage [0]; For (Int i = 1; I  

Now when I actually use this in Boost's unmodified map, my code is compiled, but fails to link Linker claims that my Many symbols have been defined several times in the object files. I really want to work my 128-bit type with this map. Do I have to screw up, or is there a better way to do this but any suggestions?

The unorod-map is almost casual about the problem you are facing, the real problem is That you are defining the hash_value and operator == in each file that contains the header.

You can either treat it either

  1. define both as inline function
  2. just Declare them in the header

If you do later (and this is what you usually want) you define those functions as a .cpp Files (or extensions used for C ++ source files). You will then compile that file and link the resulting object with your second code which uses the int128 type.

EDIT: You can still make your comparison cleaner, like something:

  BULL operator == (int128 const & amp; o1, int128 const & amp; O2) {Return o1.storage [0] == o2.storage [0] & amp; Amp; O1.storage [1] == o2.storage [1] & amp; O1.storage [2] == o2.storage [2] & amp; Amp; O1.storage [3] == o2.storage [3]); }  

No comments:

Post a Comment