A library for working with phylogenetic and population genetic data.
v0.32.0
sequence_set.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_SEQUENCE_SEQUENCE_SET_H_
2 #define GENESIS_SEQUENCE_SEQUENCE_SET_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2023 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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
25 */
26 
35 
36 #include <stdexcept>
37 #include <vector>
38 
39 namespace genesis {
40 namespace sequence {
41 
42 // =================================================================================================
43 // SequenceSet
44 // =================================================================================================
45 
54 {
55 public:
56 
57  // -------------------------------------------------------------------------
58  // Typedefs and Enums
59  // -------------------------------------------------------------------------
60 
61  using iterator = std::vector<Sequence>::iterator;
62  using const_iterator = std::vector<Sequence>::const_iterator;
63 
64  using reference = Sequence&;
65  using const_reference = Sequence const&;
66 
67  // -------------------------------------------------------------------------
68  // Constructors and Rule of Five
69  // -------------------------------------------------------------------------
70 
71  SequenceSet() = default;
72  ~SequenceSet() = default;
73 
74  SequenceSet( SequenceSet const& ) = default;
75  SequenceSet( SequenceSet&& ) = default;
76 
77  SequenceSet& operator= ( SequenceSet const& ) = default;
78  SequenceSet& operator= ( SequenceSet&& ) = default;
79 
80  friend void swap( SequenceSet& lhs, SequenceSet& rhs )
81  {
82  using std::swap;
83  swap( lhs.sequences_, rhs.sequences_ );
84  }
85 
86  // -------------------------------------------------------------------------
87  // Accessors
88  // -------------------------------------------------------------------------
89 
93  size_t size() const
94  {
95  return sequences_.size();
96  }
97 
101  bool empty() const
102  {
103  return sequences_.empty();
104  }
105 
106  reference at ( size_t index )
107  {
108  return sequences_.at( index );
109  }
110 
111  const_reference at ( size_t index ) const
112  {
113  return sequences_.at( index );
114  }
115 
116  reference operator[] ( size_t index )
117  {
118  return sequences_[ index ];
119  }
120 
121  const_reference operator[] ( size_t index ) const
122  {
123  return sequences_[ index ];
124  }
125 
126  // -------------------------------------------------------------------------
127  // Modifiers
128  // -------------------------------------------------------------------------
129 
133  reference add( Sequence const& s )
134  {
135  sequences_.push_back( s );
136  return sequences_.back();
137  }
138 
143  {
144  sequences_.push_back( std::move(s) );
145  return sequences_.back();
146  }
147 
151  void remove( size_t index )
152  {
153  if( index >= sequences_.size() ) {
154  throw std::out_of_range( "Index out of range for removing from SequenceSet." );
155  }
156 
157  sequences_.erase( sequences_.begin() + index );
158  }
159 
164  void remove( size_t first_index, size_t last_index )
165  {
166  if( first_index >= sequences_.size()
167  || last_index >= sequences_.size()
168  || first_index >= last_index
169  ) {
170  throw std::out_of_range( "Invalid indices for removing from SequenceSet." );
171  }
172 
173  sequences_.erase( sequences_.begin() + first_index, sequences_.begin() + last_index );
174  }
175 
179  void remove( iterator position )
180  {
181  sequences_.erase( position );
182  }
183 
188  void remove( iterator first, iterator last )
189  {
190  sequences_.erase( first, last );
191  }
192 
196  void clear()
197  {
198  sequences_.clear();
199  }
200 
201  // -------------------------------------------------------------------------
202  // Iterators
203  // -------------------------------------------------------------------------
204 
206  {
207  return sequences_.begin();
208  }
209 
211  {
212  return sequences_.end();
213  }
214 
216  {
217  return sequences_.cbegin();
218  }
219 
221  {
222  return sequences_.cend();
223  }
224 
226  {
227  return sequences_.cbegin();
228  }
229 
231  {
232  return sequences_.cend();
233  }
234 
235  // -------------------------------------------------------------------------
236  // Data Members
237  // -------------------------------------------------------------------------
238 
239 private:
240 
241  std::vector<Sequence> sequences_;
242 };
243 
244 } // namespace sequence
245 } // namespace genesis
246 
247 #endif // include guard
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::sequence::SequenceSet::cend
const_iterator cend() const
Definition: sequence_set.hpp:230
genesis::sequence::SequenceSet::remove
void remove(size_t first_index, size_t last_index)
Remove the Sequences between the first_index (inclusive) and the last_index (exclusive) from the Sequ...
Definition: sequence_set.hpp:164
genesis::sequence::SequenceSet::SequenceSet
SequenceSet()=default
genesis::sequence::Sequence
Definition: sequence/sequence.hpp:40
genesis::sequence::SequenceSet::~SequenceSet
~SequenceSet()=default
genesis::sequence::SequenceSet::clear
void clear()
Remove all Sequences from the SequenceSet, leaving it with a size() of 0.
Definition: sequence_set.hpp:196
genesis::sequence::SequenceSet::const_iterator
std::vector< Sequence >::const_iterator const_iterator
Definition: sequence_set.hpp:62
genesis::sequence::SequenceSet::end
iterator end()
Definition: sequence_set.hpp:210
genesis::sequence::SequenceSet::cbegin
const_iterator cbegin() const
Definition: sequence_set.hpp:225
genesis::sequence::SequenceSet::size
size_t size() const
Definition: sequence_set.hpp:93
genesis::sequence::SequenceSet::iterator
std::vector< Sequence >::iterator iterator
Definition: sequence_set.hpp:61
genesis::sequence::SequenceSet::add
reference add(Sequence &&s)
Add a Sequence to the SequenceSet by moving it, and return a reference to it.
Definition: sequence_set.hpp:142
genesis::sequence::SequenceSet::remove
void remove(size_t index)
Remove the Sequence at a given index from the SequenceSet.
Definition: sequence_set.hpp:151
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::sequence::SequenceSet
Store a set of Sequences.
Definition: sequence_set.hpp:53
genesis::sequence::SequenceSet::begin
const_iterator begin() const
Definition: sequence_set.hpp:215
genesis::sequence::SequenceSet::const_reference
Sequence const & const_reference
Definition: sequence_set.hpp:65
genesis::sequence::SequenceSet::add
reference add(Sequence const &s)
Add a Sequence to the SequenceSet by copying it, and return a reference to it.
Definition: sequence_set.hpp:133
genesis::sequence::SequenceSet::operator=
SequenceSet & operator=(SequenceSet const &)=default
genesis::sequence::SequenceSet::at
const_reference at(size_t index) const
Definition: sequence_set.hpp:111
genesis::sequence::SequenceSet::empty
bool empty() const
Definition: sequence_set.hpp:101
genesis::sequence::SequenceSet::remove
void remove(iterator position)
Remove the Sequence at a given iterator position from the SequenceSet.
Definition: sequence_set.hpp:179
genesis::sequence::SequenceSet::begin
iterator begin()
Definition: sequence_set.hpp:205
genesis::sequence::SequenceSet::at
reference at(size_t index)
Definition: sequence_set.hpp:106
genesis::sequence::SequenceSet::end
const_iterator end() const
Definition: sequence_set.hpp:220
genesis::sequence::SequenceSet::swap
friend void swap(SequenceSet &lhs, SequenceSet &rhs)
Definition: sequence_set.hpp:80
sequence.hpp
genesis::sequence::SequenceSet::remove
void remove(iterator first, iterator last)
Remove the Sequences between the first (inclusive) and the last (exclusive) iterator position from th...
Definition: sequence_set.hpp:188
genesis::sequence::SequenceSet::operator[]
reference operator[](size_t index)
Definition: sequence_set.hpp:116