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

SparseVector.h (Maintainer: Andreas Bertsch)

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: Andreas Bertsch $
00025 // --------------------------------------------------------------------------
00026 //
00027 #ifndef OPENMS_DATASTRUCTURES_SPARSEVECTOR_H
00028 #define OPENMS_DATASTRUCTURES_SPARSEVECTOR_H
00029 
00030 #include <OpenMS/CONCEPT/Types.h>
00031 
00032 #include <map>
00033 
00034 namespace OpenMS
00035 {
00036 
00046   class SparseVector
00047   {
00048     // @name Friends
00049     // @{
00050     friend class SparseVectorIterator;
00051     friend class SparseVectorConstIterator;
00052     friend class DoubleProxy;
00053     // @}
00054     
00060     class DoubleProxy
00061     {
00062     
00063       public:
00064 
00065         // @name Constructors and Destructors
00066         // @{
00068         DoubleProxy(SparseVector& vec, UInt index);
00069         // @}
00070 
00071         // @name Operators
00072         // @{
00074         DoubleProxy& operator=(const DoubleProxy& rhs);
00075         
00077         DoubleProxy& operator=(double val);
00078         
00080         operator double() const;
00081         // @}
00082         
00083       private:
00084       
00086         SparseVector& vec_;
00087         
00089         int index_;
00090     };
00091 
00092     // forward declaration
00093     class SparseVectorConstIterator;
00094     
00096     class SparseVectorIterator
00097     {
00098       // @name Friends
00099       // @{
00100       friend class SparseVector;
00101       friend class SparseVector::SparseVectorConstIterator;
00102       // @}
00103       
00104     public:
00105 
00106       // @name Constructors and Destructors
00107       // @{
00109       SparseVectorIterator(const SparseVectorIterator& source);
00110       
00112       virtual ~SparseVectorIterator();
00113       // @}
00114 
00115       // @name Operators
00116       // @{
00118       SparseVectorIterator& operator ++ (); // prefix
00119 
00121       SparseVectorIterator operator ++ (int); // postfix
00122 
00124       DoubleProxy operator * ();      
00125       // @}
00126       
00127     
00128       // @name Accessors
00129       // @{
00131       SparseVectorIterator& hop();
00132       
00134       UInt position() const;
00135       // @}
00136       
00137       // @name Predicates
00138       // @{
00140       bool operator!=(const SparseVectorIterator& other);
00141       // @}
00142       
00143     private:
00144 
00146       SparseVectorIterator();
00147 
00149       SparseVectorIterator(SparseVector& vector, int position);
00150       
00151       // the position in SparseVector
00152       UInt position_;
00153      
00155       SparseVector& vector_;
00156 
00157       // the position in the underlying map of SparseVector
00158       std::map<UInt,double>::const_iterator valit_;
00159     };
00160     
00162     class SparseVectorConstIterator
00163     {
00164       // @name Friends
00165       // @{
00166       friend class SparseVector;
00167       // @}
00168       
00169     public:
00170   
00171       // @name Constructors and Destructors
00172       // @{
00174       SparseVectorConstIterator(const SparseVectorConstIterator& source);
00175       
00177       SparseVectorConstIterator(const SparseVector::SparseVectorIterator& source);
00178       
00180       virtual ~SparseVectorConstIterator();
00181       // @}
00182 
00183       // @name Operators
00184       // @{
00186       SparseVectorConstIterator& operator ++ ();
00187 
00189       SparseVectorConstIterator operator ++ (int);
00190 
00192       double operator * ();
00193       // @}
00194 
00195       // @name Accessors
00196       // @{
00198       SparseVectorConstIterator& hop();
00199       
00201       UInt position() const;
00202       // @} 
00203       
00204       // @name Predicates
00205       // @{
00207       bool operator != (const SparseVectorConstIterator& other);
00208       // @}
00209 
00210     private:
00211 
00213       SparseVectorConstIterator();
00214 
00216       SparseVectorConstIterator(const SparseVector& vector, int position);
00217       
00218       // the position in SparseVector
00219       UInt position_;
00220       
00222       const SparseVector& vector_;
00223       
00224       // the position in the underlying map of SparseVector
00225       std::map<UInt, double>::const_iterator valit_;
00226     };
00227 
00228   public:
00229     
00230     // @name Typedefs
00231     // @{
00232     typedef SparseVectorConstIterator const_iterator;
00233     typedef SparseVectorConstIterator ConstIterator;
00234     typedef SparseVectorIterator iterator;
00235     typedef SparseVectorIterator Iterator;
00236     // @}
00237     
00239     // @{
00241     SparseVector();
00242 
00244     SparseVector(int size);
00245 
00247     SparseVector(const SparseVector& source);
00248     
00250     virtual ~SparseVector();
00251     // @}
00252 
00253     // @name Operators
00254     // @{
00256     SparseVector& operator = (const SparseVector& source);
00257 
00259     const DoubleProxy operator[] (UInt pos) const;
00260 
00262     DoubleProxy operator[] (UInt pos);
00263     // @}
00264 
00265   
00266     // @name Accessors
00267     // @{
00269     UInt size() const;
00270     
00272     UInt nonzero_size() const;
00273     
00275     double at(UInt pos) const;
00276     
00278     void push_back(double value);
00279 
00281     void clear();
00282     
00284     void resize(UInt newsize);
00285     // @}
00286 
00288 
00289 
00290     iterator begin();
00291     
00293     iterator end();
00294     
00296     const_iterator begin() const;
00297     
00299     const_iterator end() const;
00301     
00302   private:
00303   
00305     std::map<UInt, double> values_;
00306 
00308     UInt size_;
00309   };
00310   
00311 }
00312 
00313 #endif //OPENMS_DATASTRUCTURES_SPARSEVECTOR_H
00314 

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