Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

DPeakArray.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework 
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_KERNEL_DPEAKARRAY_H
00028 #define OPENMS_KERNEL_DPEAKARRAY_H
00029 
00030 #include <OpenMS/config.h>
00031 #include <OpenMS/CONCEPT/Macros.h>
00032 #include <OpenMS/KERNEL/ComparatorUtils.h>
00033 #include <OpenMS/FORMAT/PersistentObject.h>
00034 
00035 #include <vector>
00036 #include <algorithm>
00037 
00038 namespace OpenMS
00039 {
00040 
00056   template <typename PeakT, typename AllocT = std::allocator<PeakT> >
00057   class DPeakArray
00058     : public std::vector<PeakT, AllocT>, 
00059       public PersistentObject
00060   {
00061     public:
00062     
00064     typedef PeakT PeakType;
00065 
00067     typedef AllocT AllocType;
00068     
00070 
00071     enum { DIMENSION = PeakType::DIMENSION };
00072 
00074     typedef std::vector<PeakType, AllocT> Base;
00075 
00077     typedef typename std::vector<PeakType, AllocT>::iterator Iterator;
00079     typedef typename std::vector<PeakType, AllocT>::const_iterator ConstIterator;
00081     typedef typename std::vector<PeakType, AllocT>::reverse_iterator ReverseIterator;
00083     typedef typename std::vector<PeakType, AllocT>::const_reverse_iterator ConstReverseIterator;
00084 
00085     // allow c'tors of other template instances to access private members
00086     template < typename ContainerT_, typename AllocT_ > friend class DPeakArray;    
00087     
00091 
00093     inline DPeakArray() : Base(), PersistentObject() {} 
00094 
00096     inline DPeakArray(const DPeakArray& p) : Base(p), PersistentObject(p) {}
00097 
00099     template <typename AllocT2>
00100     inline DPeakArray(const DPeakArray<PeakT, AllocT2>& p) : Base(p.begin(),p.end()), PersistentObject(p) {}
00101 
00103     template <typename AllocT2>
00104     inline DPeakArray(const DPeakArray<PeakT, AllocT2>& p, const AllocT& alloc) 
00105     : 
00106       Base(alloc),
00107       PersistentObject(p)
00108     {
00109       // copy data manually into new vector
00110       //TODO #ifdef OPENMS_64BIT_ARCHITECTURE .. use large capacity for ExternalAlloc() to avoid reallocation?! Check cache locality (benchmark)!
00111       this->reserve(p.capacity());
00112       //std::copy(p.begin(), p.end(), std::back_inserter(this->back()));        
00113       for(typename std::vector<PeakT, AllocT2>::const_iterator it = p.begin(); it != p.end(); ++it)
00114       {
00115         this->push_back(*it);
00116       }
00117       
00118       if (p.size() != this->size())
00119       {
00120         std::cout << "ERROR: given size: " << p.size() << " pushed size: " << this->size() << std::endl;
00121       }
00122     }
00123 
00124     
00126     inline ~DPeakArray() {}
00127 
00129     DPeakArray(typename std::vector<PeakType, AllocT>::size_type n) : Base(n), PersistentObject() {} 
00130 
00132     DPeakArray(typename std::vector<PeakType, AllocT>::size_type n, const PeakType& peak) : Base(n, peak), PersistentObject() {} 
00133 
00134 
00136     DPeakArray(const AllocT& a) : Base(a), PersistentObject() {} 
00137 
00139     DPeakArray(typename std::vector<PeakType>::size_type n, const PeakType& peak, const AllocT& a) : Base(n, peak, a), PersistentObject() {} 
00140     
00141         
00142     
00144     template <class InputIterator>
00145     DPeakArray(InputIterator f, InputIterator l) : Base(f,l), PersistentObject() {}
00146 
00148     
00150     DPeakArray& operator = (const DPeakArray& rhs) 
00151     { 
00152       if (this==&rhs) return *this;
00153       Base::operator=(rhs);
00154       // don't return Base immediately to avoid a cast
00155       return *this;
00156     }
00157 
00159     //template <typename AllocT2>
00160     //DPeakArray& operator = (const DPeakArray<PeakT, AllocT2>& rhs) 
00161     //{ 
00162     //  //if (this==&rhs) return *this;
00163     //  Base::operator=(rhs);
00164     //  // don't return Base immediately to avoid a cast
00165     //  return *this;
00166     //}
00167     
00168     
00175 
00176     void sortByIntensity(bool reverse=false) 
00177     { 
00178       if (reverse)
00179       {
00180         std::sort(Base::begin(), Base::end(), reverseComparator(typename PeakType::IntensityLess())); 
00181       }
00182       else
00183       {
00184         std::sort(Base::begin(), Base::end(), typename PeakType::IntensityLess()); 
00185       }
00186     }
00187     
00189     void sortByPosition() 
00190     { 
00191       std::sort(Base::begin(), Base::end(), typename PeakType::PositionLess());
00192     }
00193 
00199     void sortByNthPosition(UInt i) throw (Exception::NotImplemented);
00200     
00210     template < typename ComparatorType >
00211     void sortByComparator ( ComparatorType const & comparator )
00212     { 
00213       std::sort(Base::begin(), Base::end(), ComparatorType( comparator ) ); 
00214     }
00215     template < typename ComparatorType >
00216     void sortByComparator ()
00217     { 
00218       std::sort(Base::begin(), Base::end(), ComparatorType() ); 
00219     }
00221 
00223     bool operator == (const DPeakArray& array) const
00224     {
00225       return std::operator==(*this,array);
00226     }
00227     
00229     bool operator !=(const DPeakArray& array) const
00230     {
00231       return !(operator==(array));
00232     }
00233 
00235     template <typename AllocT2>
00236     bool operator < (const DPeakArray<PeakT, AllocT2>& array) const
00237     {
00238       return Base::size() < array.size();
00239     }
00240 
00242     template <typename AllocT2>
00243     bool operator > (const DPeakArray<PeakT, AllocT2>& array) const
00244     {
00245       return Base::size() > array.size();
00246     }
00247     
00249     bool operator <= (const DPeakArray& array) const
00250     {
00251       return operator<(array) || operator==(array);
00252     }
00253 
00255     bool operator >= (const DPeakArray& array) const
00256     {
00257       return operator>(array) || operator==(array);
00258     }
00259 
00260     protected:
00261       // Docu in base class
00262       virtual void clearChildIds_()
00263       {
00264         //nothing to do here
00265       };
00266   };
00267 
00269   template <typename PeakT, typename AllocT>
00270   std::ostream& operator << (std::ostream& os, const DPeakArray<PeakT, AllocT>& array)
00271   {
00272     os << "-- DPEAKARRAY BEGIN --"<<std::endl;
00273     for (typename DPeakArray<PeakT, AllocT>::const_iterator it = array.begin(); it!=array.end(); ++it)
00274     {
00275       os << *it << std::endl;
00276     }
00277     os << "-- DPEAKARRAY END --"<<std::endl;
00278     return os;
00279   }
00280 
00281 //---------------------------------------------------------------
00282 //  Implementation of the inline / template functions
00283 //---------------------------------------------------------------
00284 
00285   template <typename PeakT, typename Alloc > 
00286   void DPeakArray<PeakT,Alloc>::sortByNthPosition(UInt i) throw (Exception::NotImplemented)
00287   { 
00288     OPENMS_PRECONDITION(i < UInt(DIMENSION), "illegal dimension")
00289     if (i==0)
00290     {
00291       std::sort(Base::begin(), Base::end(), typename PeakType::template NthPositionLess<0>() );
00292     }
00293     else if (i==1)
00294     {
00295       std::sort(Base::begin(), Base::end(), typename PeakType::template NthPositionLess<1>() );
00296     }
00297     else if (i==2)
00298     {
00299       std::sort(Base::begin(), Base::end(), typename PeakType::template NthPositionLess<2>() );
00300     }
00301     else
00302     {
00303       throw Exception::NotImplemented(__FILE__,__LINE__,__FUNCTION__);
00304     }
00305   }
00306 
00307 } // namespace OpenMS
00308 
00309 #endif // OPENMS_KERNEL_DPEAKARRAY_H

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1