1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2022 Max Bachmann */
#pragma once
#include <array>
#include <limits>
#include <stdint.h>
#include <stdio.h>
#include <type_traits>
#include <unordered_set>
namespace rapidfuzz {
namespace detail {
/*
* taken from https://stackoverflow.com/a/17251989/11335032
*/
template <typename T, typename U>
bool CanTypeFitValue(const U value)
{
const intmax_t botT = intmax_t(std::numeric_limits<T>::min());
const intmax_t botU = intmax_t(std::numeric_limits<U>::min());
const uintmax_t topT = uintmax_t(std::numeric_limits<T>::max());
const uintmax_t topU = uintmax_t(std::numeric_limits<U>::max());
return !((botT > botU && value < static_cast<U>(botT)) || (topT < topU && value > static_cast<U>(topT)));
}
template <typename CharT1, size_t size = sizeof(CharT1)>
struct CharSet;
template <typename CharT1>
struct CharSet<CharT1, 1> {
using UCharT1 = typename std::make_unsigned<CharT1>::type;
std::array<bool, std::numeric_limits<UCharT1>::max() + 1> m_val;
CharSet() : m_val{}
{}
void insert(CharT1 ch)
{
m_val[UCharT1(ch)] = true;
}
template <typename CharT2>
bool find(CharT2 ch) const
{
if (!CanTypeFitValue<CharT1>(ch)) return false;
return m_val[UCharT1(ch)];
}
};
template <typename CharT1, size_t size>
struct CharSet {
std::unordered_set<CharT1> m_val;
CharSet() : m_val{}
{}
void insert(CharT1 ch)
{
m_val.insert(ch);
}
template <typename CharT2>
bool find(CharT2 ch) const
{
if (!CanTypeFitValue<CharT1>(ch)) return false;
return m_val.find(CharT1(ch)) != m_val.end();
}
};
} // namespace detail
} // namespace rapidfuzz
|