A toolkit for working with phylogenetic data.
v0.24.0
sample_set.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_PLACEMENT_SAMPLE_SET_H_
2 #define GENESIS_PLACEMENT_SAMPLE_SET_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 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 
35 
36 #include <cassert>
37 #include <stdexcept>
38 #include <string>
39 #include <vector>
40 
41 namespace genesis {
42 namespace placement {
43 
44 // =================================================================================================
45 // SampleSet
46 // =================================================================================================
47 
54 class SampleSet
55 {
56 public:
57 
58  // -------------------------------------------------------------------------
59  // Typedefs
60  // -------------------------------------------------------------------------
61 
62  using iterator = typename std::vector<Sample>::iterator;
63  using const_iterator = typename std::vector<Sample>::const_iterator;
64 
65  // -------------------------------------------------------------------------
66  // Constructors and Rule of Five
67  // -------------------------------------------------------------------------
68 
69  SampleSet() = default;
70  ~SampleSet() = default;
71 
72  SampleSet( SampleSet const& ) = default;
73  SampleSet( SampleSet&& ) = default;
74 
75  SampleSet& operator= ( SampleSet const& ) = default;
76  SampleSet& operator= ( SampleSet&& ) = default;
77 
78  void swap( SampleSet& other )
79  {
80  using std::swap;
81  swap( names_, other.names_ );
82  swap( smpls_, other.smpls_ );
83  }
84 
85  // -------------------------------------------------------------------------
86  // Modifiers
87  // -------------------------------------------------------------------------
88 
94  void add( Sample const& smp, std::string const& name ="" )
95  {
96  names_.push_back( name );
97  smpls_.push_back( smp );
98  }
99 
105  void add( Sample&& smp, std::string const& name = "" )
106  {
107  names_.push_back( name );
108  smpls_.push_back( std::move(smp) );
109  }
110 
117  void remove_at( size_t index )
118  {
119  if( index >= smpls_.size() ) {
120  throw std::invalid_argument(
121  "Cannot remove element at index " + std::to_string( index ) + " from SampleSet with " +
122  std::to_string( smpls_.size() ) + " samples."
123  );
124  }
125  assert( names_.size() == smpls_.size() );
126 
127  names_.erase( names_.begin() + index );
128  smpls_.erase( smpls_.begin() + index );
129  }
130 
134  void clear()
135  {
136  names_.clear();
137  smpls_.clear();
138  }
139 
140  // -------------------------------------------------------------------------
141  // Name Accessors
142  // -------------------------------------------------------------------------
143 
144  std::string const& name_at( size_t index ) const
145  {
146  if( index >= names_.size() ) {
147  throw std::invalid_argument(
148  "Cannot access element at index " + std::to_string( index ) + " from SampleSet with " +
149  std::to_string( names_.size() ) + " samples."
150  );
151  }
152  return names_[index];
153  }
154 
155  std::vector<std::string> const& names() const
156  {
157  return names_;
158  }
159 
160  // -------------------------------------------------------------------------
161  // Accessors
162  // -------------------------------------------------------------------------
163 
165  {
166  return smpls_.begin();
167  }
168 
170  {
171  return smpls_.cbegin();
172  }
173 
175  {
176  return smpls_.end();
177  }
178 
180  {
181  return smpls_.cend();
182  }
183 
184  Sample& at ( size_t index )
185  {
186  return smpls_.at(index);
187  }
188 
189  Sample const& at ( size_t index ) const
190  {
191  return smpls_.at(index);
192  }
193 
194  Sample& operator [] ( size_t index )
195  {
196  return smpls_[index];
197  }
198 
199  Sample const& operator [] ( size_t index ) const
200  {
201  return smpls_[index];
202  }
203 
204  std::vector<Sample> const& samples() const
205  {
206  return smpls_;
207  }
208 
213  operator std::vector<Sample> const&() const
214  {
215  return smpls_;
216  }
217 
218  // -------------------------------------------------------------------------
219  // General Properties
220  // -------------------------------------------------------------------------
221 
225  bool empty() const
226  {
227  assert( names_.empty() == smpls_.empty() );
228  return smpls_.empty();
229  }
230 
234  size_t size() const
235  {
236  assert( names_.size() == smpls_.size() );
237  return smpls_.size();
238  }
239 
240  // -------------------------------------------------------------------------
241  // Data Members
242  // -------------------------------------------------------------------------
243 
244 private:
245 
246  std::vector<std::string> names_;
247  std::vector<Sample> smpls_;
248 
249 };
250 
251 } // namespace placement
252 } // namespace genesis
253 
254 #endif // include guard
size_t size() const
Return the size of the SampleSet, i.e., the number of Samples.
Definition: sample_set.hpp:234
SampleSet & operator=(SampleSet const &)=default
std::vector< Sample > const & samples() const
Definition: sample_set.hpp:204
bool empty() const
Return whether the SampleSet is empty.
Definition: sample_set.hpp:225
void swap(SequenceSet &lhs, SequenceSet &rhs)
Sample const & at(size_t index) const
Definition: sample_set.hpp:189
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Sample & at(size_t index)
Definition: sample_set.hpp:184
Sample & operator[](size_t index)
Definition: sample_set.hpp:194
const_iterator begin() const
Definition: sample_set.hpp:169
void clear()
Delete all Samples in this SampleSet.
Definition: sample_set.hpp:134
std::vector< std::string > const & names() const
Definition: sample_set.hpp:155
void add(Sample const &smp, std::string const &name="")
Add a Sample with a name to the SampleSet.
Definition: sample_set.hpp:94
typename std::vector< Sample >::const_iterator const_iterator
Definition: sample_set.hpp:63
Store a set of Samples with associated names.
Definition: sample_set.hpp:54
std::string const & name_at(size_t index) const
Definition: sample_set.hpp:144
void add(Sample &&smp, std::string const &name="")
Add a Sample with a name to the SampleSet.
Definition: sample_set.hpp:105
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on...
Definition: sample.hpp:68
const_iterator end() const
Definition: sample_set.hpp:179
std::shared_ptr< BaseOutputTarget > to_string(std::string &target_string)
Obtain an output target for writing to a string.
typename std::vector< Sample >::iterator iterator
Definition: sample_set.hpp:62
void swap(SampleSet &other)
Definition: sample_set.hpp:78
void remove_at(size_t index)
Remove the Sample at a certain index position.
Definition: sample_set.hpp:117