我尝试创建将std::string
映射到std::string
的哈希映射,到目前为止,我使用了以下代码:
template<typename TKey, typename TValue>
struct lockfree_hash_map_traits_t
: public cds::container::split_list::type_traits
{
typedef cds::container::michael_list_tag ordered_list ; // what type of ordered list we want to use
typedef std::hash<TKey> hash ; // hash functor for the key stored in split-list map
// Type traits for our MichaelList class
struct ordered_list_traits: public cds::container::michael_list::type_traits {
typedef std::less<TKey> less; // comparer that specifies order of list nodes
};
};
template<typename TKey, typename TValue>
class lockfree_hash_map_t {
public:
typedef
cds::container::SplitListMap<cds::gc::HP, TKey, TValue, lockfree_hash_map_traits_t<TKey, TValue> >
base_hash_map;
// ... some stuff
private:
base_hash_map _map;
};
它基于 libcds 文档。但我不确定我是否正确使用哈希映射...根据描述SplitListMap的论文基础列表应按键的哈希排序,但文档建议使用 std::less<TKey>
来指定 Michael List 顺序。使用std::less<std::string>
正确吗?
这是哈希映射的通常做法。哈希函数可以为不同的键生成一个哈希代码。它被称为碰撞。当我们搜索一个键时,我们计算一个键的哈希值,在映射中搜索这个哈希值(通常哈希值只是哈希表中的一个索引),然后将找到的项目的键与我们的键进行比较。所以std::less是必要的。