Skip to content

Commit

Permalink
[MISC] introduce alphabet_variant::{rank_to_char, char_to_rank} function
Browse files Browse the repository at this point in the history
  • Loading branch information
marehr committed Mar 11, 2021
1 parent b6ee2e8 commit 8710362
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
45 changes: 29 additions & 16 deletions include/seqan3/alphabet/composite/alphabet_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,8 @@ class alphabet_variant : public alphabet_base<alphabet_variant<alternative_types
return partial_sum;
}();

/*!\brief Compile-time generated lookup table which maps the rank to char.
*
* A map generated at compile time where the key is the rank of the variant
* of all alternatives and the value is the corresponding char of that rank
* and alternative.
*
*/
static constexpr std::array<char_type, alphabet_size> rank_to_char = []() constexpr
//!\copydoc seqan3::alphabet_variant::rank_to_char
static constexpr std::array<char_type, alphabet_size> rank_to_char_table = []() constexpr
{
// Explicitly writing assign_rank_to_char within assign_rank_to_char
// causes this bug (g++-7 and g++-8):
Expand Down Expand Up @@ -517,6 +511,18 @@ class alphabet_variant : public alphabet_base<alphabet_variant<alternative_types
return value_to_char;
}();

/*!\brief Compile-time generated lookup table which maps the rank to char.
*
* A map generated at compile time where the key is the rank of the variant
* of all alternatives and the value is the corresponding char of that rank
* and alternative.
*
*/
static constexpr char_type rank_to_char(rank_type const rank)
{
return rank_to_char_table[rank];
}

//!\brief Converts an object of one of the given alternatives into the internal representation.
//!\tparam index The position of `alternative_t` in the template pack `alternative_types`.
//!\tparam alternative_t One of the alternative types.
Expand Down Expand Up @@ -578,14 +584,8 @@ class alphabet_variant : public alphabet_base<alphabet_variant<alternative_types
}()
};

/*!\brief Compile-time generated lookup table which maps the char to rank.
*
* An map generated at compile time where the key is the char of one of the
* alternatives and the value is the corresponding rank over all alternatives (by
* conflict will default to the first).
*
*/
static constexpr std::array<rank_type, detail::size_in_values_v<char_type>> char_to_rank = []() constexpr
//!\copydoc seqan3::alphabet_variant::char_to_rank
static constexpr std::array<rank_type, detail::size_in_values_v<char_type>> char_to_rank_table = []() constexpr
{
constexpr size_t alternative_size = sizeof...(alternative_types);
constexpr size_t table_size = detail::size_in_values_v<char_type>;
Expand All @@ -604,6 +604,19 @@ class alphabet_variant : public alphabet_base<alphabet_variant<alternative_types

return char_to_rank;
}();

/*!\brief Compile-time generated lookup table which maps the char to rank.
*
* A map generated at compile time where the key is the char of one of the
* alternatives and the value is the corresponding rank over all alternatives (by
* conflict will default to the first).
*
*/
static constexpr rank_type char_to_rank(char_type const chr)
{
using index_t = std::make_unsigned_t<char_type>;
return char_to_rank_table[static_cast<index_t>(chr)];
}
};

} // namespace seqan3
28 changes: 18 additions & 10 deletions test/unit/alphabet/composite/alphabet_variant_detail_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class detail_alphabet_variant : public seqan3::alphabet_variant<alphabet_types..
{
public:
using seqan3::alphabet_variant<alphabet_types...>::partial_sum_sizes;
using seqan3::alphabet_variant<alphabet_types...>::rank_to_char;
using seqan3::alphabet_variant<alphabet_types...>::char_to_rank;
using seqan3::alphabet_variant<alphabet_types...>::rank_to_char_table;
using seqan3::alphabet_variant<alphabet_types...>::char_to_rank_table;
using seqan3::alphabet_variant<alphabet_types...>::first_valid_char_table;
};

Expand Down Expand Up @@ -51,17 +51,19 @@ TEST(alphabet_variant_detail_test, partial_sum_sizes)
EXPECT_EQ(partial_sum4[3], 10);
}

