Set_Map/Windows_Set_Map/Map.hpp

82 lines
1.8 KiB
C++
Raw Permalink Normal View History

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