SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
alphabet_base.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
17 #include <seqan3/std/concepts>
18 #include <seqan3/std/type_traits>
19 
20 namespace seqan3
21 {
22 
50 template <typename derived_type, size_t size, typename char_t = char>
52 {
53 protected:
54  static_assert(size != 0, "alphabet size must be >= 1"); // == 1 is handled below in separate specialisation
55 
64 
65 public:
69  constexpr alphabet_base() noexcept = default;
70  constexpr alphabet_base(alphabet_base const &) noexcept = default;
71  constexpr alphabet_base(alphabet_base &&) noexcept = default;
72  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
73  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
74  ~alphabet_base() noexcept = default;
75 
94  constexpr char_type to_char() const noexcept
96  requires !std::same_as<char_t, void>
98  {
99  return derived_type::rank_to_char[rank];
100  }
101 
116  constexpr rank_type to_rank() const noexcept
117  {
118  return rank;
119  }
121 
140  constexpr derived_type & assign_char(char_type const c) noexcept
144  {
145  using index_t = std::make_unsigned_t<char_type>;
146  rank = derived_type::char_to_rank[static_cast<index_t>(c)];
147  return static_cast<derived_type &>(*this);
148  }
149 
165  constexpr derived_type & assign_rank(rank_type const c) noexcept
166  {
167  assert(static_cast<size_t>(c) < static_cast<size_t>(alphabet_size));
168  rank = c;
169  return static_cast<derived_type &>(*this);
170  }
172 
175 
178 
180  friend constexpr bool operator==(derived_type const lhs, derived_type const rhs) noexcept
181  {
182  return seqan3::to_rank(lhs) == seqan3::to_rank(rhs);
183  }
184 
186  friend constexpr bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
187  {
188  return seqan3::to_rank(lhs) != seqan3::to_rank(rhs);
189  }
190 
192  friend constexpr bool operator<(derived_type const lhs, derived_type const rhs) noexcept
193  {
194  return seqan3::to_rank(lhs) < seqan3::to_rank(rhs);
195  }
196 
198  friend constexpr bool operator>(derived_type const lhs, derived_type const rhs) noexcept
199  {
200  return seqan3::to_rank(lhs) > seqan3::to_rank(rhs);
201  }
202 
204  friend constexpr bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
205  {
206  return seqan3::to_rank(lhs) <= seqan3::to_rank(rhs);
207  }
208 
210  friend constexpr bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
211  {
212  return seqan3::to_rank(lhs) >= seqan3::to_rank(rhs);
213  }
215 
216 private:
218  rank_type rank{};
219 };
220 
232 template <typename derived_type, typename char_t>
233 class alphabet_base<derived_type, 1ul, char_t>
234 {
235 protected:
242  using rank_type = bool;
244 
245 public:
249  constexpr alphabet_base() noexcept = default;
250  constexpr alphabet_base(alphabet_base const &) noexcept = default;
251  constexpr alphabet_base(alphabet_base &&) noexcept = default;
252  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
253  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
254  ~alphabet_base() noexcept = default;
255 
260  constexpr char_type to_char() const noexcept
263  requires !std::same_as<char_t, void>
265  {
266  return derived_type::char_value;
267  }
268 
270  constexpr rank_type to_rank() const noexcept
271  {
272  return 0;
273  }
275 
279  constexpr derived_type & assign_char(char_type const) noexcept
284  {
285  return static_cast<derived_type &>(*this);
286  }
287 
289  constexpr derived_type & assign_rank(rank_type const) noexcept
290  {
291  return static_cast<derived_type &>(*this);
292  }
294 
296  static constexpr bool alphabet_size = 1;
297 
300 
302  friend constexpr bool operator==(derived_type const, derived_type const) noexcept
303  {
304  return true;
305  }
306 
308  friend constexpr bool operator!=(derived_type const, derived_type const) noexcept
309  {
310  return false;
311  }
312 
314  friend constexpr bool operator<(derived_type const, derived_type const) noexcept
315  {
316  return false;
317  }
318 
320  friend constexpr bool operator>(derived_type const, derived_type const) noexcept
321  {
322  return false;
323  }
324 
326  friend constexpr bool operator<=(derived_type const, derived_type const) noexcept
327  {
328  return true;
329  }
330 
332  friend constexpr bool operator>=(derived_type const, derived_type const) noexcept
333  {
334  return true;
335  }
337 
338 private:
339 #if SEQAN3_WORKAROUND_GCC_87113
340  bool _bug_workaround{};
341 #endif
342 };
343 
344 } // namespace seqan3
seqan3::alphabet_base::operator<
constexpr friend bool operator<(derived_type const lhs, derived_type const rhs) noexcept
Checks whether the letter lhs is smaller than rhs.
Definition: alphabet_base.hpp:192
type_traits
Provides C++20 additions to the type_traits header.
seqan3::alphabet_base::operator<=
constexpr friend bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
Checks whether the letter lhs is smaller than or equal to rhs.
Definition: alphabet_base.hpp:204
seqan3::alphabet_base< derived_type, 1ul, char_t >::assign_rank
constexpr derived_type & assign_rank(rank_type const) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:289
seqan3::alphabet_base::alphabet_size
static constexpr detail::min_viable_uint_t< size > alphabet_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:174
seqan3::alphabet_base::operator>
constexpr friend bool operator>(derived_type const lhs, derived_type const rhs) noexcept
Checks whether the letter lhs is greater than rhs.
Definition: alphabet_base.hpp:198
seqan3::alphabet_base::to_char
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:94
seqan3::alphabet_base
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:51
seqan3::to_rank
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:142
seqan3::alphabet_base< derived_type, 1ul, char_t >::operator<=
constexpr friend bool operator<=(derived_type const, derived_type const) noexcept
Letters are always equal.
Definition: alphabet_base.hpp:326
seqan3::alphabet_base< derived_type, 1ul, char_t >::operator!=
constexpr friend bool operator!=(derived_type const, derived_type const) noexcept
Letters are never unequal.
Definition: alphabet_base.hpp:308
seqan3::alphabet_base< derived_type, 1ul, char_t >::operator<
constexpr friend bool operator<(derived_type const, derived_type const) noexcept
One letter cannot be smaller than another.
Definition: alphabet_base.hpp:314
concepts
The Concepts library.
seqan3::alphabet_base::assign_char
constexpr derived_type & assign_char(char_type const c) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:140
same_as
The concept std::same_as<T, U> is satisfied if and only if T and U denote the same type.
seqan3::alphabet_base::operator==
constexpr friend bool operator==(derived_type const lhs, derived_type const rhs) noexcept
Checks whether the letters lhs and rhs are equal.
Definition: alphabet_base.hpp:180
seqan3::alphabet_base< derived_type, 1ul, char_t >::operator>
constexpr friend bool operator>(derived_type const, derived_type const) noexcept
One letter cannot be bigger than another.
Definition: alphabet_base.hpp:320
seqan3::alphabet_base< derived_type, 1ul, char_t >::to_rank
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:270
seqan3::alphabet_base::operator!=
constexpr friend bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
Checks whether the letters lhs and rhs are unequal.
Definition: alphabet_base.hpp:186
seqan3::alphabet_base::rank_type
detail::min_viable_uint_t< size - 1 > rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:62
seqan3::alphabet_base::alphabet_base
constexpr alphabet_base() noexcept=default
Defaulted.
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:36
seqan3::alphabet_base< derived_type, 1ul, char_t >::operator>=
constexpr friend bool operator>=(derived_type const, derived_type const) noexcept
Letters are always equal.
Definition: alphabet_base.hpp:332
seqan3::alphabet_base< derived_type, 1ul, char_t >::operator==
constexpr friend bool operator==(derived_type const, derived_type const) noexcept
Letters are always equal.
Definition: alphabet_base.hpp:302
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
seqan3::alphabet_base::char_type
std::conditional_t< std::same_as< char_t, void >, char, char_t > char_type
The char representation; conditional needed to make semi alphabet definitions legal.
Definition: alphabet_base.hpp:60
int_types.hpp
Provides metaprogramming utilities for integer types.
std
SeqAn specific customisations in the standard namespace.
seqan3::alphabet_base< derived_type, 1ul, char_t >::rank_type
bool rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:242
seqan3::alphabet_base::operator>=
constexpr friend bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
Checks whether the letter lhs is bigger than or equal to rhs.
Definition: alphabet_base.hpp:210
seqan3::alphabet_base::assign_rank
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:165
std::conditional_t
seqan3::alphabet_base::to_rank
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:116
concept.hpp
Core alphabet concept and free function/type trait wrappers.
std::make_unsigned_t