A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
indexed_list.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_CORE_INDEXED_LIST_H_
2 #define GENESIS_UTILS_CORE_INDEXED_LIST_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
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@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <memory>
35 #include <vector>
36 
38 
39 namespace genesis {
40 namespace utils {
41 
42 // =================================================================================================
43 // Forward Declarations
44 // =================================================================================================
45 
46 template <typename T>
48 
49 // =================================================================================================
50 // Indexed List
51 // =================================================================================================
52 
58 template <typename T>
60 {
61 public:
62 
63  // -------------------------------------------------------------------------
64  // Member Types
65  // -------------------------------------------------------------------------
66 
67  using value_type = T;
68 
70  using const_reference = const value_type&;
71  using pointer = value_type*;
72  using const_pointer = const value_type*;
73 
76 
77  using size_type = size_t;
78 
79  // -------------------------------------------------------------------------
80  // Constructor and Rule of Five
81  // -------------------------------------------------------------------------
82 
86  IndexedList() = default;
87 
91  explicit IndexedList( size_type n )
92  : IndexedList( n, value_type{} )
93  {}
94 
98  IndexedList( size_type n, value_type const& val )
99  {
100  content_.reserve(n);
101  for( size_t i = 0; i < n; ++i ) {
102  push_back( val );
103  }
104  }
105 
106  // /**
107  // * @brief Range constructor, copies all elements in the range `[ first, last )`.
108  // */
109  // template <class InputIterator>
110  // IndexedList (InputIterator first, InputIterator last)
111  // {
112  // while( first != last ) {
113  // push_back( *first );
114  // ++first;
115  // }
116  // }
117 
121  IndexedList( IndexedList const& other )
122  {
123  content_.clear();
124  content_.reserve( other.size() );
125  for( auto const& up_elem : other.content_ ) {
126  push_back( *up_elem );
127  }
128  }
129 
133  IndexedList( IndexedList&& other ) = default;
134 
138  IndexedList( std::initializer_list<value_type> il )
139  {
140  content_.reserve( il.size() );
141  for( auto const& elem : il ) {
142  push_back( elem );
143  }
144  }
145 
150  {
151  // Copy-swap-idiom.
152  swap( other );
153  return *this;
154  }
155 
159  IndexedList& operator= ( IndexedList&& other ) = default;
160 
164  ~IndexedList() = default;
165 
166  void swap( IndexedList& other )
167  {
168  using std::swap;
169  swap( content_, other.content_ );
170  }
171 
172  // -------------------------------------------------------------------------
173  // Iterators
174  // -------------------------------------------------------------------------
175 
177  {
178  return IndexedListIterator<value_type>( *this );
179  }
180 
182  {
184  }
185 
187  {
188  return IndexedListIterator<value_type>( *this, size() );
189  }
190 
192  {
193  return IndexedListIterator<const value_type>( *this, size() );
194  }
195 
197  {
199  }
200 
202  {
203  return IndexedListIterator<const value_type>( *this, size() );
204  }
205 
206  // -------------------------------------------------------------------------
207  // Capacity
208  // -------------------------------------------------------------------------
209 
210  size_type size() const
211  {
212  return content_.size();
213  }
214 
215  bool empty() const
216  {
217  return content_.empty();
218  }
219 
220  // -------------------------------------------------------------------------
221  // Element Access
222  // -------------------------------------------------------------------------
223 
225  {
226  return *(content_[n]);
227  }
228 
230  {
231  return *(content_[n]);
232  }
233 
235  {
236  return *(content_.at(n));
237  }
238 
240  {
241  return *(content_.at(n));
242  }
243 
245  {
246  return *(content_.front());
247  }
248 
250  {
251  return *(content_.front());
252  }
253 
255  {
256  return *(content_.back());
257  }
258 
260  {
261  return *(content_.back());
262  }
263 
264  // -------------------------------------------------------------------------
265  // Modifiers
266  // -------------------------------------------------------------------------
267 
268  void push_back( value_type const& value )
269  {
270  content_.push_back( utils::make_unique<value_type>( value ));
271  }
272 
273  void push_back( value_type&& value )
274  {
275  content_.push_back( utils::make_unique<value_type>( std::move( value )));
276  }
277 
278  // -------------------------------------------------------------------------
279  // Data Members
280  // -------------------------------------------------------------------------
281 
282 private:
283 
284  std::vector< std::unique_ptr< value_type >> content_;
285 };
286 
287 // =================================================================================================
288 // Indexed List Iterator
289 // =================================================================================================
290 
291 template <typename T>
292 class IndexedListIterator
293 {
294 public:
295 
296  // -------------------------------------------------------------------------
297  // Member Types
298  // -------------------------------------------------------------------------
299 
300  using value_type = T;
302 
304  using const_reference = const value_type&;
305  using pointer = value_type*;
306  using const_pointer = const value_type*;
307 
308  using size_type = size_t;
309 
310  // -------------------------------------------------------------------------
311  // Constructor and Rule of Five
312  // -------------------------------------------------------------------------
313 
314  // IndexedListIterator()
315  // : pos_(0)
316  // , list_(il)
317  // {}
318 
320  : pos_(0)
321  , list_(il)
322  {}
323 
325  : pos_(pos)
326  , list_(il)
327  {}
328 
329  ~IndexedListIterator() = default;
330 
331  IndexedListIterator( IndexedListIterator const& x ) = default;
333 
334  IndexedListIterator& operator= ( IndexedListIterator const& x ) = default;
336 
337  // -------------------------------------------------------------
338  // Accessors
339  // -------------------------------------------------------------
340 
342  {
343  return list_[pos_];
344  }
345 
347  {
348  return &(list_[pos_]);
349  }
350 
351  // -------------------------------------------------------------
352  // Iteration
353  // -------------------------------------------------------------
354 
356  {
357  ++pos_;
358  return *this;
359  }
360 
362  {
363  self_type tmp = *this;
364  ++(*this);
365  return tmp;
366  }
367 
368  bool operator == (self_type const& other) const
369  {
370  return &list_ == &(other.list_) && pos_ == other.pos_;
371  }
372 
373  bool operator != (self_type const& other) const
374  {
375  return !(*this == other);
376  }
377 
378  // -------------------------------------------------------------------------
379  // Data Members
380  // -------------------------------------------------------------------------
381 
382 private:
383 
384  size_type pos_;
386 };
387 
388 } // namespace utils
389 } // namespace genesis
390 
391 #endif // include guard
reference operator[](size_type n)
void push_back(value_type const &value)
const_iterator begin() const
const_reference back() const
void swap(SequenceSet &lhs, SequenceSet &rhs)
void swap(IndexedList &other)
const_reference at(size_type n) const
IndexedList & operator=(IndexedList other)
Copy assignment.
void push_back(value_type &&value)
const_iterator cend() const
bool operator!=(self_type const &other) const
Provides some valuable additions to STD.
IndexedList(std::initializer_list< value_type > il)
Construct from initializer list by copying all elements.
IndexedListIterator(IndexedList< value_type > &il, size_type pos)
IndexedList(IndexedList const &other)
Range constructor, copies all elements in the range [ first, last ).
IndexedListIterator & operator=(IndexedListIterator const &x)=default
~IndexedList()=default
Destructor.
IndexedListIterator(IndexedList< value_type > &il)
const_iterator end() const
IndexedList(size_type n, value_type const &val)
Fill constructor, using a certain value to initialize all elements.
bool operator==(self_type const &other) const
IndexedList(size_type n)
Fill constructor with default initialization of the values.
reference at(size_type n)
http://www.boost.org/doc/libs/1_59_0/doc/html/container/non_standard_containers.html ...
IndexedList()=default
Default constructor.
const_iterator cbegin() const
const_reference front() const