| 1 |
#include <map> |
| 2 |
#include <algorithm> |
| 3 |
#include <functional> |
| 4 |
#include <iterator> |
| 5 |
|
| 6 |
/* |
| 7 |
// SF: |
| 8 |
// template <typename Key, typename Value> |
| 9 |
// class Dictionary; |
| 10 |
// |
| 11 |
// template <typename Key, typename Value> |
| 12 |
// Dictionary<Key, Value> |
| 13 |
// subset(Key const &key, Dictionary<Key, Value> const &dict); |
| 14 |
|
| 15 |
//FUNCTION |
| 16 |
template <typename Key, typename Value> |
| 17 |
class Dictionary |
| 18 |
{ |
| 19 |
friend Dictionary<Key, Value> |
| 20 |
subset<Key, Value>(Key const &key, |
| 21 |
Dictionary<Key, Value> const &dict); |
| 22 |
|
| 23 |
std::map<Key, Value> d_dict; |
| 24 |
public: |
| 25 |
Dictionary(); |
| 26 |
}; |
| 27 |
|
| 28 |
template <typename Key, typename Value> |
| 29 |
Dictionary<Key, Value> |
| 30 |
subset(Key const &key, Dictionary<Key, Value> const &dict) |
| 31 |
{ |
| 32 |
Dictionary<Key, Value> ret; |
| 33 |
|
| 34 |
std::remove_copy_if(dict.d_dict.begin(), dict.d_dict.end(), |
| 35 |
std::inserter(ret.d_dict, ret.d_dict.begin()), |
| 36 |
std::bind2nd(std::equal_to<Key>(), key)); |
| 37 |
return ret; |
| 38 |
} |
| 39 |
//= |
| 40 |
*/ |
| 41 |
//CLASS1 |
| 42 |
template <typename Key, typename Value> |
| 43 |
class Iterator; |
| 44 |
|
| 45 |
template <typename Key, typename Value> |
| 46 |
class Dictionary |
| 47 |
{ |
| 48 |
friend class Iterator<Key, Value>; |
| 49 |
//= |
| 50 |
std::map<Key, Value> d_dict; |
| 51 |
|
| 52 |
public: |
| 53 |
Dictionary(); |
| 54 |
|
| 55 |
// uses the friend's constructor |
| 56 |
template <typename Key2, typename Value2> |
| 57 |
Iterator<Key2, Value2> begin(); |
| 58 |
|
| 59 |
|
| 60 |
template <typename Key2, typename Value2> |
| 61 |
Iterator<Key2, Value2> // uses a member function |
| 62 |
subset(Key const &key); |
| 63 |
}; |
| 64 |
|
| 65 |
//CLASS2 |
| 66 |
template <typename Key, typename Value> |
| 67 |
template <typename Key2, typename Value2> |
| 68 |
Iterator<Key2, Value2> Dictionary<Key, Value>::begin() |
| 69 |
{ |
| 70 |
return Iterator<Key, Value>(*this); |
| 71 |
} |
| 72 |
template <typename Key, typename Value> |
| 73 |
template <typename Key2, typename Value2> |
| 74 |
Iterator<Key2, Value2> Dictionary<Key, Value>::subset(Key const &key) |
| 75 |
{ |
| 76 |
return Iterator<Key, Value>(*this).subset(key); |
| 77 |
} |
| 78 |
//= |
| 79 |
|
| 80 |
/* |
| 81 |
//CLASS3 |
| 82 |
template <typename Key, typename Value> |
| 83 |
class Iterator |
| 84 |
{ |
| 85 |
std::map<Key, Value> &d_dict; |
| 86 |
|
| 87 |
public: |
| 88 |
Iterator(Dictionary<Key, Value> &dict) |
| 89 |
: |
| 90 |
d_dict(dict.d_dict) |
| 91 |
{} |
| 92 |
//= |
| 93 |
*/ |
| 94 |
//CLASS3a |
| 95 |
template <typename Key, typename Value> |
| 96 |
class Iterator |
| 97 |
{ |
| 98 |
friend Dictionary<Key, Value>::Dictionary(); |
| 99 |
std::map<Key, Value> &d_dict; |
| 100 |
Iterator(Dictionary<Key, Value> &dict); |
| 101 |
public: |
| 102 |
//= |
| 103 |
typename std::map<Key, Value>::iterator begin(); |
| 104 |
typename std::map<Key, Value>::iterator subset(Key const &key); |
| 105 |
}; |
| 106 |
|
| 107 |
//CLASS4 |
| 108 |
template <typename Key, typename Value> |
| 109 |
typename std::map<Key, Value>::iterator Iterator<Key, Value>::begin() |
| 110 |
{ |
| 111 |
return d_dict.begin(); |
| 112 |
} |
| 113 |
//= |
| 114 |
|
| 115 |
template <typename Key, typename Value> |
| 116 |
Iterator<Key, Value>::Iterator(Dictionary<Key, Value> &dict) |
| 117 |
: |
| 118 |
d_dict(dict.d_dict) |
| 119 |
{} |
| 120 |
|
| 121 |
template <typename Key, typename Value> |
| 122 |
typename std::map<Key, Value>::iterator |
| 123 |
Iterator<Key, Value>::subset(Key const &key) |
| 124 |
{ |
| 125 |
return d_dict.begin(); |
| 126 |
} |