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