mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-27 21:08:44 +08:00
Merge remote-tracking branch 'origin/development'
This commit is contained in:
commit
8dc79479f7
158
src/debounce.h
158
src/debounce.h
@ -119,7 +119,7 @@ namespace etl
|
||||
uint8_t state;
|
||||
|
||||
/// The state count.
|
||||
int16_t count;
|
||||
uint16_t count;
|
||||
};
|
||||
}
|
||||
|
||||
@ -170,13 +170,12 @@ namespace etl
|
||||
{
|
||||
debounce_base::add(sample);
|
||||
|
||||
if (sample)
|
||||
if (count < REPEAT_THRESHOLD)
|
||||
{
|
||||
// Set sample.
|
||||
if (count < REPEAT_THRESHOLD)
|
||||
++count;
|
||||
|
||||
if (sample)
|
||||
{
|
||||
++count;
|
||||
|
||||
if (count == VALID_THRESHOLD)
|
||||
{
|
||||
// Set.
|
||||
@ -196,16 +195,12 @@ namespace etl
|
||||
state |= REPEATING;
|
||||
count = HOLD_THRESHOLD;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear sample.
|
||||
if (count > -VALID_THRESHOLD)
|
||||
{
|
||||
--count;
|
||||
|
||||
if (count == -VALID_THRESHOLD)
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (count == VALID_THRESHOLD)
|
||||
{
|
||||
// Clear.
|
||||
state |= CHANGED;
|
||||
@ -213,16 +208,9 @@ namespace etl
|
||||
state &= ~HELD;
|
||||
state &= ~REPEATING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sample)
|
||||
{
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
state &= ~LAST;
|
||||
state &= ~LAST;
|
||||
}
|
||||
}
|
||||
|
||||
return (state & CHANGED) != 0;
|
||||
@ -266,13 +254,12 @@ namespace etl
|
||||
{
|
||||
debounce_base::add(sample);
|
||||
|
||||
if (sample)
|
||||
if (count < HOLD_THRESHOLD)
|
||||
{
|
||||
// Set sample.
|
||||
if (count < HOLD_THRESHOLD)
|
||||
{
|
||||
++count;
|
||||
++count;
|
||||
|
||||
if (sample)
|
||||
{
|
||||
if (count == VALID_THRESHOLD)
|
||||
{
|
||||
// Set.
|
||||
@ -285,32 +272,22 @@ namespace etl
|
||||
state |= CHANGED;
|
||||
state |= HELD;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear sample.
|
||||
if (count > -VALID_THRESHOLD)
|
||||
{
|
||||
--count;
|
||||
|
||||
if (count == -VALID_THRESHOLD)
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (count == VALID_THRESHOLD)
|
||||
{
|
||||
// Clear.
|
||||
state |= CHANGED;
|
||||
state &= ~CURRENT;
|
||||
state &= ~HELD;
|
||||
state &= ~REPEATING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sample)
|
||||
{
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
state &= ~LAST;
|
||||
state &= ~LAST;
|
||||
}
|
||||
}
|
||||
|
||||
return (state & CHANGED) != 0;
|
||||
@ -352,44 +329,34 @@ namespace etl
|
||||
{
|
||||
debounce_base::add(sample);
|
||||
|
||||
if (sample)
|
||||
if (count < VALID_THRESHOLD)
|
||||
{
|
||||
// Set sample.
|
||||
if (count < VALID_THRESHOLD)
|
||||
{
|
||||
++count;
|
||||
++count;
|
||||
|
||||
if (sample)
|
||||
{
|
||||
if (count == VALID_THRESHOLD)
|
||||
{
|
||||
// Set.
|
||||
state |= CHANGED;
|
||||
state |= CURRENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear sample.
|
||||
if (count > -VALID_THRESHOLD)
|
||||
{
|
||||
--count;
|
||||
|
||||
if (count == -VALID_THRESHOLD)
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (count == VALID_THRESHOLD)
|
||||
{
|
||||
// Clear.
|
||||
state |= CHANGED;
|
||||
state &= ~CURRENT;
|
||||
state &= ~HELD;
|
||||
state &= ~REPEATING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sample)
|
||||
{
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
state &= ~LAST;
|
||||
state &= ~LAST;
|
||||
}
|
||||
}
|
||||
|
||||
return (state & CHANGED) != 0;
|
||||
@ -453,33 +420,35 @@ namespace etl
|
||||
{
|
||||
debounce_base::add(sample);
|
||||
|
||||
if (sample)
|
||||
if (count < repeat_threshold)
|
||||
{
|
||||
// Set sample.
|
||||
if (count < repeat_threshold)
|
||||
{
|
||||
++count;
|
||||
++count;
|
||||
|
||||
if (sample)
|
||||
{
|
||||
if (count == valid_threshold)
|
||||
{
|
||||
// Set.
|
||||
state |= CHANGED;
|
||||
state |= CURRENT;
|
||||
if (sample)
|
||||
{
|
||||
// Set.
|
||||
state |= CHANGED;
|
||||
state |= CURRENT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (hold_threshold != valid_threshold)
|
||||
{
|
||||
if (count == hold_threshold)
|
||||
if ((count == hold_threshold) && sample)
|
||||
{
|
||||
// Held.
|
||||
state |= CHANGED;
|
||||
state |= HELD;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (repeat_threshold != hold_threshold)
|
||||
{
|
||||
if (count == repeat_threshold)
|
||||
if ((count == repeat_threshold) && sample)
|
||||
{
|
||||
// Repeat.
|
||||
state |= CHANGED;
|
||||
@ -487,16 +456,12 @@ namespace etl
|
||||
count = hold_threshold;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Clear sample.
|
||||
if (count > -valid_threshold)
|
||||
{
|
||||
--count;
|
||||
|
||||
if (count == -valid_threshold)
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (count == valid_threshold)
|
||||
{
|
||||
// Clear.
|
||||
state |= CHANGED;
|
||||
@ -504,16 +469,9 @@ namespace etl
|
||||
state &= ~HELD;
|
||||
state &= ~REPEATING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sample)
|
||||
{
|
||||
state |= LAST;
|
||||
}
|
||||
else
|
||||
{
|
||||
state &= ~LAST;
|
||||
state &= ~LAST;
|
||||
}
|
||||
}
|
||||
|
||||
return (state & CHANGED) != 0;
|
||||
|
||||
67
src/hash.h
67
src/hash.h
@ -36,6 +36,7 @@ SOFTWARE.
|
||||
// The default hash calculation.
|
||||
#include "fnv_1.h"
|
||||
#include "type_traits.h"
|
||||
#include "static_assert.h"
|
||||
|
||||
///\defgroup hash Standard hash calculations
|
||||
///\ingroup maths
|
||||
@ -75,9 +76,11 @@ namespace etl
|
||||
/// Specialisation for bool.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template <>
|
||||
template <>
|
||||
struct hash <bool>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(bool), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(bool v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -88,9 +91,11 @@ namespace etl
|
||||
/// Specialisation for char.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template <>
|
||||
template <>
|
||||
struct hash<char>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(char), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(char v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -104,6 +109,8 @@ namespace etl
|
||||
template<> struct
|
||||
hash<signed char>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(signed char), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(signed char v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -117,6 +124,8 @@ namespace etl
|
||||
template<>
|
||||
struct hash<unsigned char>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned char), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(unsigned char v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -130,6 +139,8 @@ namespace etl
|
||||
template<>
|
||||
struct hash<wchar_t>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(wchar_t), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(wchar_t v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -140,9 +151,11 @@ namespace etl
|
||||
/// Specialisation for short.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template<>
|
||||
template<>
|
||||
struct hash<short>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(short), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(short v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -156,6 +169,8 @@ namespace etl
|
||||
template<>
|
||||
struct hash<unsigned short>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned short), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(unsigned short v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -169,6 +184,8 @@ namespace etl
|
||||
template<>
|
||||
struct hash<int>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(int), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(int v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -179,9 +196,11 @@ namespace etl
|
||||
/// Specialisation for unsigned int.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template<>
|
||||
template<>
|
||||
struct hash<unsigned int>
|
||||
{
|
||||
STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned int), "size_t smaller than type");
|
||||
|
||||
size_t operator ()(unsigned int v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
@ -195,15 +214,17 @@ namespace etl
|
||||
template<>
|
||||
struct hash<long>
|
||||
{
|
||||
// If it fits into a size_t.
|
||||
template <typename T = long>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) >= sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
}
|
||||
|
||||
// If it doesn't fit into a size_t.
|
||||
template <typename T = long>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) < sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
|
||||
@ -218,15 +239,17 @@ namespace etl
|
||||
template<>
|
||||
struct hash<long long>
|
||||
{
|
||||
// If it fits into a size_t.
|
||||
template <typename T = long long>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) >= sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
}
|
||||
|
||||
// If it doesn't fit into a size_t.
|
||||
template <typename T = long long>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) < sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
|
||||
@ -241,15 +264,17 @@ namespace etl
|
||||
template<>
|
||||
struct hash<unsigned long>
|
||||
{
|
||||
// If it fits into a size_t.
|
||||
template <typename T = unsigned long>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) >= sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
}
|
||||
|
||||
// If it doesn't fit into a size_t.
|
||||
template <typename T = unsigned long>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) < sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
|
||||
@ -264,15 +289,17 @@ namespace etl
|
||||
template<>
|
||||
struct hash<unsigned long long>
|
||||
{
|
||||
// If it fits into a size_t.
|
||||
template <typename T = unsigned long long>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) >= sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
return static_cast<size_t>(v);
|
||||
}
|
||||
|
||||
// If it doesn't fit into a size_t.
|
||||
template <typename T = unsigned long long>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
typename etl::enable_if<sizeof(size_t) < sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
{
|
||||
uint8_t* p = reinterpret_cast<uint8_t*>(&v);
|
||||
@ -287,6 +314,7 @@ namespace etl
|
||||
template<>
|
||||
struct hash<float>
|
||||
{
|
||||
// If it's the same size as a size_t.
|
||||
template <typename T = float>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
@ -294,6 +322,7 @@ namespace etl
|
||||
return *reinterpret_cast<size_t*>(&v);
|
||||
}
|
||||
|
||||
// If it's not the same size as a size_t.
|
||||
template <typename T = float>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
@ -307,9 +336,10 @@ namespace etl
|
||||
/// Specialisation for double.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template<>
|
||||
template<>
|
||||
struct hash<double>
|
||||
{
|
||||
// If it's the same size as a size_t.
|
||||
template <typename T = double>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
@ -317,6 +347,7 @@ namespace etl
|
||||
return *reinterpret_cast<size_t*>(&v);
|
||||
}
|
||||
|
||||
// If it's not the same size as a size_t.
|
||||
template <typename T = double>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
@ -330,9 +361,10 @@ namespace etl
|
||||
/// Specialisation for long double.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template<>
|
||||
template<>
|
||||
struct hash<long double>
|
||||
{
|
||||
// If it's the same size as a size_t.
|
||||
template <typename T = long double>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
@ -340,6 +372,7 @@ namespace etl
|
||||
return *reinterpret_cast<size_t*>(&v);
|
||||
}
|
||||
|
||||
// If it's not the same size as a size_t.
|
||||
template <typename T = long double>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(T), size_t>::type
|
||||
operator ()(T v) const
|
||||
@ -353,9 +386,10 @@ namespace etl
|
||||
/// Specialisation for pointers.
|
||||
///\ingroup hash
|
||||
//***************************************************************************
|
||||
template <typename T>
|
||||
template <typename T>
|
||||
struct hash<T*>
|
||||
{
|
||||
// If it's the same size as a size_t.
|
||||
template <typename U = T>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(U*), size_t>::type
|
||||
operator ()(U* v) const
|
||||
@ -363,6 +397,7 @@ namespace etl
|
||||
return reinterpret_cast<size_t>(v);
|
||||
}
|
||||
|
||||
// If it's the same size as a size_t.
|
||||
template <typename U = T>
|
||||
typename etl::enable_if<sizeof(size_t) == sizeof(U*), size_t>::type
|
||||
operator ()(const U* v) const
|
||||
@ -370,6 +405,7 @@ namespace etl
|
||||
return reinterpret_cast<size_t>(v);
|
||||
}
|
||||
|
||||
// If it's not the same size as a size_t.
|
||||
template <typename U = T>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(U*), size_t>::type
|
||||
operator ()(U* v) const
|
||||
@ -378,6 +414,7 @@ namespace etl
|
||||
return __private_hash__::generic_hash<size_t>(p, p + sizeof(v));
|
||||
}
|
||||
|
||||
// If it's not the same size as a size_t.
|
||||
template <typename U = T>
|
||||
typename etl::enable_if<sizeof(size_t) != sizeof(U*), size_t>::type
|
||||
operator ()(const U* v) const
|
||||
|
||||
@ -170,6 +170,26 @@ namespace
|
||||
|
||||
CHECK_EQUAL(size_t(&i), hash);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_hash_const_pointer)
|
||||
{
|
||||
int i;
|
||||
size_t hash = etl::hash<const int*>()(&i);
|
||||
|
||||
CHECK_EQUAL(size_t(&i), hash);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_hash_const_pointer_const)
|
||||
{
|
||||
int i;
|
||||
const int * const pi = &i;
|
||||
|
||||
size_t hash = etl::hash<const int *>()(pi);
|
||||
|
||||
CHECK_EQUAL(size_t(&i), hash);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user