A library for working with phylogenetic and population genetic data.
v0.27.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
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::placement::SampleSet::operator=
SampleSet & operator=(SampleSet const &)=default
genesis::placement::SampleSet::add
void add(Sample &&smp, std::string const &name="")
Add a Sample with a name to the SampleSet.
Definition: sample_set.hpp:105
genesis::placement::SampleSet
Store a set of Samples with associated names.
Definition: sample_set.hpp:54
genesis::placement::SampleSet::add
void add(Sample const &smp, std::string const &name="")
Add a Sample with a name to the SampleSet.
Definition: sample_set.hpp:94
genesis::placement::Sample
Manage a set of Pqueries along with the PlacementTree where the PqueryPlacements are placed on.
Definition: sample.hpp:68
genesis::placement::SampleSet::iterator
typename std::vector< Sample >::iterator iterator
Definition: sample_set.hpp:62
genesis::placement::SampleSet::at
Sample const & at(size_t index) const
Definition: sample_set.hpp:189
genesis::placement::SampleSet::empty
bool empty() const
Return whether the SampleSet is empty.
Definition: sample_set.hpp:225
genesis::placement::SampleSet::operator[]
Sample & operator[](size_t index)
Definition: sample_set.hpp:194
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::placement::SampleSet::samples
std::vector< Sample > const & samples() const
Definition: sample_set.hpp:204
genesis::placement::SampleSet::begin
iterator begin()
Definition: sample_set.hpp:164
genesis::placement::SampleSet::names
std::vector< std::string > const & names() const
Definition: sample_set.hpp:155
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::placement::SampleSet::clear
void clear()
Delete all Samples in this SampleSet.
Definition: sample_set.hpp:134
genesis::placement::SampleSet::remove_at
void remove_at(size_t index)
Remove the Sample at a certain index position.
Definition: sample_set.hpp:117
genesis::placement::SampleSet::~SampleSet
~SampleSet()=default
genesis::placement::SampleSet::end
const_iterator end() const
Definition: sample_set.hpp:179
genesis::placement::SampleSet::begin
const_iterator begin() const
Definition: sample_set.hpp:169
genesis::placement::SampleSet::size
size_t size() const
Return the size of the SampleSet, i.e., the number of Samples.
Definition: sample_set.hpp:234
genesis::placement::SampleSet::SampleSet
SampleSet()=default
genesis::placement::SampleSet::at
Sample & at(size_t index)
Definition: sample_set.hpp:184
genesis::placement::SampleSet::name_at
std::string const & name_at(size_t index) const
Definition: sample_set.hpp:144
genesis::placement::SampleSet::const_iterator
typename std::vector< Sample >::const_iterator const_iterator
Definition: sample_set.hpp:63
sample.hpp
genesis::placement::SampleSet::swap
void swap(SampleSet &other)
Definition: sample_set.hpp:78
genesis::placement::SampleSet::end
iterator end()
Definition: sample_set.hpp:174