stl_helpers.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: stl_helpers.hpp 1162 2008-03-27 21:18:13Z snelsen $"
00002 
00003 
00004 
00010 #ifndef GPSTK_STL_HELPERS_HPP
00011 #define GPSTK_STL_HELPERS_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 //============================================================================
00036 //
00037 //This software developed by Applied Research Laboratories at the University of
00038 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00039 //Department of Defense. The U.S. Government retains all rights to use,
00040 //duplicate, distribute, disclose, or release this software. 
00041 //
00042 //Pursuant to DoD Directive 523024 
00043 //
00044 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00045 //                           release, distribution is unlimited.
00046 //
00047 //=============================================================================
00048 
00049 
00050 
00051 
00052 
00053 
00054 #include <cmath>
00055 #include <algorithm>
00056 #include <list>
00057 
00058 namespace gpstk
00059 {
00062 
00064    template<class For> For max(const std::list<For>& lst) 
00065    {
00066       return *max_element(lst.begin(), lst.end());
00067    }
00068    
00070    template<class For> For min(const std::list<For>& lst) 
00071    {
00072       return *min_element(lst.begin(), lst.end());
00073    }
00074 
00076    template<class bt>
00077    struct ListStats
00078    {
00080       unsigned n;
00082       bt mean, sigma, min, max;
00084       ListStats():n(0),mean(0),sigma(0),min(0),max(0){};
00085    };
00086 
00091    template<class bt> ListStats<bt> stats(const std::list<bt>& lst)
00092    {
00093       ListStats<bt> s;
00094       bt sum=0, sumsq=0;
00095 
00096       s.n = lst.size();
00097       if (s.n<1)
00098          return s;
00099       
00100       typename std::list<bt>::const_iterator li;
00101       li=lst.begin();
00102       s.min = s.max = *li;
00103       for(; li!=lst.end(); li++)
00104       {
00105          s.min = std::min(s.min, *li);
00106          s.max = std::max(s.max, *li);
00107          sum += *li;
00108       }
00109       s.mean = sum/s.n;
00110 
00111       if (s.n<2)
00112          return s;
00113 
00114       for(li=lst.begin(); li!=lst.end(); li++)
00115       {
00116          bt z=*li-s.mean;
00117          sumsq += z*z;
00118       }
00119    
00120       s.sigma = sqrt(sumsq/(s.n-1));
00121 
00122       return s;
00123    }
00124 
00126 
00127 } // namespace
00128    
00129 #endif

Generated on Thu Feb 9 03:31:01 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1