A library for working with phylogenetic and population genetic data.
v0.32.0
utils/math/bitvector/helper.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2021 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lczech@carnegiescience.edu>
20  Department of Plant Biology, Carnegie Institution For Science
21  260 Panama Street, Stanford, CA 94305, USA
22 */
23 
32 
33 #include <algorithm>
34 #include <cassert>
35 #include <stdexcept>
36 #include <string>
37 
38 namespace genesis {
39 namespace utils {
40 
41 // =================================================================================================
42 // Bitvector Helper
43 // =================================================================================================
44 
45 std::vector<bool> make_bool_vector_from_indices( std::vector<size_t> const& indices, size_t size )
46 {
47  // Get the largest element of the vector. If it's empty, we return an all-false vector.
48  auto max_it = std::max_element( indices.begin(), indices.end() );
49  if( max_it == indices.end() ) {
50  return std::vector<bool>( size, false );
51  }
52  size_t target_size = *max_it + 1;
53  if( size > 0 ) {
54  if( target_size > size ) {
55  throw std::invalid_argument(
56  "Cannot use make_bool_vector_from_indices() with size " + std::to_string( size ) +
57  " that is smaller than required to include the larged index " +
58  std::to_string( *max_it ) + " in the list of indices (zero-based)."
59  );
60  }
61  target_size = size;
62  }
63 
64  // Fill a bool vector, setting all positions to true
65  // that are indicated by the indices, pun intended.
66  auto result = std::vector<bool>( target_size, false );
67  for( auto const& idx : indices ) {
68  assert( idx < result.size() );
69  result[idx] = true;
70  }
71  return result;
72 }
73 
74 } // namespace utils
75 } // namespace genesis
helper.hpp
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
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::utils::make_bool_vector_from_indices
std::vector< bool > make_bool_vector_from_indices(std::vector< size_t > const &indices, size_t size)
Helper function to create a bool vector from a set of indices to be set to true.
Definition: utils/math/bitvector/helper.cpp:45