SeqAn3
alphabet_base.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (chr) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (chr) 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 
40 #pragma once
41 
44 #include <seqan3/std/concepts>
45 
46 namespace seqan3
47 {
48 
76 template <typename derived_type, size_t size, typename char_t = char>
78 {
79 public:
80  //TODO: this should be > 1 and there should be a different specialisation for size 1
81  static_assert(size > 0, "It does not make sense to use the base class for alphabets of size < 1.");
82 
86  using char_type = char_t;
89  using rank_type = detail::min_viable_uint_t<size - 1>;
91 
95  constexpr alphabet_base() noexcept : rank{} {}
96  constexpr alphabet_base(alphabet_base const &) = default;
97  constexpr alphabet_base(alphabet_base &&) = default;
98  constexpr alphabet_base & operator=(alphabet_base const &) = default;
99  constexpr alphabet_base & operator=(alphabet_base &&) = default;
100  ~alphabet_base() = default;
102 
120  constexpr char_type to_char() const noexcept
122  requires !std::Same<char_type, void>
124  {
125  return derived_type::rank_to_char[rank];
126  }
127 
142  constexpr rank_type to_rank() const noexcept
143  {
144  return rank;
145  }
147 
165  constexpr derived_type & assign_char(std::conditional_t<std::Same<char_type, void>, char, char_type> const c) noexcept
169  {
170  using index_t = std::make_unsigned_t<char_type>;
171  rank = derived_type::char_to_rank[static_cast<index_t>(c)];
172  return static_cast<derived_type &>(*this);
173  }
174 
189  constexpr derived_type & assign_rank(rank_type const c) noexcept
190  {
191  assert(static_cast<size_t>(c) < static_cast<size_t>(value_size));
192  rank = c;
193  return static_cast<derived_type &>(*this);
194  }
196 
198  static detail::min_viable_uint_t<size> constexpr value_size = size;
199 
202  friend constexpr bool operator==(derived_type const & lhs, derived_type const & rhs) noexcept
203  {
204  using seqan3::to_rank;
205  return to_rank(lhs) == to_rank(rhs);
206  }
207 
208  friend constexpr bool operator!=(derived_type const & lhs, derived_type const & rhs) noexcept
209  {
210  using seqan3::to_rank;
211  return to_rank(lhs) != to_rank(rhs);
212  }
213 
214  friend constexpr bool operator<(derived_type const & lhs, derived_type const & rhs) noexcept
215  {
216  using seqan3::to_rank;
217  return to_rank(lhs) < to_rank(rhs);
218  }
219 
220  friend constexpr bool operator>(derived_type const & lhs, derived_type const & rhs) noexcept
221  {
222  using seqan3::to_rank;
223  return to_rank(lhs) > to_rank(rhs);
224  }
225 
226  friend constexpr bool operator<=(derived_type const & lhs, derived_type const & rhs) noexcept
227  {
228  using seqan3::to_rank;
229  return to_rank(lhs) <= to_rank(rhs);
230  }
231 
232  friend constexpr bool operator>=(derived_type const & lhs, derived_type const & rhs) noexcept
233  {
234  using seqan3::to_rank;
235  return to_rank(lhs) >= to_rank(rhs);
236  }
238 
239 private:
241  rank_type rank;
242 
245  using derived_t = derived_type;
246 
248  friend derived_type;
249 };
250 
251 } // namespace seqan3
detail::min_viable_uint_t< size - 1 > rank_type
The type of the alphabet when represented as a number (e.g. via to_rank()).
Definition: alphabet_base.hpp:89
char_t char_type
The type of the alphabet when converted to char (e.g. via to_char()).
Definition: alphabet_base.hpp:87
Free function/metafunction wrappers for alphabets with member functions/types.
Contains metaprogramming utilities for integer types.
SeqAn specific customisations in the standard namespace.
Definition: align_result.hpp:221
::ranges::size size
Alias for ranges::size. Obtains the size of a range whose size can be calculated in constant time...
Definition: ranges:195
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
constexpr derived_type & assign_rank(rank_type const c) noexcept
Assign from a numeric value.
Definition: alphabet_base.hpp:189
static detail::min_viable_uint_t< size > constexpr value_size
The size of the alphabet, i.e. the number of different values it can take.
Definition: alphabet_base.hpp:198
The Concepts library.
The concept std::Same<T, U> is satisfied if and only if T and U denote the same type.
constexpr char_type to_char() const noexcept
Return the letter as a character of char_type.
Definition: alphabet_base.hpp:120
constexpr derived_type & assign_char(std::conditional_t< std::Same< char_type, void >, char, char_type > const c) noexcept
Assign from a character.
Definition: alphabet_base.hpp:165
constexpr rank_type to_rank() const noexcept
Return the letter&#39;s numeric value (rank in the alphabet).
Definition: alphabet_base.hpp:142
A CRTP-base that makes defining a custom alphabet easier.
Definition: alphabet_base.hpp:77
Implementation of a masked alphabet to be used for cartesian compositions.
Definition: mask.hpp:61
constexpr underlying_rank_t< alphabet_type > to_rank(alphabet_type const alph) requires requires(alphabet_type alph)
Implementation of seqan3::semi_alphabet_concept::to_rank() that delegates to a member function...
Definition: member_exposure.hpp:97