TEST(alphabet_variant_detail_test, rank_to_char)
TEST(alphabet_variant_detail_test, rank_to_char_table)
{
constexpr std::array rank_to_char2 = detail_alphabet_variant<seqan3::dna4, seqan3::gap>::rank_to_char;
constexpr std::array rank_to_char2 = detail_alphabet_variant<seqan3::dna4, seqan3::gap>::rank_to_char_table;
EXPECT_EQ(rank_to_char2.size(), 5u);
EXPECT_EQ(rank_to_char2[0], 'A');
EXPECT_EQ(rank_to_char2[1], 'C');
EXPECT_EQ(rank_to_char2[2], 'G');
EXPECT_EQ(rank_to_char2[3], 'T');
EXPECT_EQ(rank_to_char2[4], '-');

constexpr std::array rank_to_char3 = detail_alphabet_variant<seqan3::dna4, seqan3::gap, seqan3::dna5>::rank_to_char;
constexpr std::array rank_to_char3 = detail_alphabet_variant<seqan3::dna4,
seqan3::gap,
seqan3::dna5>::rank_to_char_table;
EXPECT_EQ(rank_to_char3.size(), 10u);
EXPECT_EQ(rank_to_char3[0], 'A');
EXPECT_EQ(rank_to_char3[1], 'C');
Expand All @@ -74,7 +76,9 @@ TEST(alphabet_variant_detail_test, rank_to_char)
EXPECT_EQ(rank_to_char3[8], 'N');
EXPECT_EQ(rank_to_char3[9], 'T');

constexpr std::array rank_to_char4 = detail_alphabet_variant<seqan3::dna5, seqan3::gap, seqan3::dna4>::rank_to_char;
constexpr std::array rank_to_char4 = detail_alphabet_variant<seqan3::dna5,
seqan3::gap,
seqan3::dna4>::rank_to_char_table;
EXPECT_EQ(rank_to_char4.size(), 10u);
EXPECT_EQ(rank_to_char4[0], 'A');
EXPECT_EQ(rank_to_char4[1], 'C');
Expand All @@ -88,17 +92,19 @@ TEST(alphabet_variant_detail_test, rank_to_char)
EXPECT_EQ(rank_to_char4[9], 'T');
}

TEST(alphabet_variant_detail_test, char_to_rank)
TEST(alphabet_variant_detail_test, char_to_rank_table)
{
constexpr std::array char_to_rank2 = detail_alphabet_variant<seqan3::dna4, seqan3::gap>::char_to_rank;
constexpr std::array char_to_rank2 = detail_alphabet_variant<seqan3::dna4, seqan3::gap>::char_to_rank_table;
EXPECT_EQ(char_to_rank2.size(), 256u);
EXPECT_EQ(char_to_rank2['A'], 0);
EXPECT_EQ(char_to_rank2['C'], 1);
EXPECT_EQ(char_to_rank2['G'], 2);
EXPECT_EQ(char_to_rank2['T'], 3);
EXPECT_EQ(char_to_rank2['-'], 4);

constexpr std::array char_to_rank3 = detail_alphabet_variant<seqan3::dna4, seqan3::gap, seqan3::dna5>::char_to_rank;
constexpr std::array char_to_rank3 = detail_alphabet_variant<seqan3::dna4,
seqan3::gap,
seqan3::dna5>::char_to_rank_table;
EXPECT_EQ(char_to_rank3.size(), 256u);
EXPECT_EQ(char_to_rank3['A'], 0);
EXPECT_EQ(char_to_rank3['C'], 1);
Expand All @@ -111,7 +117,9 @@ TEST(alphabet_variant_detail_test, char_to_rank)
EXPECT_EQ(char_to_rank3['N'], 8);
EXPECT_EQ(char_to_rank3['T'], 3);

constexpr std::array char_to_rank4 = detail_alphabet_variant<seqan3::dna5, seqan3::gap, seqan3::dna4>::char_to_rank;
constexpr std::array char_to_rank4 = detail_alphabet_variant<seqan3::dna5,
seqan3::gap,
seqan3::dna4>::char_to_rank_table;
EXPECT_EQ(char_to_rank4.size(), 256u);
EXPECT_EQ(char_to_rank4['A'], 0);
EXPECT_EQ(char_to_rank4['C'], 1);
Expand Down

0 comments on commit 8710362

Please sign in to comment.