forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Join.hh
79 lines (74 loc) · 2.79 KB
/
Join.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/io/Join.hh
//---------------------------------------------------------------------------//
#pragma once
#include "detail/Joined.hh" // IWYU pragma: export
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Join items, similar to python's "str.join" method.
*
* This utility function will concatenate the values passed to it. The given
* conjunction ONLY appears between values.
*
* \code
cout << celeritas::join(foo.begin(), foo.end(), ", ") << endl
\endcode
*
* The result is a thin class that is streamable. (It can explicitly be
* converted to a string with the
* \c to_string method). By doing this instead of returning a std::string,
* large and dynamic containers can be e.g. saved to disk.
*/
template<class InputIterator, class Conjunction>
detail::Joined<InputIterator, Conjunction, detail::StreamValue>
join(InputIterator first, InputIterator last, Conjunction&& conjunction)
{
return {first, last, std::forward<Conjunction>(conjunction), {}};
}
//---------------------------------------------------------------------------//
/*!
* Join items transformed by a helper functor.
*
* This joins all given elements, inserting conjunction betwen them. The 'op'
* operator must transform each element into a printable object. For example,
* \code
[](const std::pair<int, int>& item) { return item->first; }
* \endcode
* could be used to get the 'key' item of a map.
*/
template<class InputIterator, class Conjunction, class UnaryOperation>
detail::Joined<InputIterator, Conjunction, detail::UnaryToStream<UnaryOperation>>
join(InputIterator first,
InputIterator last,
Conjunction&& conjunction,
UnaryOperation&& op)
{
return {first,
last,
std::forward<Conjunction>(conjunction),
{std::forward<UnaryOperation>(op)}};
}
//---------------------------------------------------------------------------//
/*!
* Join using a functor that takes (ostream&, value).
*/
template<class InputIterator, class Conjunction, class StreamOp>
detail::Joined<InputIterator, Conjunction, StreamOp>
join_stream(InputIterator first,
InputIterator last,
Conjunction&& conjunction,
StreamOp&& op)
{
return {first,
last,
std::forward<Conjunction>(conjunction),
std::forward<StreamOp>(op)};
}
//---------------------------------------------------------------------------//
} // namespace celeritas