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 
00039 #ifndef _SIMTH_ARRAY_H
00040 #define _SIMTH_ARRAY_H
00041 
00042 
00043 #include <string>
00044 #include <iostream>
00045 #include <stdexcept>
00046 #include <vector>
00047 
00048 namespace simth
00049 {
00050 
00060 
00061 
00062 
00063 
00064 
00065 
00068 template<class T> class Array
00069 {
00070     
00071     
00072     std::vector<T> array;
00073 
00074   public:
00076     Array(): array(0) {}
00079     Array(size_t size);
00085     Array(const T& val, size_t size);
00087     Array(const Array<T>& a);
00089     Array<T>& operator=(const Array<T>& a);
00091     ~Array() {}
00093     T& operator[](size_t pos);
00095     const T& operator[](size_t pos) const;
00097     size_t size() const {return array.size();}
00102     void resize(size_t newSize, const T& val = T());
00103 };
00104 
00105 
00106 template<class T>
00107 inline Array<T>::Array(size_t size)
00108     : array(size)
00109 {}
00110 
00111 template<class T>
00112 inline Array<T>::Array(const T& val, size_t size)
00113     : array(size, val)
00114 {}
00115 
00116 template<class T>
00117 inline Array<T>::Array(const Array<T>& a)
00118     : array(a.array)
00119 {}
00120 
00121 template<class T>
00122 inline Array<T>& Array<T>::operator=(const Array<T>& a)
00123 {
00124   array.operator=(a.array);
00125   return *this;
00126 }
00127 
00128 template<class T>
00129 inline void Array<T>::resize(size_t newSize, const T& val)
00130 {
00131   array.resize(newSize, val);
00132 }
00133 
00134 template<class T>
00135 std::ostream& operator<<(std::ostream &os, const Array<T> &a)
00136 {
00137   
00138   std::ios::fmtflags oldSettings = os.flags();
00139   
00140   os.precision(2);
00141   size_t len = a.size();
00142   for(size_t i = len - 1; i >= 0; i--) {
00143     std::cout<<" "<<a[i]<<" ";
00144   }
00145   os<<" <- start";
00146   
00147   os.flags(oldSettings);
00148   return os;
00149 }
00150 
00151 template<class T>
00152 inline T& Array<T>::operator[](size_t pos)
00153 {
00154   if(DEBUG) {
00155     if(pos >= size()) {
00156       throw std::out_of_range("inline reference checkedVector::operator[](size_t pos)const : pos > size" 
00157 #ifdef PHBIB_HEADER
00158                   ", pos == "+
00159                               simth::i2string(pos)+" this.size() == "+
00160                               simth::i2string(size())
00161 #endif
00162       );
00163     }
00164   }
00165   return array[pos];
00166 }
00167 
00168 template<class T>
00169 inline const T& Array<T>::operator[](size_t pos) const
00170 {
00171   if(DEBUG) {
00172     if(pos >= size()) {
00173       throw std::out_of_range("inline reference checkedVector::operator[](size_t pos)const : pos > size" 
00174 #ifdef PHBIB_HEADER
00175                   ", pos == "+
00176                               simth::i2string(pos)+" this.size() == "+
00177                               simth::i2string(size())
00178 #endif
00179       );
00180     }
00181   }
00182   return array[pos];
00183 }
00184 
00185 
00187 
00188 } 
00189 
00190 #endif // _SIMTH_ARRAY_H