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-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 #include <seqan3/std/concepts>
17 #include <seqan3/std/type_traits>
18 
21 
22 namespace seqan3
23 {
24 
53 template <typename derived_type, size_t size, typename char_t = char>
55 {
56 protected:
57  static_assert(size != 0, "alphabet size must be >= 1"); // == 1 is handled below in separate specialisation
58 
69 
76 
77 public:
81  constexpr alphabet_base() noexcept = default;
82  constexpr alphabet_base(alphabet_base const &) noexcept = default;
83  constexpr alphabet_base(alphabet_base &&) noexcept = default;
84  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
85  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
86  ~alphabet_base() noexcept = default;
88 
108  constexpr char_type to_char() const noexcept
110  requires (!std::same_as<char_t, void>)
112  {
113  return derived_type::rank_to_char[rank];
114  }
115 
132  constexpr rank_type to_rank() const noexcept
133  {
134  return rank;
135  }
137 
158  constexpr derived_type & assign_char(char_type const c) noexcept
160  requires (!std::same_as<char_t, void>)
162  {
163  using index_t = std::make_unsigned_t<char_type>;
164  rank = derived_type::char_to_rank[static_cast<index_t>(c)];
165  return static_cast<derived_type &>(*this);
166  }
167 
185  constexpr derived_type & assign_rank(rank_type const c) noexcept
186  {
187  assert(static_cast<size_t>(c) < static_cast<size_t>(alphabet_size));
188  rank = c;
189  return static_cast<derived_type &>(*this);
190  }
192 
198 
201 
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 
215  friend constexpr bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
216  {
217  return seqan3::to_rank(lhs) != seqan3::to_rank(rhs);
218  }
219 
224  friend constexpr bool operator<(derived_type const lhs, derived_type const rhs) noexcept
225  {
226  return seqan3::to_rank(lhs) < seqan3::to_rank(rhs);
227  }
228 
233  friend constexpr bool operator>(derived_type const lhs, derived_type const rhs) noexcept
234  {
235  return seqan3::to_rank(lhs) > seqan3::to_rank(rhs);
236  }
237 
242  friend constexpr bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
243  {
244  return seqan3::to_rank(lhs) <= seqan3::to_rank(rhs);
245  }
246 
251  friend constexpr bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
252  {
253  return seqan3::to_rank(lhs) >= seqan3::to_rank(rhs);
254  }
256 
257 private:
260 };
261 
273 template <typename derived_type, typename char_t>
274 class alphabet_base<derived_type, 1ul, char_t>
275 {
276 protected:
283  using rank_type = bool;
285 
286 public:
290  constexpr alphabet_base() noexcept = default;
291  constexpr alphabet_base(alphabet_base const &) noexcept = default;
292  constexpr alphabet_base(alphabet_base &&) noexcept = default;
293  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
294  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
295  ~alphabet_base() noexcept = default;
297 
302  constexpr char_type to_char() const noexcept
304  requires (!std::same_as<char_t, void>)
306  {
307  return derived_type::char_value;
308  }
309 
311  constexpr rank_type to_rank() const noexcept
312  {
313  return 0;
314  }
316 
321  constexpr derived_type & assign_char(char_type const) noexcept
323  requires (!std::same_as<char_t, void>)
325  {
326  return static_cast<derived_type &>(*this);
327  }
328 
330  constexpr derived_type & assign_rank(rank_type const) noexcept
331  {
332  return static_cast<derived_type &>(*this);
333  }
335 
337  static constexpr bool alphabet_size = 1;
338 
341 
343  friend constexpr bool operator==(derived_type const, derived_type const) noexcept
344  {
345  return true;
346  }
347 
349  friend constexpr bool operator!=(derived_type const, derived_type const) noexcept
350  {
351  return false;
352  }
353 
355  friend constexpr bool operator<(derived_type const, derived_type const) noexcept
356  {
357  return false;
358  }
359 
361  friend constexpr bool operator>(derived_type const, derived_type const) noexcept
362  {
363  return false;
364  }
365 
367  friend constexpr bool operator<=(derived_type const, derived_type const) noexcept
368  {
369  return true;
370  }
371 
373  friend constexpr bool operator>=(derived_type const, derived_type const) noexcept
374  {
375  return true;
376  }
378 
379 private:
380 #if SEQAN3_WORKAROUND_GCC_87113
381  bool _bug_workaround{};
382 #endif
383 };
384 
385 } // namespace seqan3
Core alphabet concept and free function/type trait wrappers.
constexpr friend bool operator<(derived_type const, derived_type const) noexcept
One letter cannot be smaller than another.
Definition: alphabet_base.hpp:355
constexpr rank_type to_rank() const noexcept
Return the letter's numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:311
constexpr friend bool operator!=(derived_type const, derived_type const) noexcept
Letters are never unequal.
Definition: alphabet_base.hpp:349
constexpr friend bool operator>=(derived_type const, derived_type const) noexcept
Letters are always equal.
Definition: alphabet_base.hpp:373
constexpr derived_type & assign_rank(rank_type const) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:330
bool rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:283
constexpr friend bool operator<=(derived_type const, derived_type const) noexcept
Letters are always equal.
Definition: alphabet_base.hpp:367
constexpr alphabet_base() noexcept=default
Defaulted.
constexpr friend bool operator>(derived_type const, derived_type const) noexcept
One letter cannot be bigger than another.
Definition: alphabet_base.hpp:361
constexpr derived_type & assign_char(char_type const) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:321
constexpr friend bool operator==(derived_type const, derived_type const) noexcept
Letters are always equal.
Definition: alphabet_base.hpp:343
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:55
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:108
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:206
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:233
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:132
constexpr derived_type & assign_char(char_type const c) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:158
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:197
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:224
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:185
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:215
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:251
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:242
rank_type rank
The value of the alphabet letter is stored as the rank.
Definition: alphabet_base.hpp:259
The Concepts library.
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:146
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:150
Provides metaprogramming utilities for integer types.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Provides C++20 additions to the type_traits header.