A library for working with phylogenetic and population genetic data.
v0.32.0
window_view_stream.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_POPULATION_WINDOW_WINDOW_VIEW_STREAM_H_
2 #define GENESIS_POPULATION_WINDOW_WINDOW_VIEW_STREAM_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2024 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@sund.ku.dk>
23  University of Copenhagen, Globe Institute, Section for GeoGenetics
24  Oster Voldgade 5-7, 1350 Copenhagen K, Denmark
25 */
26 
37 
39 
40 #include <cassert>
41 #include <memory>
42 #include <vector>
43 
44 namespace genesis {
45 namespace population {
46 
47 // =================================================================================================
48 // Window View Stream
49 // =================================================================================================
50 
73 template<class InputStreamIterator, class Data = typename InputStreamIterator::value_type>
74 class WindowViewStream final : public BaseWindowStream<
75  InputStreamIterator, Data, ::genesis::population::WindowView<Data>
76 >
77 {
78 public:
79 
80  // -------------------------------------------------------------------------
81  // Typedefs and Enums
82  // -------------------------------------------------------------------------
83 
84  using InputStreamType = InputStreamIterator;
85  using DataType = Data;
88 
91 
93 
94  static_assert(
95  std::is_same<WindowViewStream, self_type>::value, "WindowViewStream != self_type"
96  );
97 
98  // The input types that we take from the underlying iterator over genome positions.
99  using InputType = typename InputStreamIterator::value_type;
100  // using Entry = typename Window::Entry;
102 
103  // This class produces an iterator of type WindowView.
104  // That WindowView then iterates over the actual values of the input.
105  using iterator_category = std::input_iterator_tag;
107  using pointer = value_type*;
109  using const_reference = value_type const&;
110 
111  // ======================================================================================
112  // Internal Iterator
113  // ======================================================================================
114 
118  class DerivedIterator final : public BaseWindowStream<
119  InputStreamIterator, DataType, WindowViewType
120  >::BaseIterator
121  {
122  public:
123 
124  // -------------------------------------------------------------------------
125  // Constructors and Rule of Five
126  // -------------------------------------------------------------------------
127 
128  using self_type = typename WindowViewStream<
129  InputStreamIterator, DataType
131 
132  // using base_iterator_type = typename base_type::BaseIterator;
133  using base_iterator_type = typename BaseWindowStream<
134  InputStreamIterator, DataType, WindowViewType
136 
137  // using WindowViewType = WindowViewType;
138  // using Window = ::genesis::population::Window<DataType>;
139  // using Entry = typename Window::Entry;
140  using InputType = typename InputStreamIterator::value_type;
141 
142  using iterator_category = std::input_iterator_tag;
144  using pointer = value_type*;
146  using const_reference = value_type const&;
147 
148  static_assert(
149  std::is_same<DerivedIterator, self_type>::value, "DerivedIterator != self_type"
150  );
151 
152  private:
153 
154  DerivedIterator() = default;
155 
157  WindowViewStream const* parent
158  )
159  // We here call the base class constructor without arguments,
160  // in order to not trigger the full setup of the base class, which we don't want here.
161  // Hella hacky, but works.
163  , parent_( parent )
164  {
165  // Edge case check. See Base for details.
166  if( ! parent_ ) {
167  return;
168  }
169 
170  // Store the underlying window iterators.
171  current_ = parent_->window_stream_->begin();
172  end_ = parent_->window_stream_->end();
173 
174  // Edge case for empty data.
175  if( current_ == end_ ) {
176  parent_ = nullptr;
177  return;
178  }
179 
180  // Start a view into the first window. This creates a view that mirrors the underlying
181  // window, and iteratres through it, using the WindowView constructor that takes a Window.
182  window_view_ = WindowViewType{ *current_ };
183  }
184 
185  public:
186 
187  virtual ~DerivedIterator() override = default;
188 
189  DerivedIterator( self_type const& ) = default;
190  DerivedIterator( self_type&& ) = default;
191 
192  DerivedIterator& operator= ( self_type const& ) = default;
193  DerivedIterator& operator= ( self_type&& ) = default;
194 
196 
197  // -------------------------------------------------------------------------
198  // Internal and Virtual Members
199  // -------------------------------------------------------------------------
200 
201  private:
202 
203  void increment_() override final
204  {
205  // Check that we are still good. If not, this function being called is likely a user
206  // error by trying to increment a past-the-end iterator.
207  assert( parent_ );
208 
209  // Increment the window
210  ++current_;
211  if( current_ == end_ ) {
212  parent_ = nullptr;
213  return;
214  }
215 
216  // Start a view into the new window. This creates a view that mirrors the underlying
217  // window, and iteratres through it, using the WindowView constructor that takes a Window.
218  window_view_ = WindowViewType{ *current_ };
219  }
220 
221  value_type& get_current_window_() const override final
222  {
223  return const_cast<value_type&>( window_view_ );
224  }
225 
226  base_type const* get_parent_() const override final
227  {
228  return parent_;
229  }
230 
231  private:
232 
233  // Parent. Needs to live here to have the correct derived type.
234  WindowViewStream const* parent_ = nullptr;
235 
236  // Base window iterators
239 
240  // Store the iterator for the window.
241  WindowViewType window_view_;
242 
243  };
244 
245  // ======================================================================================
246  // Main Class
247  // ======================================================================================
248 
249  // -------------------------------------------------------------------------
250  // Constructors and Rule of Five
251  // -------------------------------------------------------------------------
252 
254  std::unique_ptr<WrappedWindowStream> window_iterator
255  )
256  : window_stream_( std::move( window_iterator ))
257  {}
258 
259  virtual ~WindowViewStream() override = default;
260 
261  WindowViewStream( WindowViewStream const& ) = default;
262  WindowViewStream( WindowViewStream&& ) = default;
263 
264  WindowViewStream& operator= ( WindowViewStream const& ) = default;
266 
268 
269  // -------------------------------------------------------------------------
270  // Virtual Members
271  // -------------------------------------------------------------------------
272 
273 protected:
274 
275  std::unique_ptr<typename base_type::BaseIterator>
276  get_begin_iterator_() override final
277  {
278  // Cannot use make_unique here, as the Iterator constructor is private,
279  // and trying to make make_unique a friend does not seem to be working...
280  return std::unique_ptr<DerivedIterator>( new DerivedIterator( this ));
281  // return utils::make_unique<DerivedIterator>( this );
282  }
283 
284  std::unique_ptr<typename base_type::BaseIterator>
285  get_end_iterator_() override final
286  {
287  return std::unique_ptr<DerivedIterator>( new DerivedIterator( nullptr ));
288  // return utils::make_unique<DerivedIterator>( nullptr );
289  }
290 
291  // -------------------------------------------------------------------------
292  // Members
293  // -------------------------------------------------------------------------
294 
295 private:
296 
297  // Need a pointer here, in order to allow to take derived classes.
298  std::unique_ptr<WrappedWindowStream> window_stream_;
299 
300 };
301 
302 // =================================================================================================
303 // Make Window View Iterator
304 // =================================================================================================
305 
315 template<class T>
316 WindowViewStream<typename T::InputStreamType, typename T::DataType>
318  T const& window_iterator
319 ) {
320  using InputStreamType = typename T::InputStreamType;
321  using DataType = typename T::DataType;
322  using BaseWindowStreamType = BaseWindowStream<InputStreamType, DataType>;
323 
325  std::unique_ptr<BaseWindowStreamType>( new T{ window_iterator })
326  );
327 }
328 
335 template<class T>
336 WindowViewStream<typename T::InputStreamType, typename T::DataType>
338  T&& window_iterator
339 ) {
340  using InputStreamType = typename T::InputStreamType;
341  using DataType = typename T::DataType;
342  using BaseWindowStreamType = BaseWindowStream<InputStreamType, DataType>;
343 
345  std::unique_ptr<BaseWindowStreamType>( new T{ std::move( window_iterator )})
346  );
347 }
348 
349 } // namespace population
350 } // namespace genesis
351 
352 #endif // include guard
genesis::population::BaseWindowStream
Base class for streams of Windows over the chromosomes of a genome.
Definition: base_window_stream.hpp:119
genesis::population::BaseWindowStream::BaseIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: base_window_stream.hpp:491
genesis::population::WindowViewStream::DerivedIterator::base_iterator_type
typename BaseWindowStream< InputStreamIterator, DataType, WindowViewType >::BaseIterator base_iterator_type
Definition: window_view_stream.hpp:135
base_window_stream.hpp
genesis::population::WindowViewStream::const_reference
value_type const & const_reference
Definition: window_view_stream.hpp:109
genesis::population::WindowViewStream::~WindowViewStream
virtual ~WindowViewStream() override=default
genesis::population::WindowViewStream::DerivedIterator::~DerivedIterator
virtual ~DerivedIterator() override=default
genesis::population::WindowViewStream::WindowViewStream
WindowViewStream(std::unique_ptr< WrappedWindowStream > window_iterator)
Definition: window_view_stream.hpp:253
genesis::population::WindowViewStream::DerivedIterator::operator=
DerivedIterator & operator=(self_type const &)=default
window_view.hpp
genesis::population::BaseWindowStream::BaseIterator::value_type
WindowType value_type
Definition: base_window_stream.hpp:492
genesis::population::WindowViewStream::DerivedIterator
Internal iterator that produces WindowViews.
Definition: window_view_stream.hpp:118
std.hpp
Provides some valuable additions to STD.
genesis::population::BaseWindowStream::BaseIterator::const_reference
value_type const & const_reference
Definition: base_window_stream.hpp:495
genesis::population::WindowViewStream::operator=
WindowViewStream & operator=(WindowViewStream const &)=default
genesis::population::BaseWindowStream::Iterator
friend Iterator
Definition: base_window_stream.hpp:633
genesis::population::WindowViewStream::DerivedIterator::self_type
typename WindowViewStream< InputStreamIterator, DataType >::DerivedIterator self_type
Definition: window_view_stream.hpp:130
genesis::population::WindowViewStream::DataType
Data DataType
Definition: window_view_stream.hpp:85
genesis::population::WindowViewStream::WindowType
::genesis::population::WindowView< DataType > WindowType
Definition: window_view_stream.hpp:86
genesis::population::WindowViewStream::base_type
BaseWindowStream< InputStreamType, DataType, WindowViewType > base_type
Definition: window_view_stream.hpp:89
genesis::population::BaseWindowStream::BaseIterator::BaseIterator
BaseIterator()=default
genesis::population::BaseWindowStream::BaseIterator::self_type
typename BaseWindowStream< InputStreamType, DataType, WindowType >::BaseIterator self_type
Definition: base_window_stream.hpp:488
genesis::population::WindowView< DataType >
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::population::WindowViewStream::InputStreamType
InputStreamIterator InputStreamType
Definition: window_view_stream.hpp:84
genesis::population::WindowViewStream::WindowViewType
WindowType WindowViewType
Definition: window_view_stream.hpp:87
window.hpp
genesis::population::WindowViewStream
Stream wrapper that turns a BaseWindowStream over Window into a BaseWindowStream over WindowView.
Definition: window_view_stream.hpp:74
genesis::population::make_window_view_stream
WindowViewStream< typename T::InputStreamType, typename T::DataType > make_window_view_stream(T const &window_iterator)
Create a WindowViewStream that iterates some underlying BaseWindowStream.
Definition: window_view_stream.hpp:317
genesis::population::WindowViewStream::InputType
typename InputStreamIterator::value_type InputType
Definition: window_view_stream.hpp:99
genesis::population::WindowViewStream::get_end_iterator_
std::unique_ptr< typename base_type::BaseIterator > get_end_iterator_() override final
Get the end iterator.
Definition: window_view_stream.hpp:285
genesis::population::BaseWindowStream::BaseIterator::pointer
value_type * pointer
Definition: base_window_stream.hpp:493
genesis::population::WindowViewStream::iterator_category
std::input_iterator_tag iterator_category
Definition: window_view_stream.hpp:105
genesis::population::WindowViewStream::DerivedIterator
friend DerivedIterator
Definition: window_view_stream.hpp:267
genesis::population::WindowViewStream::WrappedWindowStreamIterator
typename WrappedWindowStream::Iterator WrappedWindowStreamIterator
Definition: window_view_stream.hpp:101
genesis::population::WindowViewStream::DerivedIterator::WindowViewStream
friend WindowViewStream
Definition: window_view_stream.hpp:195
genesis::population::BaseWindowStream::BaseIterator::reference
value_type & reference
Definition: base_window_stream.hpp:494
genesis::population::BaseWindowStream::BaseIterator::InputType
typename InputStreamType::value_type InputType
Definition: base_window_stream.hpp:489
genesis::population::WindowViewStream::get_begin_iterator_
std::unique_ptr< typename base_type::BaseIterator > get_begin_iterator_() override final
Get the begin iterator.
Definition: window_view_stream.hpp:276