Loading...
Searching...
No Matches
seqan3::semialphabet_any< size > Class Template Reference
Alphabet » Composite

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

`#include <seqan3/alphabet/composite/semialphabet_any.hpp>`

Inheritance diagram for seqan3::semialphabet_any< size >:
[legend]

## Public Member Functions

constexpr semialphabet_any< size > & | assign_rank (rank_type const c) noexcept
| Assign from a numeric value.
template<typename other_alph_t> requires (!std::same_as<other_alph_t, semialphabet_any>) && semialphabet<other_alph_t> && (alphabet_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.
constexpr rank_type | to_rank () const noexcept
| Return the letter's numeric value (rank in the alphabet).
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_any & | operator= (semialphabet_any const &) noexcept=default
| Defaulted.
constexpr semialphabet_any & | operator= (semialphabet_any &&) noexcept=default
| Defaulted.
| ~semialphabet_any () noexcept=default
| Defaulted.
template<typename other_alph_t> requires (!std::same_as<other_alph_t, semialphabet_any>) && semialphabet<other_alph_t> && (alphabet_size<other_alph_t> == size)
| semialphabet_any (other_alph_t const other)
| Construct semialphabet_any from alphabet of the same size.
Public Member Functions inherited from seqan3::alphabet_base< semialphabet_any< size >, size, void >
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_base & | operator= (alphabet_base const &) noexcept=default
| Defaulted.
constexpr alphabet_base & | operator= (alphabet_base &&) noexcept=default
| Defaulted.
| ~alphabet_base () noexcept=default
| Defaulted.
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).
constexpr semialphabet_any< size > & | assign_char (char_type const chr) 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.

## Additional Inherited Members

Static Public Attributes inherited from seqan3::alphabet_base< semialphabet_any< size >, size, void >
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.
Protected Types inherited from seqan3::alphabet_base< semialphabet_any< size >, size, void >
using | char_type
| The char representation; conditional needed to make semi alphabet definitions legal.
using | rank_type
| The type of the alphabet when represented as a number (e.g. via to_rank()).

## Detailed Description

template<size_t size>
class seqan3::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

// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
// 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 executable are a concern.
#include <iostream>
#include <seqan3/alphabet/aminoacid/all.hpp>
#include <seqan3/alphabet/composite/semialphabet_any.hpp>
#include <seqan3/core/debug_stream.hpp>
#include <seqan3/utility/range/to.hpp>
using namespace seqan3::literals;
// Print is a template and gets instantiated two times because the behaviour is different for both types
template <typename rng_t>
void print(rng_t && r)
{
seqan3::debug_stream << r << '\n';
}
// 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<seqan3::semialphabet_any<10>> & r, bool is_murphy)
{
// Algorithm example that replaces rank 0 with rank 1
for (auto & v : r)
if (seqan3::to_rank(v) == 0)
seqan3::assign_rank_to(1, v);
// Here we verify the type for printing
if (is_murphy)
print(r
| std::views::transform(
[](auto const & in)
{
return static_cast<seqan3::aa10murphy>(in);
}));
else
print(r
| std::views::transform(
[](auto const & in)
{
return static_cast<seqan3::aa10li>(in);
}));
}
// 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(seqan3::aa10li_vector const & v)
{
std::vector<seqan3::semialphabet_any<10>> tmp = v
| std::views::transform(
[](auto const & in)
{
return static_cast<seqan3::semialphabet_any<10>>(in);
})
| seqan3::ranges::to<std::vector>();
algorithm(tmp, false);
}
void algo_pre(seqan3::aa10murphy_vector const & v)
{
std::vector<seqan3::semialphabet_any<10>> tmp = v
| std::views::transform(
[](auto const & in)
{
return static_cast<seqan3::semialphabet_any<10>>(in);
})
| seqan3::ranges::to<std::vector>();
algorithm(tmp, true);
}
int main()
{
seqan3::aa10li_vector v1{"AVRSTXOUB"_aa10li};
algo_pre(v1); // BIKBBBKCB
seqan3::aa10murphy_vector v2{"AVRSTXOUB"_aa10murphy};
algo_pre(v2); // BIKSSSKCB
}
all.hpp
Meta-header for the Alphabet / Aminoacid submodule .
seqan3::aa10li
The reduced Li amino acid alphabet.
Definition aa10li.hpp:80
seqan3::aa10murphy
The reduced Murphy amino acid alphabet.
Definition aa10murphy.hpp:79
seqan3::semialphabet_any
A semi-alphabet that type erases all other semi-alphabets of the same size.
Definition semialphabet_any.hpp:45
debug_stream.hpp
Provides seqan3::debug_stream and related types.
seqan3::assign_rank_to
constexpr auto assign_rank_to
Assign a rank to an alphabet object.
Definition alphabet/concept.hpp:288
seqan3::to_rank
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition alphabet/concept.hpp:152
seqan3::debug_stream
debug_stream_type debug_stream
A global instance of seqan3::debug_stream_type.
Definition debug_stream.hpp:38
seqan3::ranges::to
seqan::stl::ranges::to to
Converts a range to a container. <dl class="no-api">This entity is not part of the SeqAn API....
Definition to.hpp:23
iostream
seqan3::literals
The SeqAn namespace for literals.
semialphabet_any.hpp
Provides seqan3::semialphabet_any.
to.hpp
Provides seqan3::ranges::to.
std::vector

This entity is stable. Since version 3.1.

## Constructor & Destructor Documentation

## semialphabet_any()

template<size_t size>
template<typename other_alph_t>
requires (!std::same_as<other_alph_t, semialphabet_any>) && semialphabet<other_alph_t> && (alphabet_size<other_alph_t> == size)

seqan3::semialphabet_any< size >::semialphabet_any | ( | other_alph_t const | other | ) |

inline explicit

Construct semialphabet_any from alphabet of the same size.

This entity is stable. Since version 3.1.

## Member Function Documentation

## assign_rank()

template<size_t size>

semialphabet_any< size > & seqan3::alphabet_base< semialphabet_any< size >, size, void >::assign_rank | ( | rank_type const | c | ) |

inline constexpr noexcept

Assign from a numeric value.

Parameters

  c | The 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.

## operator other_alph_t()

template<size_t size>
template<typename other_alph_t>
requires (!std::same_as<other_alph_t, semialphabet_any>) && semialphabet<other_alph_t> && (alphabet_size<other_alph_t> == size) && std::regular<other_alph_t>

seqan3::semialphabet_any< size >::operator other_alph_t | ( |  | ) | const

inline explicit

Enable conversion of semialphabet_any into other (semi-)alphabet of the same size.

This entity is stable. Since version 3.1.

## to_rank()

template<size_t size>

rank_type seqan3::alphabet_base< semialphabet_any< size >, size, void >::to_rank | ( |  | ) | const

inline constexpr noexcept

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.

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

- semialphabet_any.hpp

Hide me