include/boost/capy/buffers/buffer_copy.hpp

100.0% Lines (36/36) 100.0% List of functions (8/9)
buffer_copy.hpp
f(x) Functions (9)
Function Calls Lines Blocks
<unknown function 28> :28 unsigned long boost::capy::detail::buffer_copy_fn::operator()<boost::capy::mutable_buffer, boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::capy::mutable_buffer const&, unsigned long) const :28 116x 100.0% 100.0% unsigned long boost::capy::detail::buffer_copy_fn::operator()<boost::capy::mutable_buffer, std::span<boost::capy::const_buffer const, 18446744073709551615ul> >(boost::capy::mutable_buffer const&, std::span<boost::capy::const_buffer const, 18446744073709551615ul> const&, unsigned long) const :28 87x 100.0% 100.0% unsigned long boost::capy::detail::buffer_copy_fn::operator()<boost::capy::mutable_buffer, std::span<boost::capy::const_buffer, 18446744073709551615ul> >(boost::capy::mutable_buffer const&, std::span<boost::capy::const_buffer, 18446744073709551615ul> const&, unsigned long) const :28 180x 100.0% 96.0% unsigned long boost::capy::detail::buffer_copy_fn::operator()<std::span<boost::capy::mutable_buffer const, 18446744073709551615ul>, boost::capy::mutable_buffer>(std::span<boost::capy::mutable_buffer const, 18446744073709551615ul> const&, boost::capy::mutable_buffer const&, unsigned long) const :28 465x 100.0% 100.0% boost::capy::detail::buffer_copy_fn::operator()<boost::capy::mutable_buffer, boost::capy::mutable_buffer>(boost::capy::mutable_buffer const&, boost::capy::mutable_buffer const&, unsigned long) const::{lambda()#1}::operator()() const :50 116x 95.0% 92.0% boost::capy::detail::buffer_copy_fn::operator()<boost::capy::mutable_buffer, std::span<boost::capy::const_buffer const, 18446744073709551615ul> >(boost::capy::mutable_buffer const&, std::span<boost::capy::const_buffer const, 18446744073709551615ul> const&, unsigned long) const::{lambda()#1}::operator()() const :50 257x 95.0% 92.0% boost::capy::detail::buffer_copy_fn::operator()<boost::capy::mutable_buffer, std::span<boost::capy::const_buffer, 18446744073709551615ul> >(boost::capy::mutable_buffer const&, std::span<boost::capy::const_buffer, 18446744073709551615ul> const&, unsigned long) const::{lambda()#1}::operator()() const :50 180x 90.0% 92.0% boost::capy::detail::buffer_copy_fn::operator()<std::span<boost::capy::mutable_buffer const, 18446744073709551615ul>, boost::capy::mutable_buffer>(std::span<boost::capy::mutable_buffer const, 18446744073709551615ul> const&, boost::capy::mutable_buffer const&, unsigned long) const::{lambda()#1}::operator()() const :50 633x 95.0% 92.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/capy
8 //
9
10 #ifndef BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP
11 #define BOOST_CAPY_BUFFERS_BUFFER_COPY_HPP
12
13 #include <boost/capy/detail/config.hpp>
14 #include <boost/capy/buffers.hpp>
15 #include <cstring>
16 #include <utility>
17
18 namespace boost {
19 namespace capy {
20
21 namespace detail {
22 struct buffer_copy_fn
23 {
24 template<
25 MutableBufferSequence MB,
26 ConstBufferSequence CB>
27 std::size_t
28 12256x operator()(
29 MB const& dest,
30 CB const& src,
31 std::size_t at_most = std::size_t(-1)) const noexcept
32 {
33 12256x std::size_t total = 0;
34 12256x std::size_t pos0 = 0;
35 12256x std::size_t pos1 = 0;
36 12256x auto const end0 = end(src);
37 12256x auto const end1 = end(dest);
38 12256x auto it0 = begin(src);
39 12256x auto it1 = begin(dest);
40 12256x while(
41 38002x total < at_most &&
42 54380x it0 != end0 &&
43 9402x it1 != end1)
44 {
45 19805x const_buffer b0 = *it0;
46 19805x mutable_buffer b1 = *it1;
47 19805x b0 += pos0;
48 19805x b1 += pos1;
49 std::size_t const amount =
50 59415x [&]
51 {
52 19805x std::size_t n = b0.size();
53 19805x if( n > b1.size())
54 5750x n = b1.size();
55 19805x if( n > at_most - total)
56 4902x n = at_most - total;
57 19805x if(n != 0)
58 18953x std::memcpy(
59 b1.data(),
60 b0.data(),
61 n);
62 19805x return n;
63 19805x }();
64 19805x total += amount;
65 19805x if(amount == b1.size())
66 {
67 7301x ++it1;
68 7301x pos1 = 0;
69 }
70 else
71 {
72 12504x pos1 += amount;
73 }
74 19805x if(amount == b0.size())
75 {
76 10569x ++it0;
77 10569x pos0 = 0;
78 }
79 else
80 {
81 9236x pos0 += amount;
82 }
83 }
84 12256x return total;
85 }
86 };
87 } // detail
88
89 /** Copy the contents of a buffer sequence into another buffer sequence.
90
91 This function copies bytes from the constant buffer sequence `src` into
92 the mutable buffer sequence `dest`, stopping when any limit is reached.
93
94 @par Constraints
95 @code
96 MutableBufferSequence<decltype(dest)> &&
97 ConstBufferSequence<decltype(src)>
98 @endcode
99
100 @return The number of bytes copied, equal to
101 `std::min(size(dest), size(src), at_most)`.
102
103 @param dest The destination buffer sequence.
104 @param src The source buffer sequence.
105 @param at_most The maximum bytes to copy. Default copies all available.
106 */
107 constexpr detail::buffer_copy_fn buffer_copy {};
108
109 } // capy
110 } // boost
111
112 #endif
113