Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

shared_vector_base.h

Go to the documentation of this file.
00001 // -*- mode: c++; -*-
00002 //
00003 //The Biomolecule Toolkit (BTK) is a C++ library for use in the
00004 //modeling, analysis, and design of biological macromolecules.
00005 //Copyright (C) 2004, Christopher Saunders <ctsa@users.sourceforge.net>
00006 //
00007 //This program is free software; you can redistribute it and/or modify
00008 //it under the terms of the GNU Lesser General Public License as published
00009 //by the Free Software Foundation; either version 2.1 of the License, or (at
00010 //your option) any later version.
00011 //
00012 //This program is distributed in the hope that it will be useful,  but
00013 //WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //Lesser General Public License for more details. 
00016 //
00017 //You should have received a copy of the GNU Lesser General Public License 
00018 //along with this program; if not, write to the Free Software Foundation, 
00019 //Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 
00025 
00026 
00027 #ifndef BTK_SHARED_VECTOR_BASE_H
00028 #define BTK_SHARED_VECTOR_BASE_H
00029 
00030 #include <boost/concept_check.hpp>
00031 #include <boost/iterator/indirect_iterator.hpp>
00032 #include <boost/shared_ptr.hpp>
00033 
00034 #include <vector>
00035 
00036 
00037 namespace BTK {
00038 
00051 template <typename T> 
00052 class SharedVectorBase {
00053   typedef boost::shared_ptr<T> shared_pointer;
00054   typedef std::vector<shared_pointer> container_type;
00055   typedef typename container_type::iterator direct_iterator;
00056   typedef typename container_type::const_iterator const_direct_iterator;
00057 
00058 public:
00059   typedef SharedVectorBase<T> self_type;
00060 
00062 
00063   typedef T value_type;
00064   typedef value_type& reference;
00065   typedef const reference const_reference;
00066   typedef value_type* pointer;
00067   typedef const pointer const_pointer;
00068   typedef typename container_type::size_type size_type;  
00069   typedef boost::indirect_iterator<direct_iterator> iterator;
00070   typedef boost::indirect_iterator<const_direct_iterator> const_iterator;
00072 
00074   SharedVectorBase(size_type s = 0) : _vector() {
00075     for (;s>0;--s) { push_back(value_type()); }
00076   }
00077 
00078   template <typename InputIterator>
00079   SharedVectorBase(InputIterator first, InputIterator last);
00080   
00081   SharedVectorBase(const self_type& source) : _vector(){
00082     deep_copy(source);
00083   }
00084 
00085   template <typename T2>
00086   SharedVectorBase(const SharedVectorBase<T2>& source) : _vector(){
00087     deep_copy(source);
00088   }
00089 
00090   virtual ~SharedVectorBase() {}
00091 
00093   self_type& operator=(const self_type& rhs) {
00094     if (&rhs == this) return *this;
00095     deep_copy(rhs);
00096     return *this;
00097   }
00098 
00099   template <typename T2>
00100   self_type& operator=(const SharedVectorBase<T2>& rhs) {
00101     deep_copy(rhs);
00102     return *this;
00103   }
00104 
00107 
00108 
00109   size_type size() const { return _vector.size(); }
00110 
00111   iterator begin() { return iterator(_vector.begin()); }
00112   const_iterator begin() const { return const_iterator(_vector.begin()); } 
00113 
00114   iterator end() { return iterator(_vector.end()); }
00115   const_iterator end() const { return const_iterator(_vector.end()); }
00116 
00117   reference operator[](size_type i) { return *(begin()+i); }
00118   const_reference operator[](size_type i) const { return *(end()+i); }
00119 
00120   reference front() { return operator[](0); }
00121   const_reference front() const { return operator[](0); }
00122   
00123   reference back() { return operator[](size()-1); }
00124   const_reference back() const { return operator[](size()-1); }
00126 
00127 protected:
00129 
00130 
00131   void clear() { _vector.clear(); }
00132 
00133   void resize(size_type s) {
00134     if (s < size()) erase(begin()+s, end());
00135     else insert(end(), s-size(), value_type());
00136   }
00137 
00138   void reserve(size_type s) { _vector.reserve(s); }
00139 
00140   void push_back(const value_type& x) {
00141     shared_pointer p(new value_type(x));
00142     _vector.push_back(p);
00143   }
00144 
00145   iterator insert(iterator pos,
00146                   const value_type& x){
00147     shared_pointer p(new value_type(x));
00148     return iterator(_vector.insert(pos.base(),p));    
00149   }
00150 
00151   void insert(iterator pos,
00152         size_type s,
00153         const value_type& x);
00154 
00155   template <typename InputIterator>
00156   void insert(iterator pos,
00157               InputIterator first,
00158               InputIterator last);
00159 
00160   iterator erase(iterator pos){
00161     return iterator(_vector.erase(pos.base()));
00162   }
00163 
00164   iterator erase(iterator first,
00165                  iterator last){
00166     return iterator(_vector.erase(first.base(),last.base()));
00167   }
00169 
00170 
00173 
00174   void shallow_push_back(value_type* x) {
00175     shared_pointer p(x);
00176     _vector.push_back(p);
00177   }
00178 
00179   void shallow_insert(iterator pos,
00180                       const_iterator first,
00181                       const_iterator last){
00182     _vector.insert(pos.base(),first.base(),last.base());
00183   }
00185 
00186 
00189 
00190   template <typename T2>
00191   void deep_copy(const SharedVectorBase<T2>& source);
00192 
00193   void shallow_copy(const self_type& source);
00195 
00196 
00197 private:
00198   container_type _vector;
00199 };
00200 
00201 } // namespace BTK
00202 
00203 #include "shared_vector_base.hpp"
00204 
00205 #endif

Generated on Wed Apr 14 00:43:18 2004 for BTK by doxygen 1.3.6