Skip to content
Permalink
Browse files

array_mode renamed back to array_majority

  • Loading branch information
domi4484 committed Feb 9, 2021
1 parent 034ace4 commit 951aa5a7a1934c1fc5e4ea6cfb4085843043d07c
@@ -2,11 +2,18 @@
"name": "array_majority",
"type": "function",
"groups": ["Arrays"],
"description": "Returns the value which has majority in the array. Majority means the count of the value is greather than half the size of the array.</p><p><b>Note</b><br/>For the same behavior of the majority() function from Aggregates see array_mode().</p>",
"arguments": [ {"arg":"array","description":"an array"} ],
"description": "Return the most common values in an array.",
"arguments": [
{"arg":"array","description":"an array"},
{"arg":"option='all'", "optional":true, "description":"a string specifying the return values handling. Valid options are:<br /><ul><li>all: Default, all most common values are returned in an array</li><li>first: Return the first of the most common values</li><li>median: Return the median of the most common values</li><li>real_majority: Return the value wich occur more than half the size of the array</li></ul>"}
],
"examples": [
{ "expression":"array_majority(array(0,1,42,42,43))", "returns":"NULL"},
{ "expression":"array_majority(array(0,1,42,42,43,42))", "returns":"NULL"},
{ "expression":"array_majority(array(0,1,42,42,43,42,42))", "returns":"42"}
{ "expression":"array_majority(array(0,1,42,42,43), 'all')", "returns":"[ 42 ]"},
{ "expression":"array_majority(array(0,1,42,42,43,1), 'all')", "returns":"[ 42, 1 ]"},
{ "expression":"array_majority(array(0,1,42,42,43,1), 'first')", "returns":"42"},
{ "expression":"array_majority(array(0,1,1,2,2), 'median')", "returns":"1.5"},
{ "expression":"array_majority(array(0,1,42,42,43), 'real_majority')", "returns":"NULL"},
{ "expression":"array_majority(array(0,1,42,42,43,42), 'real_majority')", "returns":"NULL"},
{ "expression":"array_majority(array(0,1,42,42,43,42,42), 'real_majority')", "returns":"42"}
]
}
@@ -2,11 +2,18 @@
"name": "array_minority",
"type": "function",
"groups": ["Arrays"],
"description": "Returns an array containing the minority values of a given array. All values are minority except the majority value, if available.",
"arguments": [ {"arg":"array","description":"an array"} ],
"description": "Return the less common values in an array.",
"arguments": [
{"arg":"array","description":"an array"},
{"arg":"option='all'", "optional":true, "description":"a string specifying the return values handling. Valid options are:<br /><ul><li>all: Default, all less common values are returned in an array</li><li>first: Return the first of the less common values</li><li>median: Return the median of the less common values</li><li>real_minority: Return all values except the real majority if thre is one</li></ul>"}
],
"examples": [
{ "expression":"array_minority(array(0,1,42,42,43))", "returns":"[ 0, 1, 42, 43 ]"},
{ "expression":"array_minority(array(0,1,42,42,43,42))", "returns":"[ 0, 1, 42, 43 ]"},
{ "expression":"array_minority(array(0,1,42,42,43,42,42))", "returns":"[ 0, 1, 43 ]"}
{ "expression":"array_minority(array(0,42,42), 'all')", "returns":"[ 0 ]"},
{ "expression":"array_minority(array(0,1,42,42), 'all')", "returns":"[ 0, 1 ]"},
{ "expression":"array_minority(array(0,1,42,42,43,1), 'first')", "returns":"43"},
{ "expression":"array_minority(array(1,2,3,3), 'median')", "returns":"1.5"},
{ "expression":"array_minority(array(0,1,42,42,43), 'real_minority')", "returns":"[ 42, 43, 0, 1 ]"},
{ "expression":"array_minority(array(0,1,42,42,43,42), 'real_minority')", "returns":"[ 42, 43, 0, 1 ]"},
{ "expression":"array_minority(array(0,1,42,42,43,42,42), 'real_minority')", "returns":"[ 43, 0, 1 ]"}
]
}

This file was deleted.

@@ -5441,7 +5441,7 @@ static QVariant convertToSameType( const QVariant &value, QVariant::Type type )
return result;
}

