SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
fast_ostreambuf_iterator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
11#pragma once
12
13#include <algorithm>
14#include <cassert>
15#include <iterator>
16#include <ranges>
17#include <seqan3/std/charconv>
18
20
21namespace seqan3::detail
22{
35template <typename char_t, typename traits_t = std::char_traits<char_t>>
36class fast_ostreambuf_iterator
37{
38private:
40 stream_buffer_exposer<char_t, traits_t> * stream_buf = nullptr;
41
42public:
46 using difference_type = ptrdiff_t;
47 using value_type = char_t;
48 using reference = char_t;
49 using pointer = void;
50 using iterator_category = std::output_iterator_tag;
52
56 fast_ostreambuf_iterator() noexcept = default;
57 fast_ostreambuf_iterator(fast_ostreambuf_iterator const &) noexcept = default;
58 fast_ostreambuf_iterator(fast_ostreambuf_iterator &&) noexcept = default;
59 fast_ostreambuf_iterator & operator=(fast_ostreambuf_iterator const &) noexcept = default;
60 fast_ostreambuf_iterator & operator=(fast_ostreambuf_iterator &&) noexcept = default;
61 ~fast_ostreambuf_iterator() noexcept = default;
62
64 explicit fast_ostreambuf_iterator(std::basic_streambuf<char_t, traits_t> & ibuf) :
65 stream_buf{reinterpret_cast<stream_buffer_exposer<char_t, traits_t> *>(&ibuf)}
66 {
67 assert(stream_buf != nullptr);
68 if (stream_buf->pptr() == stream_buf->epptr())
69 stream_buf->overflow(); // ensures that put area has space available
70 }
72
77 fast_ostreambuf_iterator & operator++()
78 {
79 return *this;
80 }
82 fast_ostreambuf_iterator & operator++(int)
83 {
84 return *this;
85 }
87
89 fast_ostreambuf_iterator & operator*()
90 {
91 return *this;
92 }
93
95 fast_ostreambuf_iterator & operator=(char_t const c)
96 {
97 assert(stream_buf != nullptr);
98 if (stream_buf->pptr() == stream_buf->epptr())
99 {
100 if (stream_buf->sputc(c) == traits_t::eof()) // overflow() [virtual], then write character
101 {
102 // LCOV_EXCL_START
103 throw std::ios_base::failure{"Cannot write to output stream (reached traits::eof() condition)."};
104 // LCOV_EXCL_STOP
105 }
106 }
107 else
108 {
109 *stream_buf->pptr() = c;
110 stream_buf->pbump(1); // advance pptr() in put area without any checks
111 }
112 return *this;
113 }
114
116 bool failed() const noexcept
117 {
118 return stream_buf->overflow() == traits_t::eof();
119 }
120
139 template <std::ranges::forward_range range_type>
140 requires std::ranges::borrowed_range<range_type>
141 auto write_range(range_type && rng)
142 {
143 using sen_t = std::ranges::sentinel_t<range_type>;
144 using it_t = std::ranges::iterator_t<range_type>;
145
146 it_t it = std::ranges::begin(rng);
147 sen_t end = std::ranges::end(rng);
148
149 while (it != end)
150 {
151 size_t const buffer_space = stream_buf->epptr() - stream_buf->pptr();
152
153 if constexpr (std::ranges::sized_range<range_type>)
154 {
155 size_t const characters_to_write = std::min<size_t>(std::ranges::distance(it, end), buffer_space);
156 auto copy_res = std::ranges::copy_n(it, characters_to_write, stream_buf->pptr());
157 it = copy_res.in;
158 stream_buf->pbump(characters_to_write);
159 }
160 else
161 {
162 size_t i = 0;
163 for (; it != end && i < buffer_space; ++it, ++i)
164 *stream_buf->pptr() = *it;
165 stream_buf->pbump(i);
166 }
167
168 if (it == end) // no more characters to write
169 return it;
170
171 // Push one more character and flush
172 if (stream_buf->overflow(*it) == traits_t::eof())
173 {
174 // LCOV_EXCL_START
175 throw std::ios_base::failure{"Cannot write to output stream (reached traits::eof() condition)."};
176 // LCOV_EXCL_STOP
177 }
178
179 ++it; // drop 1 character that has been written in overflow()
180 }
181
182 return it;
183 }
184
186 // overload for non-std::ranges::borrowed_range types that return void
187 template <std::ranges::forward_range range_type>
188 void write_range(range_type && rng)
189 {
190 write_range(rng); // lvalue is always a safe range. return value is ignored because iterator would be dangling
191 }
193
198 template <typename number_type>
199 requires std::is_arithmetic_v<number_type>
200 auto write_number(number_type num)
201 {
202 if (stream_buf->epptr() - stream_buf->pptr() > 300) // enough space for any number, should be likely
203 {
204 auto res = std::to_chars(stream_buf->pptr(), stream_buf->epptr(), num);
205 stream_buf->pbump(res.ptr - stream_buf->pptr()); // advance pptr
206 }
207 else
208 {
209 std::array<char, 300> arithmetic_buffer{};
210 auto res = std::to_chars(&arithmetic_buffer[0], &arithmetic_buffer[0] + sizeof(arithmetic_buffer), num);
211 write_range(std::ranges::subrange<char *, char *>(&arithmetic_buffer[0], res.ptr));
212 }
213 }
214
219 void write_end_of_line(bool const add_cr)
220 {
221 if (add_cr)
222 *this = '\r';
223 *this = '\n';
224 }
225};
226
227} // namespace seqan3::detail
T begin(T... args)
The <charconv> header from C++17's standard library.
T copy_n(T... args)
T end(T... args)
SeqAn specific customisations in the standard namespace.
Provides seqan3::detail::stream_buffer_exposer.
T to_chars(T... args)
Hide me