2024-09-24 00:01:11 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "RBTree.hpp"
|
|
|
|
|
|
|
|
|
|
namespace Lenyiin
|
|
|
|
|
{
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// Map <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
template <class K, class V>
|
|
|
|
|
class Map
|
|
|
|
|
{
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// <20><><EFBFBD>ڴӽڵ<D3BD><DAB5>л<EFBFBD>ȡ<EFBFBD><C8A1>ֵ<EFBFBD>ķº<C4B7><C2BA><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
struct MapKeyOfT
|
|
|
|
|
{
|
|
|
|
|
const K& operator()(const std::pair<K, V>& kv)
|
|
|
|
|
{
|
2024-09-24 04:43:00 +08:00
|
|
|
|
return kv.first; // <20><><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ıȽ<C4B1><C8BD><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD>֧<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::iterator iterator;
|
|
|
|
|
typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::const_iterator const_iterator;
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD>صĵ<D8B5><C4B5><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
iterator begin()
|
|
|
|
|
{
|
|
|
|
|
return _tree.begin();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>β<EFBFBD><CEB2>λ<EFBFBD>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
iterator end()
|
|
|
|
|
{
|
|
|
|
|
return _tree.end();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// const <20>汾<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
const_iterator begin() const
|
|
|
|
|
{
|
|
|
|
|
return _tree.begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const_iterator end() const
|
|
|
|
|
{
|
|
|
|
|
return _tree.end();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
std::pair<iterator, bool> Insert(const std::pair<K, V>& kv)
|
|
|
|
|
{
|
|
|
|
|
return _tree.Insert(kv);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// ɾ<><C9BE>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD>ɹ<EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
bool Erase(const K& key)
|
|
|
|
|
{
|
|
|
|
|
return _tree.Erase(key);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ԫ<EFBFBD>صĵ<D8B5><C4B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
iterator Find(const K& key)
|
|
|
|
|
{
|
|
|
|
|
return _tree.Find(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// [] <20>±<EFBFBD><C2B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
V& operator[](const K& key)
|
|
|
|
|
{
|
|
|
|
|
std::pair<iterator, bool> ret = _tree.Insert(std::make_pair(key, V()));
|
|
|
|
|
return ret.first->second;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 04:43:00 +08:00
|
|
|
|
// У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺϷ<C4BA><CFB7><EFBFBD>
|
2024-09-24 00:01:11 +08:00
|
|
|
|
bool IsRBTree()
|
|
|
|
|
{
|
|
|
|
|
return _tree.IsRBTree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
RBTree<K, std::pair<K, V>, MapKeyOfT> _tree;
|
|
|
|
|
};
|
|
|
|
|
}
|