A library for working with phylogenetic and population genetic data.
v0.32.0
char_lookup.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_TOOLS_CHAR_LOOKUP_H_
2 #define GENESIS_UTILS_TOOLS_CHAR_LOOKUP_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 #include <array>
36 #include <cassert>
37 #include <functional>
38 #include <string>
39 #include <stdexcept>
40 
41 namespace genesis {
42 namespace utils {
43 
44 // =================================================================================================
45 // Chars Lookup Table
46 // =================================================================================================
47 
53 template< typename T >
55 {
56 public:
57 
58  // -------------------------------------------------------------------------
59  // Typedefs and Enums
60  // -------------------------------------------------------------------------
61 
62  using value_type = T;
63  using key_type = char;
64 
65  static const size_t ArraySize = 128;
66 
67  // -------------------------------------------------------------------------
68  // Constructors and Rule of Five
69  // -------------------------------------------------------------------------
70 
75  : CharLookup( T{} )
76  {}
77 
81  explicit CharLookup( T const& init_all )
82  {
83  for( size_t i = 0; i < ArraySize; ++i ) {
84  table_[i] = init_all;
85  }
86  }
87 
94  explicit CharLookup( std::initializer_list<T> init )
95  {
96  if( init.size() != ArraySize ) {
97  throw std::domain_error(
98  "Expect initializer list of size " + std::to_string(ArraySize) +
99  " instead of size " + std::to_string( init.size() ) + "."
100  );
101  }
102 
103  size_t i = 0;
104  for( auto const& e : init ) {
105  table_[i] = e;
106  ++i;
107  }
108  assert( i == ArraySize );
109  }
110 
111  ~CharLookup() = default;
112 
113  CharLookup(CharLookup const&) = default;
114  CharLookup(CharLookup&&) = default;
115 
116  CharLookup& operator= (CharLookup const&) = default;
117  CharLookup& operator= (CharLookup&&) = default;
118 
119  // -------------------------------------------------------------------------
120  // Setters
121  // -------------------------------------------------------------------------
122 
126  void set_char( char c, T value )
127  {
128  assert( 0 <= c );
129  table_[ static_cast<unsigned char>(c) ] = value;
130  }
131 
135  void set_char_upper_lower( char c, T value )
136  {
137  assert( 0 <= c );
138  table_[ static_cast<unsigned char>( toupper(c) )] = value;
139  table_[ static_cast<unsigned char>( tolower(c) )] = value;
140  }
141 
145  void set_if( std::function<bool ( char )> predicate, T value )
146  {
147  for( unsigned char c = 0; c < ArraySize; ++c ) {
148  if( predicate(c) ) {
149  table_[ c ] = value;
150  }
151  }
152  }
153 
157  void set_selection( std::string const& chars, T value )
158  {
159  for( char c : chars ) {
160  assert( 0 <= c );
161  table_[ static_cast<unsigned char>(c) ] = value;
162  }
163  }
164 
169  void set_selection_upper_lower( std::string const& chars, T value )
170  {
171  for( char c : chars ) {
172  assert( 0 <= c );
173  table_[ static_cast<unsigned char>( toupper(c) )] = value;
174  table_[ static_cast<unsigned char>( tolower(c) )] = value;
175  }
176  }
177 
181  void set_range( char first, char last, T value )
182  {
183  for( auto c = first; c <= last; ++c ) {
184  assert( 0 <= c );
185  table_[ static_cast<unsigned char>(c) ] = value;
186  }
187  }
188 
192  void set_all( T value )
193  {
194  for( unsigned char c = 0; c < 128; ++c ) {
195  table_[ c ] = value;
196  }
197  }
198 
199  // -------------------------------------------------------------------------
200  // Getters
201  // -------------------------------------------------------------------------
202 
209  T operator [] ( char c ) const
210  {
211  assert( 0 <= c );
212  return table_[ static_cast<unsigned char>(c) ];
213  }
214 
218  T get( char c ) const
219  {
220  assert( 0 <= c );
221  return table_[ static_cast<unsigned char>(c) ];
222  }
223 
228  std::string get_chars_equal_to( T comp_value ) const
229  {
230  std::string ret;
231  for( unsigned char c = 0; c < 128; ++c ) {
232  if( get(c) == comp_value ) {
233  ret += c;
234  }
235  }
236  return ret;
237  }
238 
242  bool all_equal_to( T comp_value ) const
243  {
244  for( unsigned char c = 0; c < 128; ++c ) {
245  if( get(c) != comp_value ) {
246  return false;
247  }
248  }
249  return true;
250  }
251 
252  // -------------------------------------------------------------------------
253  // Data Members
254  // -------------------------------------------------------------------------
255 
256 private:
257 
258  std::array< T, ArraySize > table_;
259 };
260 
261 } // namespace utils
262 } // namespace genesis
263 
264 #endif // include guard
genesis::utils::CharLookup::set_if
void set_if(std::function< bool(char)> predicate, T value)
Set the lookup status for all chars that fulfill a given predicate.
Definition: char_lookup.hpp:145
genesis::utils::CharLookup::all_equal_to
bool all_equal_to(T comp_value) const
Return whether all chars compare equal to a given value.
Definition: char_lookup.hpp:242
genesis::utils::CharLookup::CharLookup
CharLookup(std::initializer_list< T > init)
Constructor that takes an initializer list of the template type.
Definition: char_lookup.hpp:94
genesis::utils::CharLookup::get
T get(char c) const
Return the lookup status for a given char.
Definition: char_lookup.hpp:218
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: function/genome_locus.hpp:52
genesis::utils::CharLookup< bool >::value_type
bool value_type
Definition: char_lookup.hpp:62
genesis::utils::CharLookup::ArraySize
static const size_t ArraySize
Definition: char_lookup.hpp:65
genesis::utils::CharLookup::set_selection_upper_lower
void set_selection_upper_lower(std::string const &chars, T value)
Set the lookup status for both the upper and lower case of all chars that are contained in a given st...
Definition: char_lookup.hpp:169
genesis::utils::CharLookup
Simple lookup table providing a value lookup for each ASCII char (0-127).
Definition: char_lookup.hpp:54
genesis::utils::CharLookup::get_chars_equal_to
std::string get_chars_equal_to(T comp_value) const
Return a std::string containg all chars which have lookup status equal to a given value.
Definition: char_lookup.hpp:228
genesis::utils::CharLookup::set_char
void set_char(char c, T value)
Set the lookup status for a given char.
Definition: char_lookup.hpp:126
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::CharLookup::operator=
CharLookup & operator=(CharLookup const &)=default
genesis::utils::CharLookup< bool >::key_type
char key_type
Definition: char_lookup.hpp:63
genesis::utils::CharLookup::set_range
void set_range(char first, char last, T value)
Set the lookup status for all chars in an inlcuding range between two chars.
Definition: char_lookup.hpp:181
genesis::utils::CharLookup::CharLookup
CharLookup(T const &init_all)
Constructor that sets all values to a given one.
Definition: char_lookup.hpp:81
genesis::utils::CharLookup::CharLookup
CharLookup()
Constructor that sets all values to the default value of the template parameter type.
Definition: char_lookup.hpp:74
genesis::utils::CharLookup::set_char_upper_lower
void set_char_upper_lower(char c, T value)
Set the lookup status for both the upper and lower case of a given char.
Definition: char_lookup.hpp:135
genesis::utils::CharLookup::set_all
void set_all(T value)
Set the lookup status for all chars at once.
Definition: char_lookup.hpp:192
genesis::utils::CharLookup::operator[]
T operator[](char c) const
Return the lookup status for a given char.
Definition: char_lookup.hpp:209
genesis::utils::CharLookup::set_selection
void set_selection(std::string const &chars, T value)
Set the lookup status for all chars that are contained in a given std::string.
Definition: char_lookup.hpp:157
genesis::utils::CharLookup::~CharLookup
~CharLookup()=default