先把代码发出来,大家先看,有什么不懂得都可以私信问我。
这块的迭代器是比较复杂,需要仔细看看。
博客内容:代码补全
作 者:陈大大陈
个人简介:一个正在努力学技术的准C++后端工程师,专注基础和实战分享 ,欢迎私信!
欢迎大家:这里是CSDN,我总结知识和写笔记的地方,喜欢的话请三连,有问题请私信
目录
HashTable.h
MyUnorderedSet.h
MyUnorderedMap.h
test.cpp
#pragma once
#include
#include
using namespace std;
//HashFunc
template
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
// 11:46继续
//HashFunc
template<>
struct HashFunc
{
size_t operator()(const string& key)
{
// BKDR
size_t hash = 0;
for (auto e : key)
{
hash *= 31;
hash += e;
}
cout << key << ":" << hash << endl;
return hash;
}
};
namespace open_address
{
enum Status
{
EMPTY,
EXIST,
DELETE
};
template
struct HashData
{
pair _kv;
Status _s; //状态
};
//struct HashFuncString
//{
// size_t operator()(const string& key)
// {
// // BKDR
// size_t hash = 0;
// for (auto e : key)
// {
// hash *= 31;
// hash += e;
// }
// cout << key << ":" << hash << endl;
// return hash;
// }
//};
template>
class HashTable
{
public:
HashTable()
{
_tables.resize(10);
}
bool Insert(const pair& kv)
{
if (Find(kv.first))
return false;
// 负载因子0.7就扩容
if (_n * 10 / _tables.size() == 7)
{
size_t newSize = _tables.size() * 2;
HashTable newHT;
newHT._tables.resize(newSize);
// 遍历旧表
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i]._s == EXIST)
{
newHT.Insert(_tables[i]._kv);
}
}
_tables.swap(newHT._tables);
}
Hash hf;
// 线性探测
size_t hashi = hf(kv.first) % _tables.size();
while (_tables[hashi]._s == EXIST)
{
hashi++;
hashi %= _tables.size();
}
_tables[hashi]._kv = kv;
_tables[hashi]._s = EXIST;
++_n;
return true;
}
HashData* Find(const K& key)
{
Hash hf;
size_t hashi = hf(key) % _tables.size();
while (_tables[hashi]._s != EMPTY)
{
if (_tables[hashi]._s == EXIST
&& _tables[hashi]._kv.first == key)
{
return &_tables[hashi];
}
hashi++;
hashi %= _tables.size();
}
return NULL;
}
// 伪删除法
bool Erase(const K& key)
{
HashData* ret = Find(key);
if (ret)
{
ret->_s = DELETE;
--_n;
return true;
}
else
{
return false;
}
}
void Print()
{
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i]._s == EXIST)
{
//printf("[%d]->%d\n", i, _tables[i]._kv.first);
cout << "[" << i << "]->" << _tables[i]._kv.first << ":" << _tables[i]._kv.second << endl;
}
else if (_tables[i]._s == EMPTY)
{
printf("[%d]->\n", i);
}
else
{
printf("[%d]->D\n", i);
}
}
cout << endl;
}
private:
vector> _tables;
size_t _n = 0; // 存储的关键字的个数
};
void TestHT1()
{
HashTable ht;
int a[] = { 4,14,24,34,5,7,1 };
for (auto e : a)
{
ht.Insert(make_pair(e, e));
}
ht.Insert(make_pair(3, 3));
ht.Insert(make_pair(3, 3));
ht.Insert(make_pair(-3, -3));
ht.Print();
ht.Erase(3);
ht.Print();
if (ht.Find(3))
{
cout << "3存在" << endl;
}
else
{
cout << "3不存在" << endl;
}
ht.Insert(make_pair(3, 3));
ht.Insert(make_pair(23, 3));
ht.Print();
}
void TestHT2()
{
string arr[] = { "香蕉", "甜瓜","苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
//HashTable ht;
HashTable ht;
for (auto& e : arr)
{
//auto ret = ht.Find(e);
HashData* ret = ht.Find(e);
if (ret)
{
ret->_kv.second++;
}
else
{
ht.Insert(make_pair(e, 1));
}
}
ht.Print();
ht.Insert(make_pair("apple", 1));
ht.Insert(make_pair("sort", 1));
ht.Insert(make_pair("abc", 1));
ht.Insert(make_pair("acb", 1));
ht.Insert(make_pair("aad", 1));
ht.Print();
}
}
namespace hash_bucket
{
template
struct HashNode
{
HashNode* _next;
T _data;
HashNode(const T& data)
:_data(data)
, _next(nullptr)
{}
};
template
class HashTable;
template
struct __HTiterator
{
typedef HashNode Node;
typedef __HTiterator Self;
Node* _node;
const HashTable* _pht;//pointer of table
size_t _hashi;
__HTiterator(Node* node,HashTable*pht,size_t hashi)
:_node(node)
,_pht(pht)
,_hashi(hashi)
{
}
__HTiterator(Node* node,const HashTable* pht, size_t hashi)
:_node(node)
, _pht(pht)
, _hashi(hashi)
{
}
Self&operator++()
{
if (_node->_next)
{
//如果当前桶还有节点,走向下一个节点
_node = _node->_next;
}
else
{
//当前桶走完了,找下一个桶
/*KeyOfT kot;
Hash hf;
size_t hashi = hf(kot(_node->_data)) % _pht._tables.size();*/
++_hashi;
while (_hashi < _pht->_tables.size())
{
if (_pht->_tables[_hashi])
{
_node = _pht->_tables[_hashi];
break;
}
++_hashi;
}
if (_hashi == _pht->_tables.size())
{
_node = nullptr;
}
}
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &_node->_data;
}
};
template
class HashTable
{
typedef HashNode Node;
template
friend struct __HTiterator;
public:
typedef __HTiterator iterator;
typedef __HTiterator const_iterator;
iterator begin()
{
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i])
{
return iterator(_tables[i],this, i);//this就是哈希表的指针
}
}
return end();
}
iterator end()
{
return iterator(nullptr, this, -1);
}
const_iterator begin() const
{
for (size_t i = 0; i < _tables.size(); i++)
{
if (_tables[i])
{
return const_iterator(_tables[i], this, i);//this就是哈希表的指针
}
}
return end();
}
//this->HashTable* _pht const
const_iterator end() const
{
return const_iterator(nullptr, this, -1);
}
HashTable()
{
_tables.resize(10);
}
~HashTable()
{
for (size_t i = 0; i < _tables.size(); i++)
{
Node* cur = _tables[i];
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_tables[i] = nullptr;
}
}
pair Insert(const T &data)
{
Hash hf;
KeyOfT kot;
iterator it = Find(kot(data));
if (it != end())
return make_pair(it, false);
//if (Find(kot(data))) return false;
// 负载因子最大到1
//if (_n*10 / _tables.size() == 7)
if (_n == _tables.size())
{
//size_t newSize = _tables.size() * 2;
//HashTable newHT;
//newHT._tables.resize(newSize);
遍历旧表
//for (size_t i = 0; i < _tables.size(); i++)
//{
// Node* cur = _tables[i];
// while(cur)
// {
// newHT.Insert(cur->_kv);
// cur = cur->_next;
// }
//}
//_tables.swap(newHT._tables);
vector newTables;
newTables.resize(_tables.size() * 2, nullptr);
// 遍历旧表
for (size_t i = 0; i < _tables.size(); i++)
{
Node* cur = _tables[i];
while (cur)
{
Node* next = cur->_next;
// 挪动到映射的新表
size_t hashi = hf(kot(cur->_data)) % newTables.size();
cur->_next = newTables[i];
newTables[i] = cur;
cur = next;
}
_tables[i] = nullptr;
}
_tables.swap(newTables);
}
size_t hashi = hf(kot(data)) % _tables.size();
Node* newnode = new Node(data);
// 头插
newnode->_next = _tables[hashi];
_tables[hashi] = newnode;
++_n;
return make_pair(iterator(newnode,this, hashi), true);
}
iterator Find(const K& key)
{
Hash hf;
KeyOfT kot;
size_t hashi = hf(key) % _tables.size();
Node* cur = _tables[hashi];
while (cur)
{
if (kot(cur->_data) == key)
{
return iterator(cur,this,hashi);
}
cur = cur->_next;
}
return end();
}
// 15:45继续
bool Erase(const T& data)
{
Hash hf;
KeyOfT kot;
size_t hashi = hf(kot(data)) % _tables.size();
Node* prev = nullptr;
Node* cur = _tables[hashi];
while (cur)
{
if (cur->_data == data)
{
if (prev == nullptr)
{
_tables[hashi] = cur->_next;
}
else
{
prev->_next = cur->_next;
}
delete cur;
return true;
}
prev = cur;
cur = cur->_next;
}
return false;
}
void Some()
{
size_t bucketSize = 0;
size_t maxBucketLen = 0;
size_t sum = 0;
double averageBucketLen = 0;
for (size_t i = 0; i < _tables.size(); i++)
{
Node* cur = _tables[i];
if (cur)
{
++bucketSize;
}
size_t bucketLen = 0;
while (cur)
{
++bucketLen;
cur = cur->_next;
}
sum += bucketLen;
if (bucketLen > maxBucketLen)
{
maxBucketLen = bucketLen;
}
}
averageBucketLen = (double)sum / (double)bucketSize;
printf("all bucketSize:%d\n", _tables.size());
printf("bucketSize:%d\n", bucketSize);
printf("maxBucketLen:%d\n", maxBucketLen);
printf("averageBucketLen:%lf\n\n", averageBucketLen);
}
private:
vector _tables;
size_t _n = 0;
list _linklist;
};
}
#pragma once
#include"HashTable.h"
namespace bit
{
template>
class unordered_set
{
struct SetKeyOfT
{
const K& operator()(const K& Key)
{
return Key;
}
};
public:
typedef typename hash_bucket::HashTable::const_iterator iterator;
typedef typename hash_bucket::HashTable::const_iterator const_iterator;
pair insert(const K& key)
{
auto ret = _ht.Insert(key);
return pair(const_iterator(ret.first._node, ret.first._pht, ret.first._hashi), ret.second);
}
/*iterator begin()
{
return _ht.begin();
}
iterator end()
{
return _ht.end();
}*/
const_iterator begin() const
{
return _ht.begin();
}
const_iterator end() const
{
return _ht.end();
}
private:
hash_bucket::HashTable _ht;
};
void test_set()
{
unordered_set us;
us.insert(5);
us.insert(15);
us.insert(32);
us.insert(3);
unordered_set::iterator it = us.begin();
while (it != us.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
for (auto e : us)
{
cout << e << ' ';
}
cout << endl;
}
}
#pragma once
#include"HashTable.h"
namespace bit
{
template>
class unordered_map
{
struct MapKeyOfT
{
const K& operator()(const pair& kv)
{
return kv.first;
}
};
public:
typedef typename hash_bucket::HashTable, MapKeyOfT, Hash>::iterator iterator;
typedef typename hash_bucket::HashTable, MapKeyOfT, Hash>::const_iterator const_iterator;
iterator begin()
{
return _ht.begin();
}
iterator end()
{
return _ht.end();
}
const_iterator begin() const
{
return _ht.begin();
}
const_iterator end() const
{
return _ht.end();
}
pair insert(const pair& kv)
{
return _ht.Insert(kv);
}
V& operator[](const K& key)
{
pair ret = _ht.Insert(make_pair(key, V()));
return ret.first->second;
}
private:
hash_bucket::HashTable, MapKeyOfT,Hash> _ht;
};
void test_map()
{
unordered_map dict;
dict.insert(make_pair("sort", ""));
dict.insert(make_pair("string", "ַ"));
dict.insert(make_pair("insert", ""));
for (auto& kv : dict)
{
//kv.first += 'x';
kv.second += 'x';
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
string arr[] = { "橘子", "苹果","香蕉", "香蕉", "橘子", "橘子", "可乐", "砂糖橘" };
unordered_map count_map;
for (auto& e : arr)
{
count_map[e]++;
}
for (auto& kv : count_map)
{
cout << kv.first << ":" << kv.second << endl;
}
cout << endl;
}
}
#include
#include
#include
#include
#include