うまく使いこなせる方法のカスタムタイプキーを押し上げ::unordered_map?
-
11-09-2019 - |
質問
私が使っているブの実施のハッシュマップをプロジェクト、現在のようにしていを実施するカスタムタイプのための鍵となります。いつの符号無し整数型あるい合は単一の128ビットのデータ型を使用しています。
私がstruct32ビット整数の配列の要素であるとして保管します。正直、すごくなった方向のハッシュマップの作品でお使いいただくことによってしかったので、良かったがって、ブドキュメント(http://www.boost.org/doc/libs/1_37_0/doc/html/hash/custom.html 用延長ブー::hash、作成したハッシュ関数などのカスタム比較演算子です。
私はこのカスタムタイプで定義されるヘッダを表します。これが私のコード:
#ifndef INT128_H_
#define INT128_H_
// Custom 128-bit datatype used to store and compare the results of a weakened hash operation.
struct int128
{
unsigned int storage[4];
/* Assignment operation that takes a 32-bit integer array of four elements.
This makes assignment of values a shorter and less painful operation. */
void operator=(const unsigned int input[4])
{
for(int i = 0; i < 4; i++)
storage[i] = input[i];
}
};
bool operator==(int128 const &o1, int128 const &o2)
{
if(o1.storage[0] == o2.storage[0] && o1.storage[1] == o2.storage[1] &&
o1.storage[2] == o2.storage[2] && o1.storage[3] == o2.storage[3])
return true;
return false;
}
// Hash function to make int128 work with boost::hash.
std::size_t hash_value(int128 const &input)
{
boost::hash<unsigned long long> hasher;
unsigned long long hashVal = input.storage[0];
for(int i = 1; i < 3; i++)
{
hashVal *= 37;
hashVal += input.storage[1];
}
return hasher(hashVal);
}
#endif
現在実際にはこのタブの順序付けマップ、マコード統が失敗します。リンカーの債権そのシンボルの定義を複数回数オブジェクトファイルです。のように私の128ビットセンターにおけるこの地図です。他のヒント何か私はビス止め、より良い方法だ。
解決
の関与のもとで順序付けマップはほとんどに付随する問題だけに出会.問題があることだけを定義する hash_value
や operator==
各ファイルのヘッダです。
できる治療するには:
- を定義するものと、インライン機能
- で宣言しているヘッダー
う場合には後者(とうばたまの定義の機能 .cpp
ファイル(またはず拡張子を使用のためのC++ソースファイル).ましてそのファイルをコンパイルやリンクのオブジェクトとその他のコードを使用するint128タイプです。
編集:できるものごとの比較清浄機のようなもの:
bool operator==(int128 const &o1, int128 const &o2)
{
return o1.storage[0] == o2.storage[0] && o1.storage[1] == o2.storage[1] &&
o1.storage[2] == o2.storage[2] && o1.storage[3] == o2.storage[3]);
}
他のヒント
リンカは、私は、シンボルを持っていると主張しています いくつかの中に定義された複数回 オブジェクトファイルます。
inline
として、あなたの関数を宣言