FreeFOAM The Cross-Platform CFD Toolkit
Histogram.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Histogram.H"
27 #include <OpenFOAM/ListOps.H>
28 
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class List>
33 void Foam::Histogram<List>::count(const List& bins, const List& l)
34 {
35  if (bins.size() < 2)
36  {
37  FatalErrorIn("Histogram<List>::count(const List&, const List&)")
38  << "Should have at least two values in bins. Now:" << bins
39  << exit(FatalError);
40  }
41 
42  counts_.setSize(bins.size()-1);
43  counts_ = 0;
44 
45  nLow_ = 0;
46  nHigh_ = 0;
47 
48  forAll(l, i)
49  {
50  label index = findLower(bins, l[i]);
51 
52  if (index == -1)
53  {
54  nLow_++;
55  }
56  else if (index == bins.size()-1)
57  {
58  nHigh_++;
59  }
60  else
61  {
62  counts_[index]++;
63  }
64  }
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
69 
70 template<class List>
72 {
73  count(bins, l);
74 }
75 
76 
77 template<class List>
79 (
80  const typename List::const_reference min,
81  const typename List::const_reference max,
82  const label nBins,
83  const List& l
84 )
85 {
86  List bins(nBins+1);
87 
88  typename List::value_type span = (max-min) / nBins;
89 
90  bins[0] = min;
91 
92  for (label i = 1; i < nBins; i++)
93  {
94  bins[i] = bins[i-1] + span;
95  }
96 
97  // Set max directly to avoid truncation errors.
98  bins[nBins] = max;
99 
100  count(bins, l);
101 }
102 
103 
104 // ************************ vim: set sw=4 sts=4 et: ************************ //