OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "Histogram.H"
00028 #include "ListOps.H"
00029 
00030 
00031 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00032 
00033 template<class List>
00034 void Foam::Histogram<List>::count(const List& bins, const List& l)
00035 {
00036     if (bins.size() < 2)
00037     {
00038         FatalErrorIn("Histogram<List>::count(const List&, const List&)")
00039             << "Should have at least two values in bins. Now:" << bins
00040             << exit(FatalError);
00041     }
00042 
00043     counts_.setSize(bins.size()-1);
00044     counts_ = 0;
00045 
00046     nLow_ = 0;
00047     nHigh_ = 0;
00048 
00049     forAll(l, i)
00050     {
00051         label index = findLower(bins, l[i]);
00052 
00053         if (index == -1)
00054         {
00055             nLow_++;
00056         }
00057         else if (index == bins.size()-1)
00058         {
00059             nHigh_++;
00060         }
00061         else
00062         {
00063             counts_[index]++;
00064         }
00065     }
00066 }
00067 
00068 
00069 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00070 
00071 template<class List>
00072 Foam::Histogram<List>::Histogram(const List& bins, const List& l)
00073 {
00074     count(bins, l);
00075 }
00076 
00077 
00078 template<class List>
00079 Foam::Histogram<List>::Histogram
00080 (
00081     const typename List::const_reference min,
00082     const typename List::const_reference max,
00083     const label nBins,
00084     const List& l
00085 )
00086 {
00087     List bins(nBins+1);
00088 
00089     typename List::value_type span = (max-min) / nBins;
00090 
00091     bins[0] = min;
00092 
00093     for (label i = 1; i < nBins; i++)
00094     {
00095         bins[i] = bins[i-1] + span;
00096     }
00097 
00098     // Set max directly to avoid truncation errors.
00099     bins[nBins] = max;
00100 
00101     count(bins, l);
00102 }
00103 
00104 
00105 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd