SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
seqan3::alphabet_base< derived_type, size, char_t > Class Template Reference

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

#include <seqan3/alphabet/alphabet_base.hpp>

+ Inheritance diagram for seqan3::alphabet_base< derived_type, size, char_t >:

Public Member Functions

Constructors, destructor and assignment
constexpr alphabet_base () noexcept=default
 Defaulted.
 
constexpr alphabet_base (alphabet_base const &) noexcept=default
 Defaulted.
 
constexpr alphabet_base (alphabet_base &&) noexcept=default
 Defaulted.
 
constexpr alphabet_baseoperator= (alphabet_base const &) noexcept=default
 Defaulted.
 
constexpr alphabet_baseoperator= (alphabet_base &&) noexcept=default
 Defaulted.
 
 ~alphabet_base () noexcept=default
 Defaulted.
 
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 chr) 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 detail::min_viable_uint_t< size > 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 = detail::min_viable_uint_t< size - 1 >
 The type of the alphabet when represented as a number (e.g. via to_rank()).
 

Friends

Comparison operators
constexpr bool operator== (derived_type const lhs, derived_type const rhs) noexcept
 Checks whether the letters lhs and rhs are equal.
 
constexpr bool operator!= (derived_type const lhs, derived_type const rhs) noexcept
 Checks whether the letters lhs and rhs are unequal.
 
constexpr bool operator< (derived_type const lhs, derived_type const rhs) noexcept
 Checks whether the letter lhs is smaller than rhs.
 
constexpr bool operator> (derived_type const lhs, derived_type const rhs) noexcept
 Checks whether the letter lhs is greater than rhs.
 
constexpr bool operator<= (derived_type const lhs, derived_type const rhs) noexcept
 Checks whether the letter lhs is smaller than or equal to rhs.
 
constexpr bool operator>= (derived_type const lhs, derived_type const rhs) noexcept
 Checks whether the letter lhs is bigger than or equal to rhs.
 

Detailed Description

template<typename derived_type, size_t size, typename char_t = char>
class seqan3::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 seqan3::semialphabet).

You can use this class to define your own alphabet, but types are not required to be based on it to model seqan3::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 static member functions (can be private if the base class is befriended):

  • static constexpr char_type rank_to_char(rank_type const rank); that defines for every possible rank value the corresponding char value. (The implementation can be a lookup-table or an arithmetic expression.)
  • static constexpr rank_type char_to_rank(char_type const chr); that defines for every possible character value the corresponding rank value (adapt size if char_type isn't char). (The implementation can be a lookup-table or an arithmetic expression.)

Example

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

// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <seqan3/utility/char_operations/transform.hpp> // seqan3::to_lower
class ab : public seqan3::alphabet_base<ab, 2>
{
private:
// make the base class a friend so it can access the tables:
// This function is expected by seqan3::alphabet_base
static constexpr char_type rank_to_char(rank_type const rank)
{
// via a lookup table
return rank_to_char_table[rank];
// or via an arithmetic expression
return rank == 1 ? 'B' : 'A';
}
// This function is expected by seqan3::alphabet_base
static constexpr rank_type char_to_rank(char_type const chr)
{
// via a lookup table
return char_to_rank_table[static_cast<index_t>(chr)];
// or via an arithmetic expression
return seqan3::to_lower(chr) == 'b' ? 1 : 0;
}
private:
// === lookup-table implementation detail ===
// map 0 -> A and 1 -> B
static constexpr std::array<char_type, alphabet_size> rank_to_char_table{'A', 'B'};
// map every letter to rank zero, except Bs
static constexpr std::array<rank_type, 256> char_to_rank_table{
// 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;
}()};
};
// The class ab satisfies the alphabet concept.
static_assert(seqan3::alphabet<ab>);
Core alphabet concept and free function/type trait wrappers.
Provides seqan3::alphabet_base.
A CRTP-base that makes defining a custom alphabet easier.
Definition alphabet_base.hpp:54
The generic alphabet concept that covers most data types used in ranges.
Refines seqan3::alphabet and adds assignability.
constexpr char_type to_lower(char_type const c) noexcept
Converts 'A'-'Z' to 'a'-'z' respectively; other characters are returned as is.
Definition transform.hpp:74
Provides utilities for modifying characters.

This entity is stable. Since version 3.1.

Member Typedef Documentation

◆ char_type

