CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/DataFormats/Common/interface/Vector.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_Vector_h
00002 #define DataFormats_Common_Vector_h
00003 // $Id: Vector.h,v 1.4 2010/07/24 14:14:45 wmtan Exp $
00004 
00005 #include <algorithm>
00006 #include <functional>
00007 #include <vector>
00008 
00009 #include "DataFormats/Common/interface/traits.h"
00010 #include "DataFormats/Common/interface/Ref.h"
00011 
00012 #if defined CMS_USE_DEBUGGING_ALLOCATOR
00013 #include "DataFormats/Common/interface/debugging_allocator.h"
00014 #endif
00015 
00016 #include "DataFormats/Common/interface/PostReadFixupTrait.h"
00017 
00018 namespace edm {
00019   class ProductID;
00020   template <typename T>
00021   class Vector  {
00022   private:
00023 #if defined(CMS_USE_DEBUGGING_ALLOCATOR)
00024     typedef std::vector<T, debugging_allocator<T> > base;
00025 #else
00026     typedef std::vector<T> base;
00027 #endif
00028 
00029   public:
00030     typedef typename base::size_type size_type;
00031     typedef typename base::value_type value_type;
00032     typedef typename base::pointer pointer;
00033     typedef typename base::reference reference;
00034     typedef typename base::const_reference const_reference;
00035       
00036     typedef typename base::iterator iterator;
00037     typedef typename base::const_iterator const_iterator;
00038 
00039     Vector();
00040     Vector(size_type);
00041     Vector(const Vector &);
00042     ~Vector();
00043       
00044     iterator begin();
00045     iterator end();
00046     const_iterator begin() const;
00047     const_iterator end() const;
00048     size_type size() const;
00049     bool empty() const;
00050     reference operator[](size_type);
00051     const_reference operator[](size_type) const;
00052     Vector<T> & operator=(const Vector<T> &);
00053     void reserve(size_t);
00054     void push_back(const value_type &);
00055     void pop_back();
00056     reference back();
00057     const_reference back() const;
00058     reference front();
00059     const_reference front() const;
00060     const base & data() const; 
00061     void clear();
00062     iterator erase(iterator pos);
00063     iterator erase(iterator first, iterator last);
00064     void swap(Vector<T> & other);
00065 
00066     void fillView(ProductID const& id,
00067                   std::vector<void const*>& pointers,
00068                   helper_vector& helpers) const;
00069 
00070   private:
00071     base data_;      
00072     typename helpers::PostReadFixupTrait<T>::type fixup_;
00073     inline void fixup() const { fixup_(data_); }
00074     inline void touch() { fixup_.touch(); }
00075   };
00076   
00077   template<typename T>
00078   inline Vector<T>::Vector() : data_() { 
00079   }
00080   
00081   template<typename T>
00082   inline Vector<T>::Vector(size_type n) : data_(n) { 
00083   }
00084   
00085   template<typename T>
00086   inline Vector<T>::Vector(const Vector<T> & o) : data_(o.data_) {
00087   }
00088   
00089   template<typename T>
00090   inline Vector<T>::~Vector() { 
00091   }
00092   
00093   template<typename T>
00094   inline Vector<T> & Vector<T>::operator=(const Vector<T> & o) {
00095     Vector<T> temp(o);
00096     this->swap(temp);
00097     return *this;
00098   }
00099   template<typename T>
00100   inline typename Vector<T>::iterator Vector<T>::begin() {
00101     fixup();
00102     touch();
00103     return iterator(data_.begin());
00104   }
00105   
00106   template<typename T>
00107   inline typename Vector<T>::iterator Vector<T>::end() {
00108     fixup();
00109     touch();
00110     return iterator(data_.end());
00111   }
00112   
00113   template<typename T>
00114   inline typename Vector<T>::const_iterator Vector<T>::begin() const {
00115     fixup();
00116     return const_iterator(data_.begin());
00117   }
00118   
00119   template<typename T>
00120   inline typename Vector<T>::const_iterator Vector<T>::end() const {
00121     fixup();
00122     return const_iterator(data_.end());
00123   }
00124   
00125   template<typename T>
00126   inline typename Vector<T>::size_type Vector<T>::size() const {
00127     return data_.size();
00128   }
00129   
00130   template<typename T>
00131   inline bool Vector<T>::empty() const {
00132     return data_.empty();
00133   }
00134   
00135   template<typename T>
00136   inline typename Vector<T>::reference Vector<T>::operator[](size_type n) {
00137     fixup();
00138     return data_[n];
00139   }
00140   
00141   template<typename T>
00142   inline typename Vector<T>::const_reference Vector<T>::operator[](size_type n) const {
00143     fixup();
00144     return data_[n];
00145   }
00146   
00147   template<typename T>
00148   inline void Vector<T>::reserve(size_t n) {
00149     data_.reserve(n);
00150   }
00151   
00152   template<typename T>
00153   inline void Vector<T>::push_back(const typename Vector<T>::value_type & d) {
00154     data_.push_back(d);
00155     touch();
00156   }
00157 
00158   template<typename T>
00159   inline void Vector<T>::pop_back() {
00160     // We have to delete the pointed-to thing, before we squeeze it
00161     // out of the vector...
00162     data_.pop_back();
00163     touch();
00164   }
00165 
00166   template<typename T>
00167   inline typename Vector<T>::reference Vector<T>::back() {
00168     fixup();
00169     touch();
00170     return data_.back();
00171   }
00172   
00173   template<typename T>
00174   inline typename Vector<T>::const_reference Vector<T>::back() const {
00175     fixup();
00176     return data_.back();
00177   }
00178   
00179   template<typename T>
00180   inline typename Vector<T>::reference Vector<T>::front() {
00181     fixup();
00182     touch();
00183     return data_.front();
00184   }
00185   
00186   template<typename T>
00187   inline typename Vector<T>::const_reference Vector<T>::front() const {
00188     fixup();
00189     return data_.front();
00190   }
00191   
00192   template<typename T>
00193   inline const typename Vector<T>::base & Vector<T>::data() const {
00194     fixup();
00195     return data_;
00196   }
00197 
00198   template<typename T>
00199   inline void Vector<T>::clear() {
00200     data_.clear();
00201   }
00202 
00203   template<typename T>
00204   typename Vector<T>::iterator Vector<T>::erase(iterator pos) {
00205     fixup();
00206     touch();
00207     return data_.erase(pos);
00208   }
00209 
00210   template<typename T>
00211   typename Vector<T>::iterator Vector<T>::erase(iterator first, iterator last) {
00212     fixup();
00213     touch();
00214     return data_.erase(first, last);
00215   }
00216 
00217   template<typename T>
00218   inline void Vector<T>::swap(Vector<T>& other) {
00219     data_.swap(other.data_);
00220     std::swap(fixup_, other.fixup_);
00221   }
00222 
00223   template<typename T>
00224   void Vector<T>::fillView(ProductID const& id,
00225                            std::vector<void const*>& pointers,
00226                            helper_vector& helpers) const
00227   {
00228     typedef Ref<Vector>      ref_type ;
00229     typedef reftobase::RefHolder<ref_type> holder_type;
00230 
00231     size_type numElements = this->size();
00232     pointers.reserve(numElements);
00233     helpers.reserve(numElements);
00234     size_type key = 0;
00235     for(typename base::const_iterator i=data_.begin(), e=data_.end(); i!=e; ++i, ++key) {
00236 
00237       pointers.push_back(&*i);
00238       holder_type h(ref_type(id, &*i, key));
00239       helpers.push_back(&h);
00240     }
00241   }
00242 
00243   template<typename T>
00244   inline void swap(Vector<T>& a, Vector<T>& b) {
00245     a.swap(b);
00246   }
00247 
00248   //----------------------------------------------------------------------
00249   //
00250   // Free function template to support creation of Views.
00251 
00252   template <typename T>
00253   inline
00254   void
00255   fillView(Vector<T> const& obj,
00256            ProductID const& id,
00257            std::vector<void const*>& pointers,
00258            helper_vector& helpers) {
00259     obj.fillView(id, pointers, helpers);
00260   }
00261 
00262   template <typename T>
00263   struct has_fillView<edm::Vector<T> >
00264   {
00265     static bool const value = true;
00266   };
00267 
00268 }
00269 
00270 #endif