Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

array.h

Go to the documentation of this file.
00001 /*-*-c++-*-*****************************************************************
00002                           array.h  -  description
00003                              -------------------
00004     begin                : Fri Feb 20 2004
00005     copyright            : (C) 2004 by Christian Stimming
00006     email                : stimming@tuhh.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00024  *   MA  02111-1307  USA                                                   *
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 // simth::Array
00064 // /////////////////////////////////////////////////////////
00065 
00068 template<class T> class Array
00069 {
00070     // As it turned out with some profiling, a vector<T> is even
00071     // better than a valarray<T> here.
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   //save old format settings:
00138   std::ios::fmtflags oldSettings = os.flags();
00139   //formatting
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   //rewrite old settings:
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 } // namespace
00189 
00190 #endif // _SIMTH_ARRAY_H

Generated on Mon Apr 24 21:19:19 2006 for simthetic by  doxygen 1.4.1