Workaround for ranges

Not fully supported by all compilers

Reverted to C++20.
Introduced ArrayOps namespace.
This commit is contained in:
Valentino Orlandi 2024-02-07 23:58:06 +01:00
parent 354e43ba46
commit 455394d112
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
4 changed files with 161 additions and 11 deletions

View File

@ -8,7 +8,7 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(MSVC)
@ -57,6 +57,7 @@ set(PROJECT_SOURCES
customs/models/logfields_listmodel.h
customs/models/logfields_listmodel.cpp
utilities/arrays.h
utilities/bwlists.h
utilities/bwutils.cpp
utilities/chars.h

View File

@ -3,6 +3,7 @@
#include "globals/db_names.h"
#include "utilities/arrays.h"
#include "utilities/printables.h"
#include "utilities/strings.h"
#include "utilities/vectors.h"
@ -14,8 +15,6 @@
#include "modules/crapview/modules/lib.h"
#include <ranges>
#include <QGraphicsItem>
#include <QTableWidget>
@ -932,11 +931,17 @@ bool Crapview::calcGlobals( std::vector<std::tuple<QString,QString>>& recur_list
{
double max_c{ 0.0 };
size_t max_i{ traf.size() };
std::ranges::for_each( std::views::enumerate(traf),
/*std::ranges::for_each( std::views::enumerate(traf),
[&max_c,&max_i](const auto ic)
{ if (auto& [i,c]{ic}; c>max_c){ max_c=c; max_i=i; } });
{ if (auto& [i,c]{ic}; c>max_c){ max_c=c; max_i=i; } });*/
for( const auto [index,count] : ArrayOps::enumerate(traf) ) {
if ( count > max_c ) {
max_c = count;
max_i = index;
}
};
if ( max_i == traf.size() ) {
traffic_list.push_back( std::make_tuple( __dash, __zero ) );
traffic_list.emplace_back( __dash, __zero );
} else {
const size_t f{ static_cast<size_t>(max_c) };
const size_t d{ max_c<10.0 ? static_cast<size_t>(max_c*100.0)%100ul : static_cast<size_t>(max_c*10.0)%10ul };
@ -944,9 +949,9 @@ bool Crapview::calcGlobals( std::vector<std::tuple<QString,QString>>& recur_list
if ( d > 0 ) {
count += QString::number( d ).prepend(QLatin1Char('.'));
}
QString value{ op ? TR::tr(this->dbQuery.DAYS.at(max_i+1).c_str())
: QStringLiteral("%1").arg(max_i, 2, 10, QChar('0')) };
traffic_list.push_back( std::make_tuple( value, count ) );
const QString value{ op ? TR::tr(this->dbQuery.DAYS.at(max_i+1).c_str())
: QStringLiteral("%1").arg(max_i, 2, 10, QChar('0')) };
traffic_list.emplace_back( value, count );
}
}
};

View File

@ -6,6 +6,7 @@
#include "modules/dialogs.h"
#include "modules/exceptions.h"
#include "utilities/arrays.h"
#include "utilities/printables.h"
#include "utilities/strings.h"
@ -13,7 +14,6 @@
#include <map>
#include <vector>
#include <ranges>
int toInt( const QString& str )
@ -1338,7 +1338,9 @@ void DbQuery::getGlobalCounts( std::optional<GlobalsData>& result, QStringView w
}
// process the day of the week
for ( auto [total,count] : std::views::zip( data.traf.day, week_days_count ) ) {
/*std::ranges::for_each( std::views::zip( data.traf.day, week_days_count ),
[](auto tc){ if (auto& [t,c]{tc}; c>0.0){ t/=c; } });*/
for ( auto [total,count] : ArrayOps::zip( data.traf.day, week_days_count ) ) {
if ( count > 0.0 ) {
total /= count;
}

View File

@ -0,0 +1,142 @@
#ifndef LOGDOCTOR__ARRAYOPS_H
#define LOGDOCTOR__ARRAYOPS_H
#include <array>
#include <tuple>
template <typename Array>
struct Zipperator
{
using array_value = Array::value_type;
using array_iterator = Array::iterator;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = std::tuple<array_value&, array_value&>;
using pointer = std::tuple<array_value&, array_value&>;
using reference = std::tuple<array_value&, array_value&>;
explicit Zipperator( array_iterator l, array_iterator r ) noexcept
: lit{l},rit{r} {}
inline reference operator*()
{ return std::tuple<array_value&, array_value&>(*lit,*rit); }
inline Zipperator& operator++() noexcept
{ lit++; rit++; return *this; }
friend bool operator!=( const Zipperator& lhs, const Zipperator& rhs ) noexcept
{ return lhs.lit != rhs.lit and lhs.rit != rhs.rit; }
private:
array_iterator lit;
array_iterator rit;
};
template <typename Array>
struct Enumerator
{
using array_size_t = Array::size_type;
using array_value = Array::value_type;
using array_iterator = Array::const_iterator;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = std::tuple<array_size_t, array_value>;
using pointer = std::tuple<array_size_t, array_value>;
using reference = std::tuple<array_size_t, array_value>;
explicit Enumerator( array_iterator a ) noexcept
: idx{0},iter{a} {}
inline reference operator*()
{ return std::make_tuple(idx,*iter); }
inline Enumerator& operator++() noexcept
{ idx++; iter++; return *this; }
friend bool operator!=( const Enumerator& lhs, const Enumerator& rhs ) noexcept
{ return lhs.iter != rhs.iter; }
private:
array_size_t idx;
array_iterator iter;
};
template <typename Array>
class ZippedArrays
{
Array& larr;
Array& rarr;
public:
explicit ZippedArrays( Array& l, Array& r )
: larr{l},rarr{r} {}
inline auto begin() noexcept
{ return Zipperator<Array>(larr.begin(),rarr.begin()); }
inline auto end() noexcept
{ return Zipperator<Array>(larr.end(),rarr.end()); }
};
template <typename Array>
class EnumeratdArray
{
const Array& arr;
public:
explicit EnumeratdArray( const Array& a )
: arr{a} {}
inline auto begin() noexcept
{ return Enumerator<Array>(arr.cbegin()); }
inline auto end() noexcept
{ return Enumerator<Array>(arr.cend()); }
};
//! ArrayOps
/*!
Utilities for the arrays
*/
namespace ArrayOps
{
//! Zips two arrays
/*!
\param l_array The left-side array
\param r_array The right-side array
\return An iterator over the two arrays
\todo Replace with std::views::zip when clang will fully support it
*/
template <typename Array>
inline auto zip( Array& l_array, Array& r_array )
{
return ZippedArrays<Array>( l_array, r_array );
}
//! Enumerates an array
/*!
\param array The array
\return An iterator over the two array along with the index
\todo Replace with std::views::enumerate when clang will fully support it
*/
template <typename Array>
inline auto enumerate( const Array& array )
{
return EnumeratdArray<Array>( array );
}
} // namespace ArrayOps
#endif // LOGDOCTOR__ARRAYOPS_H