SeqAn3
char.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
53 #pragma once
54 
55 #include <limits>
56 
57 #include <meta/meta.hpp>
58 
61 
62 namespace seqan3::detail
63 {
66 
68 using char_adaptations = meta::list<char,
69  char16_t,
70  char32_t,
71  wchar_t>;
72 // TODO Windows adaption requires different handling of wchar_t, in Windows it is 16 bits and holds UTF-16 code units
74 using char_adaptations_rank_types = meta::list<std::uint_least8_t,
75  std::uint_least16_t,
76  std::uint_least32_t,
77  std::uint_least32_t>;
78 
80 template <typename type_in_list>
82  requires meta::in<char_adaptations, type_in_list>::value
84 struct is_char_adaptation<type_in_list> :
85  std::true_type
86 {};
87 
89 } // namespace seqan3::detail
90 
91 namespace seqan3
92 {
93 
96 
97 // ------------------------------------------------------------------
98 // type metafunctions
99 // ------------------------------------------------------------------
100 
106 template <typename char_type>
108  requires detail::is_char_adaptation_v<char_type>
110 struct underlying_char<char_type>
111 {
113  using type = char_type;
114 };
115 
121 template <typename char_type>
123  requires detail::is_char_adaptation_v<char_type>
125 struct underlying_rank<char_type>
126 {
128  using type = meta::at<detail::char_adaptations_rank_types,
129  meta::find_index<detail::char_adaptations, char_type>>;
130 };
131 
132 // ------------------------------------------------------------------
133 // value metafunctions
134 // ------------------------------------------------------------------
135 
141 template <typename char_type>
143  requires detail::is_char_adaptation_v<char_type>
145 struct alphabet_size<char_type>
146 {
148  using type = detail::min_viable_uint_t<static_cast<uint64_t>(std::numeric_limits<char_type>::max()) + 1 -
149  std::numeric_limits<char_type>::lowest()>;
151  static constexpr type value =
152  static_cast<type>(std::numeric_limits<char_type>::max()) + 1 - std::numeric_limits<char_type>::lowest();
153 };
154 
155 // ------------------------------------------------------------------
156 // free functions
157 // ------------------------------------------------------------------
158 
170 template <typename char_type>
171 constexpr underlying_char_t<char_type> to_char(char_type const chr)
172  requires detail::is_char_adaptation_v<char_type>
173 {
174  return chr;
175 }
176 
182 template <typename char_type>
183 constexpr underlying_rank_t<char_type> to_rank(char_type const chr)
184  requires detail::is_char_adaptation_v<char_type>
185 {
186  return chr;
187 }
188 
195 template <typename char_type>
196 constexpr char_type & assign_char(char_type & chr, underlying_char_t<char_type> const chr2)
197  requires detail::is_char_adaptation_v<char_type>
198 {
199  return chr = chr2;
200 }
201 
208 template <typename char_type>
209 constexpr char_type && assign_char(char_type && chr, underlying_char_t<char_type> const chr2)
210  requires detail::is_char_adaptation_v<std::remove_reference_t<char_type>>
211 {
212  return std::move(chr = chr2);
213 }
214 
221 template <typename char_type>
222 constexpr char_type & assign_rank(char_type & chr, underlying_rank_t<char_type> const rank)
223  requires detail::is_char_adaptation_v<char_type>
224 {
225  return chr = rank;
226 }
227 
234 template <typename char_type>
235 constexpr char_type && assign_rank(char_type && chr, underlying_rank_t<char_type> const rank)
236  requires detail::is_char_adaptation_v<std::remove_reference_t<char_type>>
237 {
238  return std::move(chr = rank);
239 }
242 
243 } // namespace seqan3
244 
245 // concept tests in alphabet/adaptation.hpp because of include order constraints
constexpr char_type && assign_rank(char_type &&chr, underlying_rank_t< char_type > const rank) requires detail
Assigning a rank to a char is the same as assigning it a numeric value.
Definition: char.hpp:235
The rank_type of the semi_alphabet. [type metafunction base template].
Definition: concept_pre.hpp:80
typename underlying_rank< semi_alphabet_type >::type underlying_rank_t
The rank_type of the semi_alphabet. [type metafunction shortcut].
Definition: concept_pre.hpp:88
Contains metaprogramming utilities for integer types.
constexpr underlying_char_t< char_type > to_char(char_type const chr) requires detail
Converting char to char is no-op (it will just return the value you pass in).
Definition: char.hpp:171
constexpr char_type & assign_char(char_type &chr, underlying_char_t< char_type > const chr2) requires detail
Assign a char to the char type (same as calling =).
Definition: char.hpp:196
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
seqan3::alphabet_concept metafunction base classes.
constexpr underlying_rank_t< char_type > to_rank(char_type const chr) requires detail
Convert char to rank by casting to an unsigned integral type of same size.
Definition: char.hpp:183
detail::min_viable_uint_t< static_cast< uint64_t >(std::numeric_limits< char_type >::max())+1 - std::numeric_limits< char_type >::lowest()> type
Smallest unsigned integral type that can hold value;.
Definition: char.hpp:149
constexpr char_type && assign_char(char_type &&chr, underlying_char_t< char_type > const chr2) requires detail
Assign a char to the char type (same as calling =).
Definition: char.hpp:209
char_type type
The same type as char_type.
Definition: char.hpp:113
constexpr char_type & assign_rank(char_type &chr, underlying_rank_t< char_type > const rank) requires detail
Assigning a rank to a char is the same as assigning it a numeric value.
Definition: char.hpp:222
The size of the alphabet. [value metafunction base template].
Definition: concept_pre.hpp:104
Definition: aligned_sequence_concept.hpp:288
meta::at< detail::char_adaptations_rank_types, meta::find_index< detail::char_adaptations, char_type > > type
An unsigned integer type of the same size as char_type.
Definition: char.hpp:129
The char_type of the alphabet. [type metafunction base template].
Definition: concept_pre.hpp:164
typename underlying_char< alphabet_type >::type underlying_char_t
The char_type of the alphabet. [type metafunction shortcut].
Definition: concept_pre.hpp:172