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;
87 
89 
109  constexpr char_type to_char() const noexcept
111  requires (!std::same_as<char_t, void>)
113  {
114  return derived_type::rank_to_char[rank];
115  }
116 
133  constexpr rank_type to_rank() const noexcept
134  {
135  return rank;
136  }
138 
159  constexpr derived_type & assign_char(char_type const c) noexcept
161  requires (!std::same_as<char_t, void>)
163  {
164  using index_t = std::make_unsigned_t<char_type>;
165  rank = derived_type::char_to_rank[static_cast<index_t>(c)];
166  return static_cast<derived_type &>(*this);
167  }
168 
186  constexpr derived_type & assign_rank(rank_type const c) noexcept
187  {
188  assert(static_cast<size_t>(c) < static_cast<size_t>(alphabet_size));
189  rank = c;
190  return static_cast<derived_type &>(*this);
191  }
193 
199 
202 
207  friend constexpr bool operator==(derived_type const lhs, derived_type const rhs) noexcept
208  {
209  return seqan3::to_rank(lhs) == seqan3::to_rank(rhs);
210  }
211 
216  friend constexpr bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
217  {
218  return seqan3::to_rank(lhs) != seqan3::to_rank(rhs);
219  }
220 
225  friend constexpr bool operator<(derived_type const lhs, derived_type const rhs) noexcept
226  {
227  return seqan3::to_rank(lhs) < seqan3::to_rank(rhs);
228  }
229 
234  friend constexpr bool operator>(derived_type const lhs, derived_type const rhs) noexcept
235  {
236  return seqan3::to_rank(lhs) > seqan3::to_rank(rhs);
237  }
238 
243  friend constexpr bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
244  {
245  return seqan3::to_rank(lhs) <= seqan3::to_rank(rhs);
246  }
247 
252  friend constexpr bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
253  {
254  return seqan3::to_rank(lhs) >= seqan3::to_rank(rhs);
255  }
257 
258 private:
261 };
262 
263 } // 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:55
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:109
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:207
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:234
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:133
constexpr derived_type & assign_char(char_type const c) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:159
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:198
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:225
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:186
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:216
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:252
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:243
rank_type rank
The value of the alphabet letter is stored as the rank.
Definition: alphabet_base.hpp:260
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.