00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _MACE_HASH_SET_H
00034 #define _MACE_HASH_SET_H
00035
00036 #if __GNUC__ >= 3
00037 #include <ext/hash_set>
00038 #else
00039 #include <hash_set>
00040 #endif
00041
00042 #include "CollectionSerializers.h"
00043 #include "RandomUtil.h"
00044 #include "mace_constants.h"
00045 #include "StrUtilNamespace.h"
00046
00052 namespace mace {
00053
00059
00060
00064 template<class Key,
00065 class Serial = SerializeSet<Key>,
00066 class HashFcn = __gnu_cxx::hash<Key>,
00067 class EqualKey = std::equal_to<Key>,
00068 class Alloc = std::allocator<Key> >
00069 class hash_set : public __gnu_cxx::hash_set<Key, HashFcn, EqualKey, Alloc>, public virtual Serial, public PrintPrintable {
00070
00071 BOOST_CLASS_REQUIRE2(Serial, Serializer, boost, ConvertibleConcept);
00072
00073 public:
00074 typedef typename __gnu_cxx::hash_set<Key, HashFcn, EqualKey, Alloc> baseType;
00075 typedef typename baseType::iterator iterator;
00076 typedef typename baseType::const_iterator const_iterator;
00077 typedef typename baseType::size_type size_type;
00078
00079 virtual ~hash_set() { }
00080
00082 void clear() {
00083 baseType::clear();
00084 }
00085
00087 bool contains(const Key& k) const {
00088 return find(k) != this->end();
00089 }
00090
00091 void push_back(const Key& item) {
00092
00093 insert(item);
00094 }
00095
00096 size_t size() const {
00097 return baseType::size();
00098 }
00099
00101 const_iterator random() const {
00102 if(this->size()==0) { return this->end(); }
00103 int rand = RandomUtil::randInt(this->size());
00104 const_iterator i;
00105 int position;
00106 for(i = this->begin(), position=0; i != this->end() && position < rand; i++, position++);
00107 return i;
00108 }
00109
00111 const std::string& getTypeName() const {
00112 const char* types[] = { "Key", "HashFcn", "EqualKey", "Alloc", 0 };
00113 static const StrUtilNamespace::StdStringList myTypes = StrUtilNamespace::getTypeFromTemplate(__PRETTY_FUNCTION__, types);
00114 return myTypes[0];
00115 }
00116
00117 void print(std::ostream& out) const {
00118 if(mace::PRINT_TYPE_NAMES) {
00119 out << "mace::HashSet<" << getTypeName() << ">";
00120 }
00121 mace::printList(out, this->begin(), this->end());
00122 }
00123 void printState(std::ostream& out) const {
00124 if(mace::PRINT_TYPE_NAMES) {
00125 out << "mace::HashSet<" << getTypeName() << ">";
00126 }
00127 mace::printListState(out, this->begin(), this->end());
00128 }
00129
00130 private:
00131 class v_const_iterator : public _SerializeSet<Key>::const_iterator {
00132 private:
00133 const_iterator i;
00134 const_iterator end;
00135
00136 public:
00137 bool isValid() {
00138 return i != end;
00139 }
00140 void next() {
00141 ASSERT(i != end);
00142 i++;
00143 }
00144 const Key& getValue() {
00145 return *i;
00146 }
00147
00148 v_const_iterator(const_iterator beg, const_iterator end) : i(beg), end(end) {}
00149 ~v_const_iterator() {}
00150 };
00151
00152 protected:
00153 typename _SerializeSet<Key>::const_iterator* getIterator() const {
00154 return new v_const_iterator(this->begin(), this->end());
00155 }
00156 void add(const Key& k) {
00157 this->insert(k);
00158 }
00159
00160 };
00161
00164 }
00165 #endif // _MACE_HASH_SET_H