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 #include <vector>
00034 #include <iostream>
00035 #include "Serializable.h"
00036 #include "XmlRpcCollection.h"
00037 #include "StrUtil.h"
00038 #include "mace_constants.h"
00039
00040 #ifndef _MACE_VECTOR_H
00041 #define _MACE_VECTOR_H
00042
00048 namespace mace {
00049
00055
00056
00060 template<class T, class Serial = SerializeList<T>, class Alloc = std::allocator<T> >
00061 class vector : public std::vector<T, Alloc>, public Serial, public PrintPrintable {
00062
00063 BOOST_CLASS_REQUIRE2(Serial, Serializer, boost, ConvertibleConcept);
00064
00065 public:
00066 typedef typename std::vector<T, 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 vector() : baseType() {
00072 }
00073
00074 vector(size_type n) : baseType(n) {
00075 }
00076
00077 vector(size_type n, const T& t) : baseType(n, t) {
00078 }
00079
00080 vector(const std::vector<T, Alloc>& v) : baseType(v) {
00081 }
00082 vector(const const_iterator& b, const const_iterator& e) : baseType(b,e) {
00083 }
00084
00085 virtual ~vector() { }
00086
00088 void resize(size_t sz) {
00089 baseType::resize(sz);
00090 }
00091
00093 void resize(size_t sz, const T& t) {
00094 baseType::resize(sz, t);
00095 }
00096
00098 size_t size() const {
00099 return baseType::size();
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00137 const std::string& getTypeName() const {
00138 const char* types[] = { "T", "Serial", "Alloc", 0 };
00139 static const StrUtilNamespace::StdStringList myTypes = StrUtilNamespace::getTypeFromTemplate(__PRETTY_FUNCTION__, types);
00140 return myTypes[0];
00141 }
00142
00143 void print(std::ostream& out) const {
00144 if(mace::PRINT_TYPE_NAMES) {
00145 out << "vector<"<<this->getTypeName()<<">";
00146 }
00147 printList(out, this->begin(), this->end());
00148 }
00149 void printState(std::ostream& out) const {
00150 if(mace::PRINT_TYPE_NAMES) {
00151 out << "vector<"<<this->getTypeName()<<">";
00152 }
00153 printListState(out, this->begin(), this->end());
00154 }
00155
00157 const_iterator random() const {
00158 if(this->size()==0) { return this->end(); }
00159 int rand = RandomUtil::randInt(this->size());
00160 return this->begin() + rand;
00161 }
00162
00165 T* getMem() {
00166 return this->_M_impl._M_start;
00167 }
00168
00169 private:
00170 class v_iterator : public _SerializeList<T>::iterator {
00171 private:
00172 iterator i;
00173 iterator end;
00174
00175 public:
00176 bool isValid() {
00177 return i != end;
00178 }
00179 void next() {
00180 ASSERT(i != end);
00181 i++;
00182 }
00183 T& getValue() {
00184 return *i;
00185 }
00186
00187 v_iterator(iterator beg, iterator end) : i(beg), end(end) {}
00188 ~v_iterator() {}
00189 };
00190
00191 class v_const_iterator : public _SerializeList<T>::const_iterator {
00192 private:
00193 const_iterator i;
00194 const_iterator end;
00195
00196 public:
00197 bool isValid() {
00198 return i != end;
00199 }
00200 void next() {
00201 ASSERT(i != end);
00202 i++;
00203 }
00204 const T& getValue() {
00205 return *i;
00206 }
00207
00208 v_const_iterator(const_iterator beg, const_iterator end) : i(beg), end(end) {}
00209 ~v_const_iterator() {}
00210 };
00211
00212 protected:
00213 typename _SerializeList<T>::iterator* getIterator() {
00214 return new v_iterator(this->begin(), this->end());
00215 }
00216 typename _SerializeList<T>::const_iterator* getIterator() const {
00217 return new v_const_iterator(this->begin(), this->end());
00218 }
00219 };
00220
00223 }
00224 #endif // _MACE_VECTOR_H