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 | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2015 OpenFOAM Foundation
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "Histogram.H"
29#include "ListOps.H"
30
31
32// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33
34template<class List>
35void Foam::Histogram<List>::count(const List& bins, const List& l)
36{
37 if (bins.size() < 2)
38 {
40 << "Should have at least two values in bins. Now:" << bins
41 << exit(FatalError);
42 }
43
44 counts_.setSize(bins.size()-1);
45 counts_ = 0;
46
47 nLow_ = 0;
48 nHigh_ = 0;
49
50 forAll(l, i)
51 {
52 label index = findLower(bins, l[i]);
53
54 if (index == -1)
55 {
56 nLow_++;
57 }
58 else if (index == bins.size()-1)
59 {
60 nHigh_++;
61 }
62 else
63 {
64 counts_[index]++;
65 }
66 }
67}
68
69
70// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
71
72template<class List>
74{
75 count(bins, l);
76}
77
78
79template<class List>
81(
82 const typename List::const_reference min,
83 const typename List::const_reference max,
84 const label nBins,
85 const List& l
86)
87{
88 List bins(nBins+1);
89
90 typename List::value_type span = (max-min) / nBins;
91
92 bins[0] = min;
93
94 for (label i = 1; i < nBins; i++)
95 {
96 bins[i] = bins[i-1] + span;
97 }
98
99 // Set max directly to avoid truncation errors.
100 bins[nBins] = max;
101
102 count(bins, l);
103}
104
105
106// ************************************************************************* //
Various functions to operate on Lists.
Y[inertIndex] max(0.0)
Calculates the counts per bin of a list.
Definition: Histogram.H:53
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
unsigned int const_reference
Definition: bitSet.H:124
label count() const
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333