Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:05

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/io/Join.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "detail/Joined.hh"  // IWYU pragma: export
0010 
0011 namespace celeritas
0012 {
0013 //---------------------------------------------------------------------------//
0014 /*!
0015  * Join items, similar to python's "str.join" method.
0016  *
0017  * This utility function will concatenate the values passed to it. The given
0018  * conjunction ONLY appears between values.
0019  *
0020  * \code
0021    cout << celeritas::join(foo.begin(), foo.end(), ", ") << endl
0022    \endcode
0023  *
0024  * The result is a thin class that is streamable. (It can explicitly be
0025  * converted to a string with the
0026  * \c to_string method). By doing this instead of returning a std::string,
0027  * large and dynamic containers can be e.g. saved to disk.
0028  */
0029 template<class InputIterator, class Conjunction>
0030 detail::Joined<InputIterator, Conjunction, detail::StreamValue>
0031 join(InputIterator first, InputIterator last, Conjunction&& conjunction)
0032 {
0033     return {first, last, std::forward<Conjunction>(conjunction), {}};
0034 }
0035 
0036 //---------------------------------------------------------------------------//
0037 /*!
0038  * Join items transformed by a helper functor.
0039  *
0040  * This joins all given elements, inserting conjunction betwen them. The 'op'
0041  * operator must transform each element into a printable object. For example,
0042  * \code
0043       [](const std::pair<int, int>& item) { return item->first; }
0044  * \endcode
0045  * could be used to get the 'key' item of a map.
0046  */
0047 template<class InputIterator, class Conjunction, class UnaryOperation>
0048 detail::Joined<InputIterator, Conjunction, detail::UnaryToStream<UnaryOperation>>
0049 join(InputIterator first,
0050      InputIterator last,
0051      Conjunction&& conjunction,
0052      UnaryOperation&& op)
0053 {
0054     return {first,
0055             last,
0056             std::forward<Conjunction>(conjunction),
0057             {std::forward<UnaryOperation>(op)}};
0058 }
0059 
0060 //---------------------------------------------------------------------------//
0061 /*!
0062  * Join using a functor that takes (ostream&, value).
0063  */
0064 template<class InputIterator, class Conjunction, class StreamOp>
0065 detail::Joined<InputIterator, Conjunction, StreamOp>
0066 join_stream(InputIterator first,
0067             InputIterator last,
0068             Conjunction&& conjunction,
0069             StreamOp&& op)
0070 {
0071     return {first,
0072             last,
0073             std::forward<Conjunction>(conjunction),
0074             std::forward<StreamOp>(op)};
0075 }
0076 
0077 //---------------------------------------------------------------------------//
0078 }  // namespace celeritas