Initial public release.
[OpenCLIPER] / LPISupport / include / LPISupport / SampleCollection.hpp
1 /* Copyright (C) 2018 Federico Simmross Wattenberg,
2  *                    Manuel Rodríguez Cayetano,
3  *                    Javier Royuela del Val,
4  *                    Elena Martín González,
5  *                    Elisa Moya Sáez,
6  *                    Marcos Martín Fernández and
7  *                    Carlos Alberola López
8  *
9  * This file is part of OpenCLIPER.
10  *
11  * OpenCLIPER is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; version 3 of the License.
14  *
15  * OpenCLIPER is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with OpenCLIPER; If not, see <http://www.gnu.org/licenses/>.
22  *
23  *
24  *  Contact:
25  *
26  *  Federico Simmross Wattenberg
27  *  E.T.S.I. Telecomunicación
28  *  Universidad de Valladolid
29  *  Paseo de Belén 15
30  *  47011 Valladolid, Spain.
31  *  fedsim@tel.uva.es
32  */
33 /*
34  * Utils.hpp
35  *
36  *  Created on: 15 de nov. de 2016
37  *      Author: manrod
38  */
39
40 #ifndef INCLUDE_OPENCLIPER_SAMPLECOLLECTION_HPP_
41 #define INCLUDE_OPENCLIPER_SAMPLECOLLECTION_HPP_
42 #include <string>
43 #include <vector>
44 #include <cmath>
45 #include <memory>
46 #include <LPISupport/InfoItems.hpp>
47
48 namespace LPISupport {
49 /**
50  * @brief Class that stores a group of samples of some measurement and provides methods for calculation of statistics and storing this information (using an InfoItem object)
51  * 
52  */    
53 class SampleCollection {
54 public:
55     /// @brief Class for configuring summary output of stored samples
56     struct OutputConfigTraits {
57         /// samples are shown in summary output if true
58         bool showSamples = true;
59         /// mean samples is shown in summary output if true
60         bool showMean = true;
61         /// variance of samples is shown in summary output if true
62         bool showVariance = true;
63     };
64
65     SampleCollection(std::string name);
66     SampleCollection(std::string name, std::shared_ptr<OutputConfigTraits> pOutputConfigTraits);
67     virtual ~SampleCollection();
68
69     /**
70      * @brief Gets the name associated to this sample collection
71      * @return the name of the collection
72      */
73     std::string getSampleName() const {return sampleName;}
74     /**
75      * @brief Sets the name associated to this sample collection
76      * @param[in] sampleName name for the collection
77      */
78     void setSampleName(std::string sampleName) {this->sampleName = sampleName;}
79     /**
80      * @brief Removes all the samples of the collection and sets flags showing that mean and variance are not valid
81      */
82     void clearSamples() {samples.clear(); meanValid = false; varianceValid = false;}
83     /**
84      * @brief Appends a sample to the collection
85      * @param[in] sample of the sample (double)
86      */
87     void appendSample(double sample) {samples.push_back(sample); meanValid = false; varianceValid = false;}
88     void addSamples(SampleCollection newSamples);
89     /**
90      * @brief Returns number of samples of the collection
91      * @return the number of samples
92      */
93     unsigned long getNumOfSamples() const {return samples.size();};
94     /**
95      * @brief Gets the value of a sample in some position
96      * @param[in] position index of the sample in the vector of samples (beginning at 0)
97      * @return the value of the sample
98      */
99     double getSample (unsigned long position) const {return samples.at(position);}
100     double getMean() ;
101     double getVariance();
102     std::unique_ptr<InfoItems> to_infoItems(unsigned int numDigitsPrec);
103 private:
104     void calcMean();
105     void calcVariance();
106     
107     /// Flag that shows if stored mean value is valid (otherwise, it must be recalculated)
108     bool meanValid = false;
109     /// Flag that shows if stored variance value is valid (otherwise, it must be recalculated)
110     bool varianceValid = false;
111     /// Vector storing the samples
112     std::vector<double> samples;
113     /// Mean of stored samples
114     double mean = 0.0;
115     /// Variance of stored samples
116     double variance = 0.0;
117     /// Name of sample collection
118     std::string sampleName = "";
119     /// Smart pointer to object for configuring summary output of the sample collection
120     std::shared_ptr<OutputConfigTraits> pOutputConfigTraits;
121 };
122
123 } /* namespace OpenCLIPER */
124
125 #endif /* INCLUDE_OPENCLIPER_SAMPLECOLLECTION_HPP_ */
126
127