Open bosthhe1 opened 1 year ago
set.h
#define _CRT_SECURE_NO_WARNINGS 1
#include"ListHash.h"
namespace hxh
{
template<class K>
class unordered_set
{
struct SetKeyOfT
{
const K& operator()(const K& key)
{
return key;
}
};
public:
const K& operator[](const K& key)
{
return *((ht.insert(key)).first);
}
typedef typename ListHash<K, K, SetKeyOfT, tmp<K>>::iterator Siterator;
Siterator begin()
{
return ht.begin();
}
Siterator end()
{
return ht.end();
}
pair<Siterator, bool> insert(const K& key)
{
return ht.insert(key);
}
private:
ListHash<K, K, SetKeyOfT,tmp<K>> ht;
};
}
map.h
#define _CRT_SECURE_NO_WARNINGS 1
#include"ListHash.h"
namespace hxh
{
template<class K, class V>
class unordered_map
{
struct MapKeyOfT
{
const K& operator()(const pair<K,V>& kv)
{
return kv.first;
}
};
public:
typedef typename ListHash<K, pair<K, V>, MapKeyOfT, tmp<K>>::iterator Miterator;
V& operator[](const pair<K, V>& _kv)
{
return (*((kv.insert(_kv)).first)).second;
}
Miterator begin()
{
return kv.begin();
}
Miterator end()
{
return kv.end();
}
pair<Miterator, bool> insert(const pair<K, V>& _kv)
{
return kv.insert(_kv);
}
private:
ListHash<K, pair<K, V>, MapKeyOfT,tmp<K>> kv;
};
}
由于线性插入和二次插入都会引发线性冲突和二次冲突,所以我选择的时以链表的形式