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
00040 #ifndef _CHECKEDVECTOR_H
00041 #define _CHECKEDVECTOR_H
00042
00043
00044 #include <vector>
00045 #include <iostream>
00046
00047 #include <simthetic/misc.h>
00048 #include <simthetic/exceptions.h>
00049
00050 namespace simth
00051 {
00052
00062
00063
00071 template<class T>
00072 class checkedVector : public std::vector<T>
00073 {
00074 public:
00077 typedef typename std::vector<T>::iterator iterator;
00080 typedef typename std::vector<T>::const_iterator const_iterator;
00081
00084 typedef typename std::vector<T>::reference reference;
00087 typedef typename std::vector<T>::const_reference const_reference;
00088
00089 public:
00092 checkedVector() : std::vector<T>() {}
00096 explicit checkedVector(unsigned int size, const T &val = T()) : std::vector<T>(size, val) {}
00100 const_reference operator[](unsigned int pos) const;
00104 reference operator[](unsigned int pos);
00108 void assign(unsigned int size, const T &val);
00113 checkedVector<T>& operator=(const checkedVector<T>& from);
00114 checkedVector<T>& operator=(const std::vector<T>& from);
00115 };
00116
00117
00118 template<class T>
00119 inline typename checkedVector<T>::const_reference checkedVector<T>::operator[](unsigned int pos) const
00120 {
00121 #if DEBUG==1
00122 if(pos >= std::vector<T>::size()) {
00123 throw std::out_of_range("checkedVector<T>::operator[] out of range");
00124 }
00125 #endif
00126 return std::vector<T>::operator[](pos);
00127 }
00128
00129
00130
00131
00132
00133 template<class T>
00134 inline typename checkedVector<T>::reference checkedVector<T>::operator[](unsigned int pos)
00135 {
00136 #if DEBUG==1
00137 if(pos >= std::vector<T>::size()) {
00138 throw std::out_of_range("checkedVector<T>::operator[] out of range");
00139 }
00140 #endif
00141 return std::vector<T>::operator[](pos);
00142 }
00143
00144
00145 template<class T>
00146 inline checkedVector<T>& checkedVector<T>::operator=(const std::vector<T>& from)
00147 {
00148 unsigned int newSize = from.size();
00149 if(newSize != this->size()) {
00150 this->resize(newSize);
00151 }
00152
00153 iterator thisIter = this->begin();
00154 for(const_iterator fromIter = from.begin();fromIter!=from.end();++fromIter) {
00155 (*thisIter) = (*fromIter);
00156 ++thisIter;
00157 }
00158 return *this;
00159 }
00160 template<class T>
00161 inline checkedVector<T>& checkedVector<T>::operator=(const checkedVector<T>& from)
00162 {
00163 return operator=(static_cast<const std::vector<T>&>(from));
00164 }
00165
00166 template<class T>
00167 inline void checkedVector<T>::assign(unsigned int newSize, const T &val)
00168 {
00169 this->resize(newSize);
00170 for(unsigned int pos=0; pos<newSize; pos++) {
00171 (*this)[pos]=val;
00172 }
00173 }
00174
00175
00176
00177 template<class T>
00178 std::ostream& operator<<(std::ostream &os, const checkedVector<T> &seq)
00179 {
00180
00181 std::ios::fmtflags oldSettings = os.flags();
00182
00183 os.precision(2);
00184 unsigned int len = seq.size();
00185 for(int i = len - 1; i >= 0; i--) {
00186 os<<" "<<seq[i]<<" ";
00187 }
00188 os<<" <- start";
00189
00190 os.flags(oldSettings);
00191 return os;
00192 }
00193
00194 std::ostream& operator<<(std::ostream &os, const checkedVector<Complex>& compSeq);
00195
00197
00198 }
00199
00200 #endif // _CONTAINERS_H