BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
bio::alphabet::base< derived_type, size, char_t > Class Template Reference

A CRTP-base that makes defining a custom alphabet easier. More...

#include <bio/alphabet/base.hpp>

+ Inheritance diagram for bio::alphabet::base< derived_type, size, char_t >:

Public Member Functions

Constructors, destructor and assignment
constexpr base () noexcept=default
 Defaulted.
 
constexpr base (base const &) noexcept=default
 Defaulted.
 
constexpr base (base &&) noexcept=default
 Defaulted.
 
constexpr baseoperator= (base const &) noexcept=default
 Defaulted.
 
constexpr baseoperator= (base &&) noexcept=default
 Defaulted.
 
 ~base () noexcept=default
 
Read functions
constexpr char_type to_char () const noexcept
 Return the letter as a character of char_type.
 
constexpr rank_type to_rank () const noexcept
 Return the letter's numeric value (rank in the alphabet).
 
Write functions
constexpr derived_type & assign_char (char_type const c) noexcept
 Assign from a character, implicitly converts invalid characters.
 
constexpr derived_type & assign_rank (rank_type const c) noexcept
 Assign from a numeric value.
 

Static Public Attributes

static constexpr size_t alphabet_size = size
 The size of the alphabet, i.e. the number of different values it can take.
 

Protected Types

Member types
using char_type = std::conditional_t< std::same_as< char_t, void >, char, char_t >
 The char representation; conditional needed to make semi alphabet definitions legal.
 
using rank_type = meta::detail::min_viable_uint_t< size - 1 >
 The type of the alphabet when represented as a number (e.g. via to_rank()).
 

Friends

constexpr derived_type & tag_invoke (custom::assign_char_to, char_type const c, derived_type &a) noexcept
 tag_invoke() wrapper around member.
 
constexpr derived_type & tag_invoke (custom::assign_rank_to, rank_type const r, derived_type &a) noexcept
 tag_invoke() wrapper around member.
 
constexpr bool tag_invoke (custom::char_is_valid_for, char_type const c, derived_type) noexcept
 tag_invoke() wrapper around member.
 
constexpr bool tag_invoke (custom::char_is_valid_for, char_type const c, std::type_identity< derived_type >) noexcept
 tag_invoke() wrapper around member.
 
consteval auto tag_invoke (custom::size, derived_type) noexcept
 tag_invoke() wrapper around member.
 
consteval auto tag_invoke (custom::size, std::type_identity< derived_type >) noexcept
 tag_invoke() wrapper around member.
 
constexpr auto tag_invoke (custom::to_char, derived_type const a) noexcept
 tag_invoke() wrapper around member.
 
constexpr auto tag_invoke (custom::to_rank, derived_type const a) noexcept
 tag_invoke() wrapper around member.
 
Comparison operators
constexpr bool operator== (derived_type const lhs, derived_type const rhs)
 Checks whether the letters lhs and rhs are equal.
 
constexpr auto operator<=> (derived_type const lhs, derived_type const rhs)
 Checks order of lhs and rhs.
 

Detailed Description

template<typename derived_type, size_t size, typename char_t = char>
class bio::alphabet::base< derived_type, size, char_t >

A CRTP-base that makes defining a custom alphabet easier.

Template Parameters
derived_typeThe CRTP parameter type.
sizeThe size of the alphabet.
char_tThe character type of the alphabet (set this to void when defining just a bio::alphabet::semialphabet).

You can use this class to define your own alphabet, but types are not required to be based on it to model bio::alphabet::alphabet, it is purely a way to avoid code duplication.

The base class represents the alphabet value as the rank and automatically deduces the rank type from the size and defines all required member functions. The derived type needs to define only the following two tables as static member variables (can be private if the base class is befriended):

  • static std::array<char_type, alphabet_size> constexpr rank_to_char that defines for every possible rank value the corresponding char value.
  • static std::array<rank_type, 256> constexpr char_to_rank that defines for every possible character value the corresponding rank value (adapt size if char_type isn't char).

Example

This creates an alphabet called ab which has size two and the two letters 'A' and 'B':

class ab : public bio::alphabet::base<ab, 2>
{
private:
// map 0 -> A and 1 -> B
static std::array<char_type, alphabet_size> constexpr rank_to_char{'A', 'B'};
// map every letter to rank zero, except Bs
static std::array<rank_type, 256> constexpr char_to_rank =
// initialise with an immediately evaluated lambda expression:
[]()
{
std::array<rank_type, 256> ret{}; // initialise all values with 0 / 'A'
// only 'b' and 'B' result in rank 1
ret['b'] = 1;
ret['B'] = 1;
return ret;
}();
// make the base class a friend so it can access the tables:
friend base<ab, 2>;
};
Core alphabet concept and free function/type trait wrappers.
Provides bio::alphabet::base.
A CRTP-base that makes defining a custom alphabet easier.
Definition: base.hpp:55
The generic alphabet concept that covers most data types used in ranges.
Definition: concept.hpp:643

Constructor & Destructor Documentation

◆ ~base()

template<typename derived_type , size_t size, typename char_t = char>
bio::alphabet::base< derived_type, size, char_t >::~base ( )
defaultnoexcept

Defaulted.

Member Function Documentation

◆ assign_char()

template<typename derived_type , size_t size, typename char_t = char>
constexpr derived_type & bio::alphabet::base< derived_type, size, char_t >::assign_char ( char_type const  c)
inlineconstexprnoexcept

Assign from a character, implicitly converts invalid characters.

Parameters
cThe character to be assigned.

Provides an implementation for bio::alphabet::assign_char_to, required to model bio::alphabet::alphabet.

Complexity

Constant.

Exceptions

Guaranteed not to throw.

◆ assign_rank()

template<typename derived_type , size_t size, typename char_t = char>
constexpr derived_type & bio::alphabet::base< derived_type, size, char_t >::assign_rank ( rank_type const  c)
inlineconstexprnoexcept

Assign from a numeric value.

Parameters
cThe rank to be assigned.

Provides an implementation for bio::alphabet::assign_rank_to, required to model bio::alphabet::semialphabet.

Complexity

Constant.

Exceptions

Guaranteed not to throw.

◆ to_char()

template<typename derived_type , size_t size, typename char_t = char>
constexpr char_type bio::alphabet::base< derived_type, size, char_t >::to_char ( ) const
inlineconstexprnoexcept

Return the letter as a character of char_type.

Provides an implementation for bio::alphabet::to_char, required to model bio::alphabet::alphabet.

Complexity

Constant.

Exceptions

Guaranteed not to throw.

◆ to_rank()

template<typename derived_type , size_t size, typename char_t = char>
constexpr rank_type bio::alphabet::base< derived_type, size, char_t >::to_rank ( ) const
inlineconstexprnoexcept

Return the letter's numeric value (rank in the alphabet).

Provides an implementation for bio::alphabet::to_rank, required to model bio::alphabet::semialphabet.

Complexity

Constant.

Exceptions

Guaranteed not to throw.


The documentation for this class was generated from the following file: