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_SET_H
00034 #define _MACE_SET_H
00035
00036 #include <set>
00037
00038 #include "CollectionSerializers.h"
00039 #include "RandomUtil.h"
00040 #include "mace_constants.h"
00041 #include "StrUtilNamespace.h"
00042
00048 namespace mace {
00049
00055
00056
00060 template<class Key, class Serial = SerializeSet<Key>, class LessFcn = std::less<Key>, class Alloc = std::allocator<Key> >
00061 class set : public std::set<Key, LessFcn, Alloc>, public virtual Serial, public PrintPrintable {
00062
00063 BOOST_CLASS_REQUIRE2(Serial, Serializer, boost, ConvertibleConcept);
00064
00065 public:
00066 typedef typename std::set<Key, LessFcn, Alloc> baseType;
00067 typedef typename baseType::iterator iterator;
00068 typedef typename baseType::const_iterator const_iterator;
00069 typedef typename baseType::size_type size_type;
00070
00071 virtual ~set() { }
00072
00074 void clear() {
00075 baseType::clear();
00076 }
00077
00079 size_t size() const {
00080 return baseType::size();
00081 }
00082
00084 bool contains(const Key& k) const {
00085 return baseType::find(k) != this->end();
00086 }
00088 bool containsKey(const Key& k) const {
00089 return contains(k);
00090 }
00092 bool contains(const baseType& other) const {
00093 typename baseType::const_iterator myi = this->begin();
00094 typename baseType::const_iterator i = other.begin();
00095 while (i != other.end() && myi != this->end()) {
00096 if (*myi < *i) {
00097 myi++;
00098 }
00099 else if (*myi == *i) {
00100 myi++;
00101 i++;
00102 }
00103 else {
00104 return false;
00105 }
00106 }
00107 return i == other.end();
00108 }
00109
00110 void push_back(const Key& item) {
00111
00112 insert(item);
00113 }
00114
00116 const_iterator random() const {
00117 if(this->size()==0) { return this->end(); }
00118 int rand = RandomUtil::randInt(this->size());
00119 const_iterator i;
00120 int position;
00121 for(i = this->begin(), position=0; i != this->end() && position < rand; i++, position++);
00122 return i;
00123 }
00124
00126 const std::string& getTypeName() const {
00127 const char* types[] = { "Key", "Serial", "LessFcn", "Alloc", 0 };
00128 static const StrUtilNamespace::StdStringList myTypes = StrUtilNamespace::getTypeFromTemplate(__PRETTY_FUNCTION__, types);
00129 return myTypes[0];
00130 }
00131
00132 void print(std::ostream& out) const {
00133 if(mace::PRINT_TYPE_NAMES) {
00134 out << "mace::Set<" << getTypeName() << ">";
00135 }
00136 mace::printList(out, this->begin(), this->end());
00137 }
00138
00139 void print(PrintNode& pr, const std::string& name) const {
00140 mace::printList(pr, name, "set<" + getTypeName() + ">", this->begin(),
00141 this->end());
00142 }
00143
00144 void printState(std::ostream& out) const {
00145 if(mace::PRINT_TYPE_NAMES) {
00146 out << "mace::Set<" << getTypeName() << ">";
00147 }
00148 mace::printListState(out, this->begin(), this->end());
00149 }
00150
00151 private:
00152 class v_const_iterator : public _SerializeSet<Key>::const_iterator {
00153 private:
00154 const_iterator i;
00155 const_iterator end;
00156
00157 public:
00158 bool isValid() {
00159 return i != end;
00160 }
00161 void next() {
00162 ASSERT(i != end);
00163 i++;
00164 }
00165 const Key& getValue() {
00166 return *i;
00167 }
00168
00169 v_const_iterator(const_iterator beg, const_iterator end) : i(beg), end(end) {}
00170 ~v_const_iterator() {}
00171 };
00172
00173 protected:
00174 typename _SerializeSet<Key>::const_iterator* getIterator() const {
00175 return new v_const_iterator(this->begin(), this->end());
00176 }
00177 void add(const Key& k) {
00178 this->insert(k);
00179 }
00180
00181 };
00182
00185 }
00186 #endif // _MACE_HASH_SET_H