SeqAn3 3.3.0-rc.1
The Modern C++ library for sequence analysis.
fast_istreambuf_iterator.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
14#pragma once
15
16#include <cassert>
17#include <iterator>
18
20
21namespace seqan3::detail
22{
35template <typename char_t, typename traits_t = std::char_traits<char_t>>
37{
38private:
41
42public:
46 using difference_type = ptrdiff_t;
47 using value_type = char_t;
48 using reference = char_t;
49 using pointer = void;
52
56 fast_istreambuf_iterator() noexcept = default;
59 fast_istreambuf_iterator & operator=(fast_istreambuf_iterator const &) noexcept = default;
60 fast_istreambuf_iterator & operator=(fast_istreambuf_iterator &&) noexcept = default;
61 ~fast_istreambuf_iterator() noexcept = default;
62
64 explicit fast_istreambuf_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 stream_buf->underflow(); // ensure the stream buffer has content on construction
69 }
71
77 {
78 assert(stream_buf != nullptr);
79 if ((stream_buf->gptr() + 1) == stream_buf->egptr())
80 stream_buf->snextc(); // move right, then underflow()
81 else
82 stream_buf->gbump(1);
83 return *this;
84 }
85
87 void operator++(int)
88 {
89 ++(*this);
90 }
92
95 {
96 assert(stream_buf != nullptr);
97 return *stream_buf->gptr();
98 }
99
105 friend bool operator==(fast_istreambuf_iterator const & lhs, std::default_sentinel_t const &) noexcept
106 {
107 assert(lhs.stream_buf != nullptr);
108 // compare size of remaining buffer; since ++ always resizes if possible, safe to compare pointers here
109 return (lhs.stream_buf->gptr() == lhs.stream_buf->egptr());
110 }
111
113 friend bool operator!=(fast_istreambuf_iterator const & lhs, std::default_sentinel_t const &) noexcept
114 {
115 return !(lhs == std::default_sentinel);
116 }
117
119 friend bool operator==(std::default_sentinel_t const &, fast_istreambuf_iterator const & rhs) noexcept
120 {
121 return rhs == std::default_sentinel;
122 }
123
125 friend bool operator!=(std::default_sentinel_t const &, fast_istreambuf_iterator const & rhs) noexcept
126 {
127 return !(rhs == std::default_sentinel);
128 }
130};
131
132} // namespace seqan3::detail
Functionally the same as std::istreambuf_iterator, but faster.
Definition: fast_istreambuf_iterator.hpp:37
ptrdiff_t difference_type
Defaults to ptrdiff_t.
Definition: fast_istreambuf_iterator.hpp:46
void operator++(int)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: fast_istreambuf_iterator.hpp:87
fast_istreambuf_iterator & operator++()
Advance by one and rebuffer if necessary (vtable lookup iff rebuffering).
Definition: fast_istreambuf_iterator.hpp:76
reference operator*() const
Read current value from buffer (no vtable lookup, safe if not at end).
Definition: fast_istreambuf_iterator.hpp:94
fast_istreambuf_iterator() noexcept=default
Defaulted.
char_t reference
The char type of the stream.
Definition: fast_istreambuf_iterator.hpp:48
friend bool operator!=(std::default_sentinel_t const &, fast_istreambuf_iterator const &rhs) noexcept
True if the read buffer is empty; involves no vtable lookup.
Definition: fast_istreambuf_iterator.hpp:125
friend bool operator!=(fast_istreambuf_iterator const &lhs, std::default_sentinel_t const &) noexcept
True if the read buffer is empty; involves no vtable lookup.
Definition: fast_istreambuf_iterator.hpp:113
friend bool operator==(fast_istreambuf_iterator const &lhs, std::default_sentinel_t const &) noexcept
True if the read buffer is not empty; involves no vtable lookup.
Definition: fast_istreambuf_iterator.hpp:105
friend bool operator==(std::default_sentinel_t const &, fast_istreambuf_iterator const &rhs) noexcept
True if the read buffer is not empty; involves no vtable lookup.
Definition: fast_istreambuf_iterator.hpp:119
stream_buffer_exposer< char_t, traits_t > * stream_buf
Down-cast pointer to the stream-buffer.
Definition: fast_istreambuf_iterator.hpp:40
void pointer
Has no pointer type.
Definition: fast_istreambuf_iterator.hpp:49
char_t value_type
The char type of the stream.
Definition: fast_istreambuf_iterator.hpp:47
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides seqan3::detail::stream_buffer_exposer.
Functionally the same as std::basic_streambuf<char_t, traits_t_>, but exposes protected members as pu...
Definition: stream_buffer_exposer.hpp:35