00001 #include <vector>
00002 namespace std{
00003 template <class T, class Allo = allocator <T> >
00004 class safe_vector : vector<T,Allo> {
00005 static int DIE_NOW() {
00006 return *(int*)0;
00007 }
00008
00009 static int temprand() {
00010 static int seed;
00011 return seed += 319282;
00012
00013 }
00014 int id;
00015 public:
00016 safe_vector (unsigned int __n, const T &val):vector<T,Allo>(__n,val) {
00017 id=temprand();
00018 }
00019 safe_vector () {
00020 id=temprand();
00021 }
00022 safe_vector (unsigned int __n): vector<T,Allo> (__n) {
00023 id=temprand();
00024 }
00025 safe_vector (const vector <T,Allo> &v): vector<T,Allo>(v){
00026 id=temprand();
00027 }
00028 const vector<T,Allo> & operator= (const vector <T,Allo> & a) {
00029 *((vector <T,Allo>*)this)=a;
00030 id = temprand();
00031 return a;
00032 }
00033 T& operator[](unsigned int n) { return (*((vector <T,Allo> *)this)).at(n); }
00034 const T& operator[](unsigned int n) const {return (*((vector <T,Allo> *)this)).at(n);}
00035 T& at(unsigned int n) { return (*((vector <T,Allo> *)this)).at(n); }
00036 const T& at(unsigned int n) const {return (*((vector <T,Allo> *)this)).at(n);}
00037 void push_back (const T& t) {
00038 vector<T,Allo>::push_back(t);
00039 id++;
00040 }
00041 void push_back () {
00042 vector<T,Allo>::push_back();
00043 id++;
00044 }
00045 vector<T,Allo> &operator = (const vector <T,Allo>& k) {
00046 *((vector <T,Allo> *)this) = k;
00047 id=temprand();
00048 return k;
00049 }
00050 void reserve (unsigned int n) {
00051 vector<T,Allo>::reserve (n);
00052 id++;
00053 }
00054 class iterator: public vector<T,Allo>::iterator {
00055 safe_vector <T,Allo> *v;
00056 int id;
00057
00058 public:
00059 void check_relevance() const {
00060 if (id!=v->id) {
00061 fprintf (stderr,__PRETTY_FUNCTION__);
00062 DIE_NOW();
00063 }
00064 }
00065 iterator (const vector<T,Allo>::iterator &i,safe_vector <T,Allo> *id):vector<T,Allo>::iterator(i) {
00066 this->v =id;
00067 this->id = id->id;
00068 }
00069
00070
00071
00072
00073 bool operator == (const vector<T,Allo>::iterator &i) {
00074 check_relevance();
00075 return (*((vector<T,Allo>::iterator *)this))==i;
00076 }
00077 bool operator == (const vector<T,Allo>::const_iterator &i) {
00078 check_relevance();
00079 return (*((vector<T,Allo>::iterator *)this))==i;
00080 }
00081 bool operator != (const vector<T,Allo>::iterator &i) {
00082 check_relevance();
00083 return (*((vector<T,Allo>::iterator *)this))!=i;
00084 }
00085 bool operator != (const vector<T,Allo>::const_iterator &i) {
00086 check_relevance();
00087 return (*((vector<T,Allo>::iterator *)this))!=i;
00088 }
00089 T & operator * () {
00090 check_relevance();
00091 return (**((vector <T,Allo>::iterator*)this));
00092 }
00093 iterator operator ++ (int) {
00094 check_relevance();
00095 return iterator((*((vector<T,Allo>::iterator *)this))++,v);
00096 }
00097
00098 iterator operator +=(int i) {
00099 (*((vector<T,Allo>::iterator *)this))+=i;
00100 check_relevance();
00101 return *this;
00102 }
00103 iterator operator -=(int i) {
00104 (*((vector<T,Allo>::iterator *)this))-=i;
00105 check_relevance();
00106 return *this;
00107 }
00108 iterator operator ++ () {
00109 return (*this)+=1;
00110 }
00111 iterator operator -- () {
00112 return (*this)-=1;
00113 }
00114
00115 };
00116
00117 class const_iterator: public vector<T,Allo>::const_iterator {
00118 public:
00119 const safe_vector <T,Allo> *v;
00120 int id;
00121 void check_relevance() const {
00122 if (id!=v->id) {
00123 fprintf (stderr,__PRETTY_FUNCTION__);
00124 DIE_NOW();
00125 }
00126 }
00127 const T & operator * () {
00128 check_relevance();
00129 return (**((vector <T,Allo>::const_iterator*)this));
00130 }
00131 bool operator == (const vector<T,Allo>::iterator &i) {
00132 check_relevance();
00133 return (*((vector<T,Allo>::const_iterator *)this))==i;
00134 }
00135 bool operator == (const vector<T,Allo>::const_iterator &i) {
00136 check_relevance();
00137 return (*((vector<T,Allo>::const_iterator *)this))==i;
00138 }
00139 bool operator != (const vector<T,Allo>::iterator &i) {
00140 check_relevance();
00141 return (*((vector<T,Allo>::const_iterator *)this))!=i;
00142 }
00143 bool operator != (const vector<T,Allo>::const_iterator &i) {
00144 check_relevance();
00145 return (*((vector<T,Allo>::const_iterator *)this))!=i;
00146 }
00147 const_iterator (const vector<T,Allo>::const_iterator &i,const safe_vector <T,Allo> *id):vector<T,Allo>::const_iterator(i) {
00148 this->v =id;
00149 this->id = id->id;
00150 }
00151 const_iterator (const iterator &i): vector<T,Allo>::const_iterator(i) {
00152 v = i->v;
00153 id = i->id;
00154 }
00155
00156 const_iterator operator ++ (int) {
00157 check_relevance();
00158 return (*((vector<T,Allo>::const_iterator *)this))++;
00159 }
00160 iterator operator +=(int i) {
00161 (*((vector<T,Allo>::const_iterator *)this))+=i;
00162 check_relevance();
00163 return *this;
00164 }
00165 iterator operator -=(int i) {
00166 (*((vector<T,Allo>::const_iterator *)this))-=i;
00167 check_relevance();
00168 return *this;
00169 }
00170 iterator operator ++ () {
00171 return (*this)+=1;
00172 }
00173 iterator operator -- () {
00174 return (*this)-=1;
00175 }
00176 };
00177 iterator begin() {
00178 return iterator (((vector<T,Allo>*)this)->begin(),this);
00179 }
00180 iterator end() {
00181 return iterator (((vector<T,Allo>*)this)->end(),this);
00182 }
00183 const_iterator end() const{
00184 return const_iterator (((vector<T,Allo>*)this)->end(),this);
00185 }
00186 const_iterator begin() const{
00187 return const_iterator (((const vector<T,Allo> *)this)->begin(),this);
00188 }
00189 iterator erase (iterator a, iterator b) {
00190 a.check_relevance();
00191 b.check_relevance();
00192 id++;
00193 return iterator (((vector <T,Allo> *)this)->erase(a,b),this);
00194 }
00195 void swap (safe_vector <T,Allo> &a) {
00196 temp = id;
00197 id = a.id;
00198 a.id = temp;
00199 ((vector <T,Allo> *)this)->swap (a);
00200 }
00201 void resize (size_type ns, const T & x) {
00202 ((vector <T,Allo> *)this)->resize(ns,x);
00203 id++;
00204 }
00205 void resize (size_type ns) {
00206 ((vector <T,Allo> *)this)->resize(ns);
00207 id++;
00208 }
00209 T& back () {
00210 if (size()>0) return ((vector <T,Allo> *)this)->back();
00211 }
00212 const T& back () const {
00213 if (size()>0) return ((vector <T,Allo> *)this)->back();
00214 }
00215 T& front () {
00216 if (size()>0) return ((vector <T,Allo> *)this)->front();
00217 }
00218 const T& front () const {
00219 if (size()>0) return ((vector <T,Allo> *)this)->front();
00220 }
00221
00222 void clear() {
00223 ((vector <T,Allo> *)this)->clear();
00224 id++;
00225 }
00226 iterator erase (iterator a) {
00227 a.check_relevance();
00228 id++;
00229 return iterator (((vector <T,Allo> *)this)->erase(a),this);
00230
00231 }
00232 iterator insert (iterator positn, const T& t) {
00233 positn.check_relevance();
00234 vector<T,Allo>::iterator i=vector<T,Allo>::insert (positn,t);
00235 id++;
00236 return iterator(i,this);
00237 }
00238 iterator insert (iterator positn, unsigned int n, const T& t) {
00239 positn.check_relevance();
00240 vector<T,Allo>::iterator i=vector<T,Allo>::insert (positn,n,t);
00241 id++;
00242 return iterator(i,this);
00243 }
00244 iterator insert (iterator positn) {
00245 positn.check_relevance();
00246 vector<T,Allo>::iterator i=vector<T,Allo>::insert (positn);
00247 id++;
00248 return iterator(i,this);
00249 }
00250 iterator insert (iterator positn, unsigned int n) {
00251 positn.check_relevance();
00252 vector<T,Allo>::iterator i=vector<T,Allo>::insert (positn,n);
00253 id++;
00254 return iterator(i,this);
00255 }
00256 };
00257 template <class _Tp, class Allo > inline void swap(safe_vector<_Tp,Allo>& x, safe_vector<_Tp,Allo>&y)
00258 {
00259 x.swap(y);
00260 }
00261 }