QGIS API Documentation  3.9.0-Master (d9ef585e47)
qgsstringstatisticalsummary.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstringstatisticalsummary.cpp
3  -------------------------------
4  Date : May 2016
5  Copyright : (C) 2016 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include <QString>
18 #include <QStringList>
19 #include <QObject>
20 #include <QVariant>
21 #include <QVariantList>
22 #include <limits>
23 
24 /***************************************************************************
25  * This class is considered CRITICAL and any change MUST be accompanied with
26  * full unit tests in test_qgsstringstatisticalsummary.py.
27  * See details in QEP #17
28  ****************************************************************************/
29 
30 QgsStringStatisticalSummary::QgsStringStatisticalSummary( QgsStringStatisticalSummary::Statistics stats )
31  : mStatistics( stats )
32 {
33  reset();
34 }
35 
37 {
38  mCount = 0;
39  mValues.clear();
40  mCountMissing = 0;
41  mMin.clear();
42  mMax.clear();
43  mMinLength = std::numeric_limits<int>::max();
44  mMaxLength = 0;
45  mSumLengths = 0;
46  mMeanLength = 0;
47 }
48 
49 void QgsStringStatisticalSummary::calculate( const QStringList &values )
50 {
51  reset();
52 
53  const auto constValues = values;
54  for ( const QString &string : constValues )
55  {
56  testString( string );
57  }
58  finalize();
59 }
60 
61 void QgsStringStatisticalSummary::addString( const QString &string )
62 {
63  testString( string );
64 }
65 
66 void QgsStringStatisticalSummary::addValue( const QVariant &value )
67 {
68  if ( value.type() == QVariant::String )
69  {
70  testString( value.toString() );
71  }
72  finalize();
73 }
74 
76 {
77  mMeanLength = mSumLengths / static_cast< double >( mCount );
78 }
79 
80 void QgsStringStatisticalSummary::calculateFromVariants( const QVariantList &values )
81 {
82  reset();
83 
84  const auto constValues = values;
85  for ( const QVariant &variant : constValues )
86  {
87  if ( variant.type() == QVariant::String )
88  {
89  testString( variant.toString() );
90  }
91  }
92 }
93 
94 void QgsStringStatisticalSummary::testString( const QString &string )
95 {
96  mCount++;
97 
98  if ( string.isEmpty() )
99  mCountMissing++;
100 
101  if ( mStatistics & CountDistinct )
102  {
103  mValues << string;
104  }
105  if ( mStatistics & Min )
106  {
107  if ( !mMin.isEmpty() && !string.isEmpty() )
108  {
109  mMin = std::min( mMin, string );
110  }
111  else if ( mMin.isEmpty() && !string.isEmpty() )
112  {
113  mMin = string;
114  }
115  }
116  if ( mStatistics & Max )
117  {
118  if ( !mMax.isEmpty() && !string.isEmpty() )
119  {
120  mMax = std::max( mMax, string );
121  }
122  else if ( mMax.isEmpty() && !string.isEmpty() )
123  {
124  mMax = string;
125  }
126  }
127  if ( mStatistics & MeanLength )
128  mSumLengths += string.length();
129  mMinLength = std::min( mMinLength, string.length() );
130  mMaxLength = std::max( mMaxLength, string.length() );
131 }
132 
134 {
135  switch ( stat )
136  {
137  case Count:
138  return mCount;
139  case CountDistinct:
140  return mValues.count();
141  case CountMissing:
142  return mCountMissing;
143  case Min:
144  return mMin;
145  case Max:
146  return mMax;
147  case MinimumLength:
148  return mMinLength;
149  case MaximumLength:
150  return mMaxLength;
151  case MeanLength:
152  return mMeanLength;
153  case All:
154  return 0;
155  }
156  return 0;
157 }
158 
160 {
161  switch ( statistic )
162  {
163  case Count:
164  return QObject::tr( "Count" );
165  case CountDistinct:
166  return QObject::tr( "Count (distinct)" );
167  case CountMissing:
168  return QObject::tr( "Count (missing)" );
169  case Min:
170  return QObject::tr( "Minimum" );
171  case Max:
172  return QObject::tr( "Maximum" );
173  case MinimumLength:
174  return QObject::tr( "Minimum length" );
175  case MaximumLength:
176  return QObject::tr( "Maximum length" );
177  case MeanLength:
178  return QObject::tr( "Mean length" );
179  case All:
180  return QString();
181  }
182  return QString();
183 }
184 
Statistic
Enumeration of flags that specify statistics to be calculated.
static QString displayName(QgsStringStatisticalSummary::Statistic statistic)
Returns the friendly display name for a statistic.
QgsStringStatisticalSummary(QgsStringStatisticalSummary::Statistics stats=QgsStringStatisticalSummary::All)
Constructor for QgsStringStatistics.
void addValue(const QVariant &value)
Adds a single variant to the statistics calculation.
void addString(const QString &string)
Adds a single string to the statistics calculation.
void calculateFromVariants(const QVariantList &values)
Calculates summary statistics for an entire list of variants at once.
QVariant statistic(QgsStringStatisticalSummary::Statistic stat) const
Returns the value of a specified statistic.
void finalize()
Must be called after adding all strings with addString() and before retrieving any calculated string ...
void calculate(const QStringList &values)
Calculates summary statistics for an entire list of strings at once.
void reset()
Resets the calculated values.