Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / containers / Lists / Histogram / Histogram.C
blob65a1b6d4f8111f797bf927a6ce390bf4567d1c3b
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "Histogram.H"
27 #include "ListOps.H"
30 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
32 template<class List>
33 void Foam::Histogram<List>::count(const List& bins, const List& l)
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     }
42     counts_.setSize(bins.size()-1);
43     counts_ = 0;
45     nLow_ = 0;
46     nHigh_ = 0;
48     forAll(l, i)
49     {
50         label index = findLower(bins, l[i]);
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     }
68 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
70 template<class List>
71 Foam::Histogram<List>::Histogram(const List& bins, const List& l)
73     count(bins, l);
77 template<class List>
78 Foam::Histogram<List>::Histogram
80     const typename List::const_reference min,
81     const typename List::const_reference max,
82     const label nBins,
83     const List& l
86     List bins(nBins+1);
88     typename List::value_type span = (max-min) / nBins;
90     bins[0] = min;
92     for (label i = 1; i < nBins; i++)
93     {
94         bins[i] = bins[i-1] + span;
95     }
97     // Set max directly to avoid truncation errors.
98     bins[nBins] = max;
100     count(bins, l);
104 // ************************************************************************* //