SeqAn3  3.1.0-rc.1
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 namespace seqan3
23 {
24 
55 template <typename derived_type, size_t size, typename char_t = char>
57 {
58 protected:
59  static_assert(size != 0, "alphabet size must be >= 1"); // == 1 is handled below in separate specialisation
60 
73 
82 
83 public:
87  constexpr alphabet_base() noexcept = default;
88  constexpr alphabet_base(alphabet_base const &) noexcept = default;
89  constexpr alphabet_base(alphabet_base &&) noexcept = default;
90  constexpr alphabet_base & operator=(alphabet_base const &) noexcept = default;
91  constexpr alphabet_base & operator=(alphabet_base &&) noexcept = default;
92  ~alphabet_base() noexcept = default;
93 
95 
115  constexpr char_type to_char() const noexcept
117  requires (!std::same_as<char_t, void>)
119  {
120  return derived_type::rank_to_char(rank);
121  }
122 
139  constexpr rank_type to_rank() const noexcept
140  {
141  return rank;
142  }
144 
165  constexpr derived_type & assign_char(char_type const chr) noexcept
167  requires (!std::same_as<char_t, void>)
169  {
170  rank = derived_type::char_to_rank(chr);
171  return static_cast<derived_type &>(*this);
172  }
173 
191  constexpr derived_type & assign_rank(rank_type const c) noexcept
192  {
193  assert(static_cast<size_t>(c) < static_cast<size_t>(alphabet_size));
194  rank = c;
195  return static_cast<derived_type &>(*this);
196  }
198 
204 
207 
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  }
216 
221  friend constexpr bool operator!=(derived_type const lhs, derived_type const rhs) noexcept
222  {
223  return seqan3::to_rank(lhs) != seqan3::to_rank(rhs);
224  }
225 
230  friend constexpr bool operator<(derived_type const lhs, derived_type const rhs) noexcept
231  {
232  return seqan3::to_rank(lhs) < seqan3::to_rank(rhs);
233  }
234 
239  friend constexpr bool operator>(derived_type const lhs, derived_type const rhs) noexcept
240  {
241  return seqan3::to_rank(lhs) > seqan3::to_rank(rhs);
242  }
243 
248  friend constexpr bool operator<=(derived_type const lhs, derived_type const rhs) noexcept
249  {
250  return seqan3::to_rank(lhs) <= seqan3::to_rank(rhs);
251  }
252 
257  friend constexpr bool operator>=(derived_type const lhs, derived_type const rhs) noexcept
258  {
259  return seqan3::to_rank(lhs) >= seqan3::to_rank(rhs);
260  }
262 
263 private:
266 };
267 
268 } // 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:57
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:115
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:212
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:239
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:139
constexpr derived_type & assign_char(char_type const chr) noexcept
Assign from a character, implicitly converts invalid characters.
Definition: alphabet_base.hpp:165
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:203
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:230
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:191
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:221
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:257
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:248
rank_type rank
The value of the alphabet letter is stored as the rank.
Definition: alphabet_base.hpp:265
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.
Provides C++20 additions to the type_traits header.