BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
bio::alphabet::semialphabet_any< size_ > Class Template Reference

A semi-alphabet that type erases all other semi-alphabets of the same size. More...

#include <bio/alphabet/composite/semialphabet_any.hpp>

+ Inheritance diagram for bio::alphabet::semialphabet_any< size_ >:

Public Member Functions

template<meta::different_from< semialphabet_any > other_alph_t>
requires (semialphabet<other_alph_t> && (size<other_alph_t> == size_) && std::regular<other_alph_t>)
 operator other_alph_t () const
 Enable conversion of semialphabet_any into other (semi-)alphabet of the same size.
 
Constructors, destructor and assignment
constexpr semialphabet_any () noexcept=default
 Defaulted.
 
constexpr semialphabet_any (semialphabet_any const &) noexcept=default
 Defaulted.
 
constexpr semialphabet_any (semialphabet_any &&) noexcept=default
 Defaulted.
 
constexpr semialphabet_anyoperator= (semialphabet_any const &) noexcept=default
 Defaulted.
 
constexpr semialphabet_anyoperator= (semialphabet_any &&) noexcept=default
 Defaulted.
 
 ~semialphabet_any () noexcept=default
 Defaulted.
 
template<meta::different_from< semialphabet_any > other_alph_t>
requires (semialphabet<other_alph_t> && size<other_alph_t> == size_)
 semialphabet_any (other_alph_t const other)
 Construct semialphabet_any from alphabet of the same size.
 
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 semialphabet_any< size_ > & assign_char (char_type const c) noexcept
 Assign from a character, implicitly converts invalid characters.
 
constexpr semialphabet_any< size_ > & assign_rank (rank_type const c) noexcept
 Assign from a numeric value.
 

Static Public Attributes

static constexpr size_t alphabet_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< void, void >, char, void >
 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()).
 

Detailed Description

template<size_t size_>
class bio::alphabet::semialphabet_any< size_ >

A semi-alphabet that type erases all other semi-alphabets of the same size.

This alphabet provides a generic representation for different alphabets of the same size by erasing the type of the alphabet it is constructed from. This enables the usage of a single type although assigning different alphabets. The semialphabet_any can also be converted to any other (semi-)alphabet of the same size. It is therefore possible to convert the semialphabet_any into an alphabet type that is not the original alphabet type. However, this should either be avoided or used with care as no warnings are given when attempting to convert the semialphabet_any into a type that is not comparable to the original alphabet type. The main advantage of using this data structure is to reduce instantiation of templates when using multiple alphabets of the same size and either their character representation is not important or they are reified at a later point in the program.

See also
https://en.wikipedia.org/wiki/Type_erasure
https://en.wikipedia.org/wiki/Reification_(computer_science)

Example

// This example illustrates how we can reduce the usage of templates (or the amount of different instantiations) via
// type erasure. Having only one function generated for `algorithm()` is the only benefit of using `semialphabet_any`
// here. Of course this only makes sense for your application if the part of the program that is agnostic of the
// character representation (your equivalent of `algorithm()`) is substantially larger than the specific parts – and
// if compile-time and/or size of the exectuble are a concern.
// READ THE FUNCTIONS FROM BUTTOM TO TOP!
#include <iostream>
using namespace bio::alphabet::literals;
// Print is a template again; you could do further processing of the reified types here
template <typename rng_t>
void print(rng_t && r)
{
fmt::print("{}\n", r);
}
// Algorithm is not a template, only one instance is generated by the compiler
// Type information is encoded via a run-time parameter
void algorithm(std::vector<bio::alphabet::semialphabet_any<10> > & r, bool is_murphy)
{
// Algorithm example that replaces rank 0 with rank 1
for (auto & v : r)
// Here we reify the type for printing
if (is_murphy)
print(r | bio::views::convert<bio::alphabet::aa10murphy>);
else
print(r | bio::views::convert<bio::alphabet::aa10li>);
}
// Two instances of algo_pre exist
// They type erase the different arguments to the same type and encode the type information as a run-time parameter
void algo_pre(std::vector<bio::alphabet::aa10li> const & v)
{
std::vector<bio::alphabet::semialphabet_any<10> > tmp = v | bio::views::convert<bio::alphabet::semialphabet_any<10>>
| bio::ranges::to<std::vector>();
algorithm(tmp, false);
}
{
std::vector<bio::alphabet::semialphabet_any<10> > tmp = v | bio::views::convert<bio::alphabet::semialphabet_any<10>>
| bio::ranges::to<std::vector>();
algorithm(tmp, true);
}
int main()
{
std::vector<bio::alphabet::aa10li> v1{"AIKAAAKCB"_aa10li};
algo_pre(v1); // AIKAAAKCB
std::vector<bio::alphabet::aa10murphy> v2{"FIKAFAKCB"_aa10murphy};
algo_pre(v2); // FIKAFAKCB
}
Meta-header for the aminoacid submodule; includes all headers from alphabet/aminoacid/.
A semi-alphabet that type erases all other semi-alphabets of the same size.
Definition: semialphabet_any.hpp:47
Core alphabet concept and free function/type trait wrappers.
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:70
constexpr auto assign_rank_to
Assign a rank to an alphabet object.
Definition: concept.hpp:138
An inline namespace for alphabet literals. It exists to safely allow using namespace.
Definition: aa10li.hpp:183
Provides bio::views::convert.
Provides bio::alphabet::semialphabet_any.
An implementation of C++23's std::ranges::to.

Member Function Documentation

◆ assign_char()

constexpr semialphabet_any< size_ > & bio::alphabet::base< semialphabet_any< size_ > , size, void >::assign_char ( char_type const  c)
inlineconstexprnoexceptinherited

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()

constexpr semialphabet_any< size_ > & bio::alphabet::base< semialphabet_any< size_ > , size, void >::assign_rank ( rank_type const  c)
inlineconstexprnoexceptinherited

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()

constexpr char_type bio::alphabet::base< semialphabet_any< size_ > , size, void >::to_char ( ) const
inlineconstexprnoexceptinherited

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()

constexpr rank_type bio::alphabet::base< semialphabet_any< size_ > , size, void >::to_rank ( ) const
inlineconstexprnoexceptinherited

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: