BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
convert.hpp
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
16#pragma once
17
18#include <array>
19
21#include <bio/meta/tag/vtag.hpp>
22
23// ============================================================================
24// conversion to/from char/rank types
25// ============================================================================
26
27namespace bio::alphabet::detail
28{
29
36template <alphabet out_t, alphabet in_t>
37constexpr std::array<out_t, size<in_t>> convert_through_char_representation = []() constexpr
38{
40 // for (decltype(alphabet_size<in_t>) i = 0; ...) causes indefinite compilation :(
41 for (auto i = decltype(size<in_t>){0}; i < size<in_t>; ++i)
42 assign_char_to(to_char(assign_rank_to(i, in_t{})), ret[i]);
43 return ret;
44}();
45
51template <meta::detail::literal_buffer_string str, typename alphabet_t>
52constexpr std::vector<alphabet_t> string_literal()
53{
54 auto illegal_char_at = []() consteval -> int32_t
55 {
56 for (int32_t i = 0; i < (int32_t)str.size(); ++i)
57 if (!char_is_valid_for<alphabet_t>(str[i]))
58 return i;
59 return -1;
60 };
61 static_assert(illegal_char_at() == -1, "Illegal character in string literal.");
62
64 r.resize(str.size());
65
66 for (size_t i = 0; i < str.size(); ++i)
67 r[i].assign_char(str[i]);
68
69 return r;
70}
71
72} // namespace bio::alphabet::detail
Core alphabet concept and free function/type trait wrappers.
constexpr auto to_char
Return the char representation of an alphabet object.
Definition: concept.hpp:192
constexpr auto assign_char_to
Assign a char to an alphabet object.
Definition: concept.hpp:260
constexpr auto assign_rank_to
Assign a rank to an alphabet object.
Definition: concept.hpp:138
T resize(T... args)
Provides bio::meta::vtag.