libidx
/home/rex/ebltrunk/core/libidx/include/stl.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef STL_HPP_
00034 #define STL_HPP_
00035 
00036 namespace std {
00037 
00039   // string
00040 
00041 #ifndef __NOSTL__ // extending STL
00042   
00043   template<class T> 
00044   string& operator<<(string& e, const T v) {
00045     e += v;
00046     return e;
00047   }
00048 
00049 #endif
00050 
00052   // ostream
00053 
00054 #ifdef __NOSTL__
00055   
00056   template<class T> 
00057   ostream& ostream::operator<<(const T &v) {
00058     eblerror("ostream not implemented for this type");
00059     return *this;
00060   }
00061 
00062   template<class T> 
00063   ostream& ostream::operator<<(T &v) {
00064     eblerror("ostream not implemented for this type");
00065     return *this;
00066   }
00067 
00068 #endif /* __NOSTL__ */
00069 
00070   template<class T> 
00071   string& operator<<(string &o, const vector<T> &v) {
00072     o << "[ ";
00073     for (typename vector<T>::const_iterator i = v.begin(); 
00074          i != v.end(); ++i)
00075       o << *i << " ";
00076     o << "]";
00077     return o;
00078   }
00079 
00080   template<class T> 
00081   ostream& operator<<(ostream &o, const vector<T> &v) {
00082     o << "[ ";
00083     for (typename vector<T>::const_iterator i = v.begin(); 
00084          i != v.end(); ++i)
00085       o << *i << " ";
00086     o << "]";
00087     return o;
00088   }
00089 
00090   template<class T> 
00091   string& operator<<(string &o, const list<T> &v) {
00092     o << "[ ";
00093     for (typename list<T>::const_iterator i = v.begin(); 
00094          i != v.end(); ++i)
00095       o << *i << " ";
00096     o << "]";
00097     return o;
00098   }
00099 
00100   template<class T> 
00101   ostream& operator<<(ostream &o, const list<T> &v) {
00102     o << "[ ";
00103     for (typename list<T>::const_iterator i = v.begin(); 
00104          i != v.end(); ++i)
00105       o << *i << " ";
00106     o << "]";
00107     return o;
00108   }
00109 
00111   // vector
00112 
00113 #ifdef __NOSTL__
00114 
00115   template <typename T>
00116   vector<T>::vector() : ebl::idx<T>((ebl::intg)0) {
00117   }
00118 
00119   template <typename T>
00120   vector<T>::vector(size_t n, const T value) : ebl::idx<T>((ebl::intg)n) {
00121     idx_fill(*this, value);
00122   }
00123 
00124   template <typename T>
00125   vector<T>::~vector() {
00126   }
00127 
00128   template <typename T>
00129   size_t vector<T>::size() const {
00130     return (size_t) this->dim(0);
00131   }
00132 
00133   template <typename T>
00134   size_t vector<T>::length() const {
00135     return (size_t) this->dim(0);
00136   }
00137   
00138   template <typename T>
00139   bool vector<T>::empty() const {
00140     return (this->dim(0) == 0) ? true : false;
00141   }
00142   
00143   template <typename T>
00144   void vector<T>::clear() {
00145     this->resize((ebl::intg)0);
00146   }
00147 
00148   // template <typename T>
00149   // void vector<T>::push_back(T &e) {
00150   //   ebl::intg i = this->dim(0);
00151   //   this->resize(i + 1);
00152   //   this->set(e, i);
00153   // }
00154       
00155   template <typename T>
00156   void vector<T>::push_back(T e) {
00157     ebl::intg i = this->dim(0);
00158     this->resize(i + 1);
00159     this->set(e, i);
00160   }
00161       
00162   template <typename T>
00163   T& vector<T>::operator[](size_t i) {
00164     ebl::intg j = (ebl::intg) i;
00165     if (j >= this->dim(0))
00166       eblerror(j << " is out of bounds in " << this->dim(0));
00167     return this->get(j);    
00168   }
00169         
00170   template <typename T>
00171   const T& vector<T>::operator[](size_t i) const {
00172     ebl::intg j = (ebl::intg) i;
00173     if (j >= this->dim(0))
00174       eblerror(j << " is out of bounds in " << this->dim(0));
00175     return this->get(j);    
00176   }
00177         
00178 #endif
00179 
00181   // min/max
00182 
00183 #ifdef __NOSTL__
00184   
00185   template <typename T>
00186   T min(const T &e1, const T &e2) {
00187     if (e2 < e1)
00188       return e2;
00189     return e1;
00190   }
00191   
00192   template <typename T>
00193   T max(const T &e1, const T &e2) {
00194     if (e2 > e1)
00195       return e2;
00196     return e1;
00197   }
00198   
00199 #endif
00200   
00201 } // end namespace ebl
00202 
00203 #endif /* STL_HPP_ */