static QVariant fcnArrayMode( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
static QVariant fcnArrayMajority( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
{
const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
QHash< QVariant, int > hash;
@@ -5452,30 +5452,37 @@ static QVariant fcnArrayMode( const QVariantList &values, const QgsExpressionCon
const QList< int > occurrences = hash.values();
const int maxValue = *std::max_element( occurrences.constBegin(), occurrences.constEnd() );

if ( hash.keys( maxValue ).isEmpty() )
return QVariant();

const QString merge = values.at( 1 ).toString();
if ( merge.compare( QLatin1String( "all" ), Qt::CaseInsensitive ) == 0 )
const QString option = values.at( 1 ).toString();
if ( option.compare( QStringLiteral( "all" ), Qt::CaseInsensitive ) == 0 )
{
return convertToSameType( hash.keys( maxValue ), values.at( 0 ).type() );
}
else if ( merge.compare( QLatin1String( "first" ), Qt::CaseInsensitive ) == 0 )
else if ( option.compare( QStringLiteral( "first" ), Qt::CaseInsensitive ) == 0 )
{
if ( hash.keys().isEmpty() )
return QVariant();

return QVariant( hash.keys( maxValue ).first() );
}
else if ( merge.compare( QLatin1String( "median" ), Qt::CaseInsensitive ) == 0 )
else if ( option.compare( QStringLiteral( "median" ), Qt::CaseInsensitive ) == 0 )
{
return fcnArrayMedian( QVariantList() << convertToSameType( hash.keys( maxValue ), values.at( 0 ).type() ), context, parent, node );
}
else if ( option.compare( QStringLiteral( "real_majority" ), Qt::CaseInsensitive ) == 0 )
{
if ( maxValue * 2 <= list.size() )
return QVariant();

return QVariant( hash.keys( maxValue ).first() );
}
else
{
parent->setEvalErrorString( QObject::tr( "No such merge behavior '%1'" ).arg( merge ) );
parent->setEvalErrorString( QObject::tr( "No such option '%1'" ).arg( option ) );
return QVariant();
}
}

static QVariant fcnArrayMajority( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
static QVariant fcnArrayMinority( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
{
const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
QHash< QVariant, int > hash;
@@ -5484,40 +5491,41 @@ static QVariant fcnArrayMajority( const QVariantList &values, const QgsExpressio
++hash[item];
}
const QList< int > occurrences = hash.values();
const int maxValue = *std::max_element( occurrences.constBegin(), occurrences.constEnd() );

QList< QVariant > keysMaxValue = hash.keys( maxValue );

if ( keysMaxValue.size() != 1 )
return QVariant();
const int minValue = *std::min_element( occurrences.constBegin(), occurrences.constEnd() );

if ( hash.value( keysMaxValue.first() ) * 2 <= list.size() )
return QVariant();

return keysMaxValue.first();
}
const QString option = values.at( 1 ).toString();
if ( option.compare( QStringLiteral( "all" ), Qt::CaseInsensitive ) == 0 )
{
return convertToSameType( hash.keys( minValue ), values.at( 0 ).type() );
}
else if ( option.compare( QStringLiteral( "first" ), Qt::CaseInsensitive ) == 0 )
{
if ( hash.keys().isEmpty() )
return QVariant();

static QVariant fcnArrayMinority( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
const QVariantList list = QgsExpressionUtils::getListValue( values.at( 0 ), parent );
QHash< QVariant, int > hash;
for ( const auto &item : list )
return QVariant( hash.keys( minValue ).first() );
}
else if ( option.compare( QStringLiteral( "median" ), Qt::CaseInsensitive ) == 0 )
{
++hash[item];
return fcnArrayMedian( QVariantList() << convertToSameType( hash.keys( minValue ), values.at( 0 ).type() ), context, parent, node );
}
const QList< int > occurrences = hash.values();
const int maxValue = *std::max_element( occurrences.constBegin(), occurrences.constEnd() );
else if ( option.compare( QStringLiteral( "real_minority" ), Qt::CaseInsensitive ) == 0 )
{
if ( hash.keys().isEmpty() )
return QVariant();

QList< QVariant > keysMaxValue = hash.keys( maxValue );
// Remove the majority, all others are minority
const int maxValue = *std::max_element( occurrences.constBegin(), occurrences.constEnd() );
if ( maxValue * 2 > list.size() )
hash.remove( hash.key( maxValue ) );

// Remove the majority, all others are minority
if ( keysMaxValue.size() == 1
&& hash.value( keysMaxValue.first() ) * 2 > list.size() )
return convertToSameType( hash.keys(), values.at( 0 ).type() );
}
else
{
hash.remove( keysMaxValue.first() );
parent->setEvalErrorString( QObject::tr( "No such option '%1'" ).arg( option ) );
return QVariant();
}

return convertToSameType( hash.keys(), values.at( 0 ).type() );
}

static QVariant fcnArrayAppend( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
@@ -7122,9 +7130,8 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "array_max" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMaximum, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_mean" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMean, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_median" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMedian, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_mode" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "merge" ), true, QVariant( "all" ) ), fcnArrayMode, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_majority" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMajority, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_minority" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArrayMinority, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_majority" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "option" ), true, QVariant( "all" ) ), fcnArrayMajority, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_minority" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "option" ), true, QVariant( "all" ) ), fcnArrayMinority, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_sum" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ), fcnArraySum, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_append" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayAppend, QStringLiteral( "Arrays" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "array_prepend" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "array" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnArrayPrepend, QStringLiteral( "Arrays" ) )
@@ -1640,7 +1640,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "array_last(array('a', 'b', 'c'))" ) << QStringLiteral( "array_last(array('a', 'b', 'c'))" ) << false << QVariant( "c" );
QTest::newRow( "array_last(array())" ) << QStringLiteral( "array_last(array())" ) << false << QVariant();

// array_min, array_max, array_mean, array_median,array_mode, array_majority, array_minority, array_sum
// array_min, array_max, array_mean, array_median, array_majority, array_minority, array_sum
QTest::newRow( "array_min('forty two')" ) << QStringLiteral( "array_min('forty two')" ) << true << QVariant();
QTest::newRow( "array_min(42)" ) << QStringLiteral( "array_min(42)" ) << true << QVariant();
QTest::newRow( "array_min(array())" ) << QStringLiteral( "array_min(array())" ) << false << QVariant();
@@ -1661,23 +1661,26 @@ class TestQgsExpression: public QObject
QTest::newRow( "array_median(array())" ) << QStringLiteral( "array_median(array())" ) << false << QVariant();
QTest::newRow( "array_median(array(0,1,42,42,43))" ) << QStringLiteral( "array_median(array(0,1,42,42,43))" ) << false << QVariant( 42 );
QTest::newRow( "array_median(array(0,0,1,2,2,42,'a','b'))" ) << QStringLiteral( "array_median(array(0,0,1,2,2,42,'a','b'))" ) << false << QVariant( 1.5 );
QTest::newRow( "array_mode('forty two')" ) << QStringLiteral( "array_mode('forty two')" ) << true << QVariant();
QTest::newRow( "array_mode(42)" ) << QStringLiteral( "array_mode(42)" ) << true << QVariant();
QTest::newRow( "array_mode(array())" ) << QStringLiteral( "array_mode(array())" ) << false << QVariant();
QTest::newRow( "array_mode(array(1,2,42,42,'a','b'))" ) << QStringLiteral( "array_mode(array(1,2,42,42,'a','b'))" ) << false << QVariant( QVariantList() << 42 );
QTest::newRow( "array_mode(array(0,1,42,42,43), 'first')" ) << QStringLiteral( "array_mode(array(0,1,42,42,43), 'first')" ) << false << QVariant( 42 );
QTest::newRow( "array_mode(array(0,1,1,2,2,42), 'median')" ) << QStringLiteral( "array_mode(array(0,1,1,2,2,42), 'median')" ) << false << QVariant( 1.5 );
QTest::newRow( "array_majority('forty two')" ) << QStringLiteral( "array_majority('forty two')" ) << true << QVariant();
QTest::newRow( "array_majority(42)" ) << QStringLiteral( "array_majority(42)" ) << true << QVariant();
QTest::newRow( "array_majority(array())" ) << QStringLiteral( "array_majority(array())" ) << false << QVariant();
QTest::newRow( "array_majority(array(0,1,42,42,43))" ) << QStringLiteral( "array_majority(array(0,1,42,42,43))" ) << false << QVariant();
QTest::newRow( "array_majority(array(0,1,42,42,43,42))" ) << QStringLiteral( "array_majority(array(0,1,42,42,43,42))" ) << false << QVariant();
QTest::newRow( "array_majority(array(0,1,42,42,43,42,42))" ) << QStringLiteral( "array_majority(array(0,1,42,42,43,42,42))" ) << false << QVariant( 42 );
QTest::newRow( "array_majority(array())" ) << QStringLiteral( "array_majority(array())" ) << false << QVariant( QVariantList() );
QTest::newRow( "array_majority(array(0,1,42,42,43), 'all')" ) << QStringLiteral( "array_majority(array(0,1,42,42,43), 'all')" ) << false << QVariant( QVariantList() << "42" );
QTest::newRow( "array_majority(array(0,1,42,42,43,1)" ) << QStringLiteral( "array_sort(array_majority(array(0,1,42,42,43,1)))" ) << false << QVariant( QVariantList() << 1 << 42 );
QTest::newRow( "array_majority(array(0,1,42,42,43,1), 'first')" ) << QStringLiteral( "array_majority(array(0,1,42,42,43), 'first')" ) << false << QVariant( 42 );
QTest::newRow( "array_majority(array(0,1,1,2,2,42), 'median')" ) << QStringLiteral( "array_majority(array(0,1,1,2,2,42), 'median')" ) << false << QVariant( 1.5 );
QTest::newRow( "array_majority(array(0,1,42,42,43), 'real_majority')" ) << QStringLiteral( "array_majority(array(0,1,42,42,43), 'real_majority')" ) << false << QVariant();
QTest::newRow( "array_majority(array(0,1,42,42,43,42), 'real_majority')" ) << QStringLiteral( "array_majority(array(0,1,42,42,43,42), 'real_majority')" ) << false << QVariant();
QTest::newRow( "array_majority(array(0,1,42,42,43,42,42), 'real_majority')" ) << QStringLiteral( "array_majority(array(0,1,42,42,43,42,42), 'real_majority')" ) << false << QVariant( 42 );
QTest::newRow( "array_minority('forty two')" ) << QStringLiteral( "array_minority('forty two')" ) << true << QVariant();
QTest::newRow( "array_minority(42)" ) << QStringLiteral( "array_minority(42)" ) << true << QVariant();
QTest::newRow( "array_minority(array())" ) << QStringLiteral( "array_minority(array())" ) << false << QVariant( QVariantList() );
QTest::newRow( "array_minority(array(1,42,43,42))" ) << QStringLiteral( "array_sort(array_minority(array(1,42,43,42)))" ) << false << QVariant( QVariantList() << 1 << 42 << 43 );
QTest::newRow( "array_minority(array(1,42,43,42,42))" ) << QStringLiteral( "array_sort(array_minority(array(1,42,43,42,42)))" ) << false << QVariant( QVariantList() << 1 << 43 );
QTest::newRow( "array_minority(array(0,42,42), 'all')" ) << QStringLiteral( "array_minority(array(0,42,42), 'all')" ) << false << QVariant( QVariantList() << 0 );
QTest::newRow( "array_minority(array(0,1,42,42))" ) << QStringLiteral( "array_sort(array_minority(array(0,1,42,42)))" ) << false << QVariant( QVariantList() << 0 << 1 );
QTest::newRow( "array_minority(array(0,42,42), 'first')" ) << QStringLiteral( "array_minority(array(0,42,42), 'first')" ) << false << QVariant( 0 );
QTest::newRow( "array_minority(array(0,1,2,3,3), 'median')" ) << QStringLiteral( "array_minority(array(1,2,3,3), 'median')" ) << false << QVariant( 1.5 );
QTest::newRow( "array_minority(array(0,1,42,42,43), 'real_minority')" ) << QStringLiteral( "array_sort(array_minority(array(0,1,42,42,43), 'real_minority'))" ) << false << QVariant( QVariantList() << 0 << 1 << 42 << 43 );
QTest::newRow( "array_minority(array(0,1,42,42,43,42), 'real_minority')" ) << QStringLiteral( "array_sort(array_minority(array(0,1,42,42,43,42), 'real_minority'))" ) << false << QVariant( QVariantList() << 0 << 1 << 42 << 43 );
QTest::newRow( "array_minority(array(0,1,42,42,43,42,42), 'real_minority')" ) << QStringLiteral( "array_sort(array_minority(array(0,1,42,42,43,42,42), 'real_minority'))" ) << false << QVariant( QVariantList() << 0 << 1 << 43 );
QTest::newRow( "array_sum('forty two')" ) << QStringLiteral( "array_sum('forty two')" ) << true << QVariant();
QTest::newRow( "array_sum(42)" ) << QStringLiteral( "array_sum(42)" ) << true << QVariant();
QTest::newRow( "array_sum(array())" ) << QStringLiteral( "array_sum(array())" ) << false << QVariant();

0 comments on commit 951aa5a

Please sign in to comment.