Skip to content

Prototypes:alphabet.hpp

Hannes Hauswedell edited this page Feb 7, 2017 · 3 revisions

alphabet.hpp:

#pragma once

#include <iostream>
#include <string>

namespace seqan3
{

// ------------------------------------------------------------------
// concept
// ------------------------------------------------------------------

template <typename T>
concept bool Alphabet = requires (T t1, T t2)
{
    // STL concepts
    requires std::is_pod_v<T> == true;
    requires std::is_swappable_v<T> == true;

    // actual data member
    t1.value;
    requires std::is_same_v<decltype(t1.value), char> == true;

    // required static members //TODO more details
    T::value_size;
    T::rank_to_value;
    T::value_to_rank;
//     T::convert(char const) -> char;

    // conversion from/to char
    //TODO

    // required comparison operators
    { t1 == t2 } -> bool;
    { t1 != t2 } -> bool;
    { t1 <  t2 } -> bool;
    { t1 >  t2 } -> bool;
    { t1 <= t2 } -> bool;
    { t1 >= t2 } -> bool;
};

} // namespace seqan3

alphabet_container.hpp:

#pragma once

#include <algorithm>
#include <iostream>
#include <string>

#include "alphabet.hpp"

namespace seqan3
{

// ------------------------------------------------------------------
// alphabet_traits
// ------------------------------------------------------------------

template <Alphabet TSeqanAlphabet>
struct alphabet_traits : public std::char_traits<char>
{
    using char_type = TSeqanAlphabet;
    using int_type = uint8_t;
    using off_type = std::streamoff;
    using pos_type = std::streampos;
    using state_type = std::mbstate_t;

    static constexpr void assign(char_type & r, char_type const & a) noexcept
    {
        r.value = a.value;
    }
    static char_type* assign(char_type * p, std::size_t count, char_type a)
    {
        std::for_each(p, p + count, [&a] (char_type & c) { c.value = a.value; });
        return p;
    }

    static constexpr bool eq(char_type a, char_type b) noexcept
    {
        return a == b;
    };

    static constexpr bool lt(char_type a, char_type b) noexcept
    {
        return a < b;
    };

    static char_type* move(char_type * dest, const char_type* src, std::size_t count)
    {
        for (std::size_t i = 0; i < count; ++i)
            assign(dest[i], src[i]);

        return dest;
    }

    static char_type* copy(char_type * dest, const char_type* src, std::size_t count)
    {
        std::copy(src, src + count, dest);
        return dest;
    }

    static constexpr int compare(const char_type* s1, const char_type* s2, std::size_t count)
    {
        for (std::size_t i = 0; i < count; ++i)
        {
            if (s1[i] < s2[i])
                return -1;
            if (s1[i] > s2[i])
                return 1;
        }
        return 0;
    }

    static constexpr std::size_t length(const char_type* s)
    {
        for (std::size_t i = 0; true; ++i)
            if (s[i] == 0)
                return i;
    }

    static constexpr const char_type* find(const char_type* p, std::size_t count, char_type const & ch)
    {
        for (std::size_t i = 0; i < count; ++i)
            if (p[i] == ch)
                return p[i];
    }

    static constexpr char_type to_char_type(int_type i) noexcept
    {
        return char_type::rank_to_value(i);
    }

    static constexpr int_type to_int_type(char_type c) noexcept
    {
        return char_type::value_to_rank(c);
    }

    static constexpr bool eq_int_type(int_type i1, int_type i2) noexcept
    {
        return i1 == i2;
    }

    static constexpr int_type eof() noexcept
    {
        return 0;
    }

    static constexpr int_type not_eof(int_type i) noexcept
    {
        if (i < char_type::value_size)
            return i;
        else
            return 0;
    }
};

// ------------------------------------------------------------------
// ostream operator
// ------------------------------------------------------------------

template <Alphabet TSeqanAlphabet, typename TTraits>
std::ostream& operator<<(std::ostream & os, std::basic_string<TSeqanAlphabet, TTraits> const & str)
{
    for (auto c : str)
        os << c;
    return os;
}

template <Alphabet TSeqanAlphabet>
std::ostream& operator<<(std::ostream & os, std::vector<TSeqanAlphabet> const & str)
{
    for (auto c : str)
        os << c;
    return os;
}

}
Clone this wiki locally