The Gaudi Framework  master (48d4dc99)
Loading...
Searching...
No Matches
zip.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12#include <GaudiKernel/System.h>
13#ifndef NDEBUG
15# include <sstream>
16#endif
17#include <utility>
18
19#if defined( __cpp_lib_ranges_zip ) && defined( __cpp_lib_ranges_as_const )
20# define GAUDI_FUNCTIONAL_USES_STD_RANGES 1
21# include <ranges>
22#else
23# include <range/v3/view/const.hpp>
24# include <range/v3/view/zip.hpp>
25#endif
26
28 // CRJ : Stuff for zipping
29 namespace zip {
30
32 template <typename OS, typename Arg>
33 OS& printSizes( OS& out, Arg&& arg ) {
34 out << "SizeOf'" << System::typeinfoName( typeid( Arg ) ) << "'=" << arg.size();
35 return out;
36 }
37
39 template <typename OS, typename Arg, typename... Args>
40 requires( sizeof...( Args ) > 0 )
41 OS& printSizes( OS& out, Arg&& arg, Args&&... args ) {
42 printSizes( out, arg ) << ", ";
43 return printSizes( out, args... );
44 }
45
47 template <typename A, typename... Rest>
48 bool check_sizes( const A& first, const Rest&... rest ) noexcept {
49 return ( ( first.size() == rest.size() ) && ... );
50 }
51
52#ifndef NDEBUG
54 template <typename... Args>
55 inline decltype( auto ) verifySizes( Args&... args ) {
56 if ( !check_sizes( args... ) ) {
57 std::ostringstream mess;
58 mess << "Zipped containers have different sizes : ";
59 printSizes( mess, args... );
60 throw GaudiException( mess.str(), "Gaudi::Functional::details::zip::verifySizes", StatusCode::FAILURE );
61 }
62 }
63#endif
64
66 template <typename... Args>
67 inline decltype( auto ) range( Args&&... args ) {
68#ifndef NDEBUG
69 verifySizes( args... );
70#endif
71#if defined( GAUDI_FUNCTIONAL_USES_STD_RANGES )
72 return std::ranges::zip_view( std::forward<Args>( args )... );
73#else
74 return ranges::views::zip( std::forward<Args>( args )... );
75#endif
76 }
77
79 template <typename... Args>
80 inline decltype( auto ) const_range( Args&&... args ) {
81#ifndef NDEBUG
82 verifySizes( args... );
83#endif
84#if defined( GAUDI_FUNCTIONAL_USES_STD_RANGES )
85 return std::ranges::as_const_view( std::ranges::zip_view( std::forward<Args>( args )... ) );
86#else
87 return ranges::views::const_( ranges::views::zip( std::forward<Args>( args )... ) );
88#endif
89 }
90 } // namespace zip
91} // namespace Gaudi::Functional::details
Define general base for Gaudi exception.
constexpr static const auto FAILURE
Definition StatusCode.h:100
OS & printSizes(OS &out, Arg &&arg)
Print the parameter.
Definition zip.h:33
decltype(auto) verifySizes(Args &... args)
Verify the data container sizes have the same sizes.
Definition zip.h:55
decltype(auto) const_range(Args &&... args)
Zips multiple containers together to form a single const range.
Definition zip.h:80
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition zip.h:67
bool check_sizes(const A &first, const Rest &... rest) noexcept
Compare sizes of 1 or more containers.
Definition zip.h:48
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260