1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
template <class KeyType, class ValueType, class HF, class SF>
struct StdMapCR
{
std::vector<std::map<KeyType,ValueType, typename SF::Less> > data;
bool Get(KeyType key, ValueType & val) { /*...*/ }
void Put(KeyType key, ValueType val) { /*...*/ }
void Resize(unsigned long new_size) { /*...*/ }
//...
};
template <class KeyType, class ValueType, class HF, class SF>
struct StdListCR
{
std::vector<std::list<std::pair<KeyType, ValueType> > > data;
bool Get(KeyType key, ValueType & val) { /*...*/ }
void Put(KeyType key, ValueType val) { /*...*/ }
void Resize(unsigned long new_size) { /*...*/ }
//...
};
template <class KeyType> struct HashFunction;
template<>
struct HashFunction<std::string>
{
struct Hash
{
unsigned long operator()(const std::string & key, unsigned long size) { /*...*/ }
};
};
template <class KeyType>
struct SearchFunctions
{
struct Equal { bool operator()(KeyTypev1, KeyTypev2) { return v1==v2; } };
struct Less { bool operator()(KeyTypev1, KeyTypev2) { return v1<v2; } };
};
template <
class KeyType, class ValueType,
template <class, class, class, class> class CRPolicy = StdListCR,
template <class> class HF = HashFunction,
template <class> class SF = SearchFunctions
>
class HashMap : public CRPolicy<KeyType, ValueType, HF<KeyType>, SF<KeyType> >
{
//...
};
//====================================================================================
struct Point3D { int x, y, z; };
template <>
struct HashFunction<Point3D>
{
struct Hash
{
unsigned long operator()(const Point3D & key, unsigned long size) { /*...*/ }
};
};
template <>
struct SearchFunctions<Point3D>
{
struct Equal { bool operator()(const Point3D & v1, const Point3D & v2) { /*...*/ } };
struct Less { bool operator()(const Point3D & v1, const Point3D & v2) { /*...*/ } };
};
//====================================================================================
int main()
{
HashMap <std::string, int, StdListCR> hmap1;
HashMap <Point3D, double, StdMapCR > hmap2;
//...
return 0;
}
|