svector.h

00001 /*
00002 
00003     This file is part of the FAST-ER machine learning system.
00004     Copyright (C) 2008  Edward Rosten and Los Alamos National Laboratory
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License along
00017     with this program; if not, write to the Free Software Foundation, Inc.,
00018     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019 */
00020 #ifndef SVECTOR_H
00021 #define SVECTOR_H
00022 
00023 
00024 #include <vector>
00025 #include <iostream>
00026 #ifdef SAFE_VECTOR
00027 namespace std{
00028 
00029 template<class T> class foovector: public std::vector<T>
00030 {
00031     public:
00032         foovector()
00033         {}
00034 
00035         foovector(size_t n)
00036         :std::vector<T>(n)
00037         {}
00038 
00039         template<class C> foovector(size_t n, const C& v)
00040         :std::vector<T>(n, v)
00041         {}
00042 
00043         template<class C> foovector(const C& begin, const C& end)
00044         :std::vector<T>(begin, end)
00045         {}
00046 
00047 
00048         foovector(const std::vector<T>& v)
00049         :std::vector<T>(v)
00050         {}
00051 
00052         foovector& operator=(const std::vector<T>& v)
00053         {
00054             std::vector<T>::operator=(v);
00055             return *this;
00056         }
00057 
00058         T&back() { return this->operator[](std::vector<T>::size()-1); }
00059         const T&back() const { return this->operator[](std::vector<T>::size()-1); }
00060 
00061         T&front() { return this->operator[]();}
00062         const T&front() const { return this->operator[]();}
00063 
00064         T&operator[](size_t n) { 
00065             try{
00066                 return std::vector<T>::at(n); 
00067             }
00068             catch(...)
00069             {
00070                 std::cerr << "Bad access n=" << n << " size=" << std::vector<T>::size() << endl;
00071                 throw;
00072             }
00073         }
00074         const T&operator[](size_t n)const {
00075             try{
00076                 return std::vector<T>::at(n); 
00077             }
00078             catch(...)
00079             {
00080                 std::cerr << "Bad access n=" << n << " size=" << std::vector<T>::size() << endl;
00081                 throw;
00082             }
00083         }
00084 };
00085 
00086 template<> class foovector<bool>: public std::vector<bool>
00087 {
00088     public:
00089         foovector()
00090         {}
00091 
00092         foovector(size_t n)
00093         :std::vector<bool>(n)
00094         {}
00095 
00096         template<class C> foovector(size_t n, const C& v)
00097         :std::vector<bool>(n, v)
00098         {}
00099 
00100         template<class C> foovector(const C& begin, const C& end)
00101         :std::vector<bool>(begin, end)
00102         {}
00103 
00104 
00105         foovector(const std::vector<bool>& v)
00106         :std::vector<bool>(v)
00107         {}
00108 
00109         foovector& operator=(const std::vector<bool>& v)
00110         {
00111             std::vector<bool>::operator=(v);
00112             return *this;
00113         }
00114 
00115         std::_Bit_reference operator[](size_t n) { return std::vector<bool>::at(n); }
00116         const bool operator[](size_t n)const { return std::vector<bool>::at(n); }
00117 };
00118 
00119 
00120 }
00121 
00122 #define vector foovector
00123 #endif
00124 #endif

Generated on Mon Mar 2 12:47:12 2009 for FAST-ER by  doxygen 1.5.3