template<typename derived_type , size_t size, typename char_t = char>
using seqan3::alphabet_base< derived_type, size, char_t >::char_type = std::conditional_t<std::same_as<char_t, void>, char, char_t>
protected

The char representation; conditional needed to make semi alphabet definitions legal.

We need a return type for seqan3::alphabet_base::to_char and seqan3::alphabet_base::assign_char other than void to make these in-class definitions valid when char_t is void.

Attention
Please use seqan3::alphabet_char_t to access this type.

This entity is stable. Since version 3.1.

◆ rank_type

template<typename derived_type , size_t size, typename char_t = char>
using seqan3::alphabet_base< derived_type, size, char_t >::rank_type = detail::min_viable_uint_t<size - 1>
protected

The type of the alphabet when represented as a number (e.g. via to_rank()).

Attention
Please use seqan3::alphabet_rank_t to access this type.

This entity is stable. Since version 3.1.

Member Function Documentation

◆ assign_char()

template<typename derived_type , size_t size, typename char_t = char>
constexpr derived_type & seqan3::alphabet_base< derived_type, size, char_t >::assign_char ( char_type const  chr)
inlineconstexprnoexcept

Assign from a character, implicitly converts invalid characters.

Parameters
chrThe character to be assigned.

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

Complexity

Constant.

Exceptions

Guaranteed not to throw.

This entity is stable. Since version 3.1.

◆ assign_rank()

template<typename derived_type , size_t size, typename char_t = char>
constexpr derived_type & seqan3::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 seqan3::assign_rank_to, required to model seqan3::semialphabet.

Complexity

Constant.

Exceptions

Guaranteed not to throw.

This entity is stable. Since version 3.1.

◆ to_char()

template<typename derived_type , size_t size, typename char_t = char>
constexpr char_type seqan3::alphabet_base< derived_type, size, char_t >::to_char ( ) const
inlineconstexprnoexcept

Return the letter as a character of char_type.

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

Complexity

Constant.

Exceptions

Guaranteed not to throw.

This entity is stable. Since version 3.1.

◆ to_rank()

template<typename derived_type , size_t size, typename char_t = char>
constexpr rank_type seqan3::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 seqan3::to_rank, required to model seqan3::semialphabet.

Complexity

Constant.

Exceptions

Guaranteed not to throw.

This entity is stable. Since version 3.1.

Friends And Related Symbol Documentation

◆ operator!=

template<typename derived_type , size_t size, typename char_t = char>
constexpr bool operator!= ( derived_type const  lhs,
derived_type const  rhs 
)
friend

Checks whether the letters lhs and rhs are unequal.

This entity is stable. Since version 3.1.

◆ operator<

template<typename derived_type , size_t size, typename char_t = char>
constexpr bool operator< ( derived_type const  lhs,
derived_type const  rhs 
)
friend

Checks whether the letter lhs is smaller than rhs.

This entity is stable. Since version 3.1.

◆ operator<=

template<typename derived_type , size_t size, typename char_t = char>
constexpr bool operator<= ( derived_type const  lhs,
derived_type const  rhs 
)
friend

Checks whether the letter lhs is smaller than or equal to rhs.

This entity is stable. Since version 3.1.

◆ operator==

template<typename derived_type , size_t size, typename char_t = char>
constexpr bool operator== ( derived_type const  lhs,
derived_type const  rhs 
)
friend

Checks whether the letters lhs and rhs are equal.

This entity is stable. Since version 3.1.

◆ operator>

template<typename derived_type , size_t size, typename char_t = char>
constexpr bool operator> ( derived_type const  lhs,
derived_type const  rhs 
)
friend

Checks whether the letter lhs is greater than rhs.

This entity is stable. Since version 3.1.

◆ operator>=

template<typename derived_type , size_t size, typename char_t = char>
constexpr bool operator>= ( derived_type const  lhs,
derived_type const  rhs 
)
friend

Checks whether the letter lhs is bigger than or equal to rhs.

This entity is stable. Since version 3.1.

Member Data Documentation

◆ alphabet_size

template<typename derived_type , size_t size, typename char_t = char>
constexpr detail::min_viable_uint_t<size> seqan3::alphabet_base< derived_type, size, char_t >::alphabet_size = size
staticconstexpr

The size of the alphabet, i.e. the number of different values it can take.

This entity is stable. Since version 3.1.


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