SeqAn3  3.0.2
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 
15 #include <cassert>
16 
19 #include <seqan3/std/concepts>
20 #include <seqan3/std/type_traits>
21 
22 namespace seqan3
23 {
24 
52 template <typename derived_type, size_t size, typename char_t = char>
54 {
55 protected:
56  static_assert(size != 0, "alphabet size must be >= 1"); // == 1 is handled below in separate specialisation
57 
66 
67 public:
71  constexpr alphabet_base() noexcept = default;
72  constexpr alphabet_base(alphabet_base const &) noexcept = default;
73  constexpr alphabet_base(alphabet_base &&) noexcept = default;
74  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
75  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
76  ~alphabet_base() noexcept = default;
77 
96  constexpr char_type to_char() const noexcept
98  requires (!std::same_as<char_t, void>)
100  {
101  return derived_type::rank_to_char[rank];
102  }
103 
118  constexpr rank_type to_rank() const noexcept
119  {
120  return rank;
121  }
123 
142  constexpr derived_type & assign_char(char_type const c) noexcept
144  requires (!std::same_as<char_t, void>)
146  {
147  using index_t = std::make_unsigned_t<char_type>;
148  rank = derived_type::char_to_rank[static_cast<index_t>(c)];
149  return static_cast<derived_type &>(*this);
150  }
151 
167  constexpr derived_type & assign_rank(rank_type const c) noexcept
168  {
169  assert(static_cast<size_t>(c) < static_cast<size_t>(alphabet_size));
170  rank = c;
171  return static_cast<derived_type &>(*this);
172  }
174 
177 
180 
182  friend constexpr bool operator==(derived_type const lhs, derived_type const rhs) noexcept
183  {
184  return seqan3::to_rank(lhs) == seqan3::to_rank(rhs);
185  }
186 
188  friend constexpr bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
189  {
190  return seqan3::to_rank(lhs) != seqan3::to_rank(rhs);
191  }
192 
194  friend constexpr bool operator<(derived_type const lhs, derived_type const rhs) noexcept
195  {
196  return seqan3::to_rank(lhs) < seqan3::to_rank(rhs);
197  }
198 
200  friend constexpr bool operator>(derived_type const lhs, derived_type const rhs) noexcept
201  {
202  return seqan3::to_rank(lhs) > seqan3::to_rank(rhs);
203  }
204 
206  friend constexpr bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
207  {
208  return seqan3::to_rank(lhs) <= seqan3::to_rank(rhs);
209  }
210 
212  friend constexpr bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
213  {
214  return seqan3::to_rank(lhs) >= seqan3::to_rank(rhs);
215  }
217 
218 private:
220  rank_type rank{};
221 };
222 
234 template <typename derived_type, typename char_t>
235 class alphabet_base<derived_type, 1ul, char_t>
236 {
237 protected:
244  using rank_type = bool;
246 
247 public:
251  constexpr alphabet_base() noexcept = default;
252  constexpr alphabet_base(alphabet_base const &) noexcept = default;
253  constexpr alphabet_base(alphabet_base &&) noexcept = default;
254  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
255  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
256  ~alphabet_base() noexcept = default;
257 
262  constexpr char_type to_char() const noexcept
265  requires (!std::same_as<char_t, void>)
267  {
268  return derived_type::char_value;
269  }
270 
272  constexpr rank_type to_rank() const noexcept
273  {
274  return 0;
275  }
277 
281  constexpr derived_type & assign_char(char_type const) noexcept
284  requires (!std::same_as<char_t, void>)
286  {
287  return static_cast<derived_type &>(*this);
288  }
289 
291  constexpr derived_type & assign_rank(rank_type const) noexcept
292  {
293  return static_cast<derived_type &>(*this);
294  }
296 
298  static constexpr bool alphabet_size = 1;
299 
302 
304  friend constexpr bool operator==(derived_type const, derived_type const) noexcept
305  {
306  return true;
307  }
308 
310  friend constexpr bool operator!=(derived_type const, derived_type const) noexcept
311  {
312  return false;
313  }
314 
316  friend constexpr bool operator<(derived_type const, derived_type const) noexcept
317  {
318  return false;
319  }
320 
322  friend constexpr bool operator>(derived_type const, derived_type const) noexcept
323  {
324  return false;
325  }
326 
328  friend constexpr bool operator<=(derived_type const, derived_type const) noexcept
329  {
330  return true;
331  }
332 
334  friend constexpr bool operator>=(derived_type const, derived_type const) noexcept
335  {
336  return true;
337  }
339 
340 private:
341 #if SEQAN3_WORKAROUND_GCC_87113
342  bool _bug_workaround{};
343 #endif
344 };
345 
346 } // 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:194
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:206
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:291
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:176
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:200
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:96
seqan3::alphabet_base
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:54
seqan3::to_rank
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:143
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:328
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:310
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:316
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:142
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:182
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:322
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:272
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:188
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:64
seqan3::alphabet_base::alphabet_base
constexpr alphabet_base() noexcept=default
Defaulted.
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
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:334
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:304
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:62
int_types.hpp
Provides metaprogramming utilities for integer types.
std
SeqAn specific customisations in the standard namespace.
cassert
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:244
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:212
seqan3::alphabet_base::assign_rank
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:167
std::conditional_t
seqan3::alphabet_base< derived_type, 1ul, char_t >::alphabet_base
constexpr alphabet_base() noexcept=default
Defaulted.
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:118
concept.hpp
Core alphabet concept and free function/type trait wrappers.
std::make_unsigned_t