SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
alphabet_base.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 #include <seqan3/std/concepts>
17 #include <seqan3/std/type_traits>
18 
21 
22 #ifdef SEQAN3_DEPRECATED_310
23 namespace seqan3::detail
24 {
26 // helper concept to deprecate old rank_to_char lookup tables
27 template <typename alphabet_t>
28 SEQAN3_CONCEPT has_rank_to_char_table = requires()
29 {
30  { alphabet_t::rank_to_char[0] };
31 };
33 
35 // helper concept to deprecate old char_to_rank lookup tables
36 template <typename alphabet_t>
37 SEQAN3_CONCEPT has_char_to_rank_table = requires()
38 {
39  { alphabet_t::char_to_rank[0] };
40 };
42 
43 } // namespace seqan3::detail
44 #endif // SEQAN3_DEPRECATED_310
45 
46 namespace seqan3
47 {
48 
79 template <typename derived_type, size_t size, typename char_t = char>
81 {
82 protected:
83  static_assert(size != 0, "alphabet size must be >= 1"); // == 1 is handled below in separate specialisation
84 
97 
106 
107 public:
111  constexpr alphabet_base() noexcept = default;
112  constexpr alphabet_base(alphabet_base const &) noexcept = default;
113  constexpr alphabet_base(alphabet_base &&) noexcept = default;
114  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
115  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
116  ~alphabet_base() noexcept = default;
117 
119 
139  constexpr char_type to_char() const noexcept
141  requires (!std::same_as<char_t, void>)
143  {
144 #ifdef SEQAN3_DEPRECATED_310
145  if constexpr (detail::has_rank_to_char_table<derived_type>)
146  return rank_to_char_table(rank);
147  else
148  return derived_type::rank_to_char(rank);
149 #else // ^^^ before 3.1.0 release / after 3.1.0 release vvv
150  return derived_type::rank_to_char(rank);
151 #endif // SEQAN3_DEPRECATED_310
152  }
153 
154 #ifdef SEQAN3_DEPRECATED_310
155 private:
156 
161  SEQAN3_DEPRECATED_310 static constexpr char_type rank_to_char_table(rank_type const rank) noexcept
162  {
163  return derived_type::rank_to_char[rank];
164  }
165 
166 public:
167 #endif // SEQAN3_DEPRECATED_310
168 
185  constexpr rank_type to_rank() const noexcept
186  {
187  return rank;
188  }
190 
211  constexpr derived_type & assign_char(char_type const chr) noexcept
213  requires (!std::same_as<char_t, void>)
215  {
216 #ifdef SEQAN3_DEPRECATED_310
217  if constexpr (detail::has_char_to_rank_table<derived_type>)
218  rank = char_to_rank_table(chr);
219  else
220  rank = derived_type::char_to_rank(chr);
221 #else // ^^^ before 3.1.0 release / after 3.1.0 release vvv
222  rank = derived_type::char_to_rank(chr);
223 #endif // SEQAN3_DEPRECATED_310
224 
225  return static_cast<derived_type &>(*this);
226  }
227 
228 #ifdef SEQAN3_DEPRECATED_310
229 private:
230 
235  SEQAN3_DEPRECATED_310 static constexpr rank_type char_to_rank_table(char_type const chr) noexcept
236  {
237  using index_t = std::make_unsigned_t<char_type>;
238 #if SEQAN3_WORKAROUND_GCC_99318
239 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
240 #endif // SEQAN3_WORKAROUND_GCC_99318
241  return derived_type::char_to_rank[static_cast<index_t>(chr)];
242  }
243 
244 public:
245 #endif // SEQAN3_DEPRECATED_310
246 
264  constexpr derived_type & assign_rank(rank_type const c) noexcept
265  {
266  assert(static_cast<size_t>(c) < static_cast<size_t>(alphabet_size));
267  rank = c;
268  return static_cast<derived_type &>(*this);
269  }
271 
277 
280 
285  friend constexpr bool operator==(derived_type const lhs, derived_type const rhs) noexcept
286  {
287  return seqan3::to_rank(lhs) == seqan3::to_rank(rhs);
288  }
289 
294  friend constexpr bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
295  {
296  return seqan3::to_rank(lhs) != seqan3::to_rank(rhs);
297  }
298 
303  friend constexpr bool operator<(derived_type const lhs, derived_type const rhs) noexcept
304  {
305  return seqan3::to_rank(lhs) < seqan3::to_rank(rhs);
306  }
307 
312  friend constexpr bool operator>(derived_type const lhs, derived_type const rhs) noexcept
313  {
314  return seqan3::to_rank(lhs) > seqan3::to_rank(rhs);
315  }
316 
321  friend constexpr bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
322  {
323  return seqan3::to_rank(lhs) <= seqan3::to_rank(rhs);
324  }
325 
330  friend constexpr bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
331  {
332  return seqan3::to_rank(lhs) >= seqan3::to_rank(rhs);
333  }
335 
336 private:
338  rank_type rank{};
339 };
340 
341 } // namespace seqan3
Core alphabet concept and free function/type trait wrappers.
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:81
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:139
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:285
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:312
constexpr alphabet_base() noexcept=default
Defaulted.
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:185
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:104
constexpr derived_type & assign_char(char_type const chr) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:211
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:276
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:303
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:264
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:294
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:330
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:321
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:96
The Concepts library.
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:155
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
Provides metaprogramming utilities for integer types.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
#define SEQAN3_DEPRECATED_310
Deprecation message for SeqAn 3.1.0 release.
Definition: platform.hpp:203
Provides C++20 additions to the type_traits header.