FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

Histogram.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "Histogram.H"
00027 #include <OpenFOAM/ListOps.H>
00028 
00029 
00030 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00031 
00032 template<class List>
00033 void Foam::Histogram<List>::count(const List& bins, const List& l)
00034 {
00035     if (bins.size() < 2)
00036     {
00037         FatalErrorIn("Histogram<List>::count(const List&, const List&)")
00038             << "Should have at least two values in bins. Now:" << bins
00039             << exit(FatalError);
00040     }
00041 
00042     counts_.setSize(bins.size()-1);
00043     counts_ = 0;
00044 
00045     nLow_ = 0;
00046     nHigh_ = 0;
00047 
00048     forAll(l, i)
00049     {
00050         label index = findLower(bins, l[i]);
00051 
00052         if (index == -1)
00053         {
00054             nLow_++;
00055         }
00056         else if (index == bins.size()-1)
00057         {
00058             nHigh_++;
00059         }
00060         else
00061         {
00062             counts_[index]++;
00063         }
00064     }
00065 }
00066 
00067 
00068 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00069 
00070 template<class List>
00071 Foam::Histogram<List>::Histogram(const List& bins, const List& l)
00072 {
00073     count(bins, l);
00074 }
00075 
00076 
00077 template<class List>
00078 Foam::Histogram<List>::Histogram
00079 (
00080     const typename List::const_reference min,
00081     const typename List::const_reference max,
00082     const label nBins,
00083     const List& l
00084 )
00085 {
00086     List bins(nBins+1);
00087 
00088     typename List::value_type span = (max-min) / nBins;
00089 
00090     bins[0] = min;
00091 
00092     for (label i = 1; i < nBins; i++)
00093     {
00094         bins[i] = bins[i-1] + span;
00095     }
00096 
00097     // Set max directly to avoid truncation errors.
00098     bins[nBins] = max;
00099 
00100     count(bins, l);
00101 }
00102 
00103 
00104 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines