static uint64_t FNVhash64(uint64_t val) {
const uint64_t FNV_offset_basis_64 = 0xCBF29CE484222325L;
const uint64_t FNV_prime_64 = 1099511628211L;
uint64_t hashval = FNV_offset_basis_64;
for (int i = 0; i < 8; i++) {
hashval = hashval ^ (val & 0xff);
hashval = hashval * FNV_prime_64;
val >>= 8;
}
return (hashval & 0x8000000000000000) ? -hashval : hashval;
}
Showing posts with label cpp_algorithm. Show all posts
Showing posts with label cpp_algorithm. Show all posts
Feb 12, 2025
[Algorithm] Fowler–Noll–Vo (FNV) hash
Jul 31, 2024
Jun 13, 2022
[C++][CPPCON] s.t. about type kinds
Reference:
https://www.youtube.com/watch?v=va9I2qivBOA
std::variant
all_of
any_of
mismatch
equal
merge
set_union
set_intersection
Packs are a distinct kind
All types belong to a kind
... or 14(types), depending how you count
e.g.
nullptr
template names belong to another kind
There is a one-of-a-kind construct
Adding new kinds is almost unprecedented.
Packs are unlike everything else in C++
pointer to member function,
i.e. ->* .* return has no type.
Hybrid algorithm, compile-time + runtime:
i.e. Linear search at compilation, binary search at runtime.
Labels:
algorithm,
cpp,
cpp_algorithm,
cpp_type,
cppcon_2021
Feb 11, 2022
[C++][c++20] std::midpoint
Reference:
https://devblogs.microsoft.com/oldnewthing/20220207-00/?p=106223
https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html
https://en.cppreference.com/w/cpp/numeric/midpoint
https://www.youtube.com/watch?v=sBtAGxBh-XI
No, don't do this:
Better if we know which value is larger:
Better:
Better:
Better:
If your compiler supports integers larger than the size of an unsigned, say because unsigned is a 32-bit value but the native register size is 64-bit, or because the compiler supports multiword arithmetic, then you can cast to the larger data type:
code snipped from talk: CppCon 2019: Marshall Clow “std::midpoint? How Hard Could it Be?”
https://devblogs.microsoft.com/oldnewthing/20220207-00/?p=106223
https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html
https://en.cppreference.com/w/cpp/numeric/midpoint
https://www.youtube.com/watch?v=sBtAGxBh-XI
Types for std::midpoint
- integral
5 of them:
signed char, short int, int, long int, long long int https://en.cppreference.com/w/cpp/language/types
signed overflow is UB; unsigned overflow is defined to wrap around.
signed converted to unsigned is always safe.
every signed integral type has a corresponding unsigned integral type and its name is std::make_unsigned_t<T> - pointer
- floating point
Denormalized numbers, INF, and NaN
"at most one inexact operation"
No, don't do this:
unsigned average(unsigned a, unsigned b)
{
return (a + b) / 2;
}
unsigned average(unsigned low, unsigned high)
{
return low + (high - low) / 2;
}
unsigned average(unsigned low, unsigned high)
{
return ((unsigned int)low + (unsigned int)high)) >> 1;
}
unsigned average(unsigned a, unsigned b)
{
return (a / 2) + (b / 2) + (a & b & 1);
}
unsigned average(unsigned a, unsigned b)
{
return (a & b) + (a ^ b) / 2;
}
unsigned average(unsigned a, unsigned b)
{
// Suppose "unsigned" is a 32-bit type and
// "unsigned long long" is a 64-bit type.
return ((unsigned long long)a + b) / 2;
}
constexpr Integer midpoint(Integer a, Integer b) noexcept {
using U = std::make_unsigned_t<Integer>;
int sign = 1;
U m = a;
U M = b;
if (a > b) { // no branch generated.
sign = -1;
m = b;
M = a;
}
return a + sign * Integer(U(M-m) >> 1); // C casting works while we are sure input is integral types.
}
template<typename T>
constexpr enable_if_t<is_pointer_v<T>, T>
midpoint_ptr(T a, T b) {
return a + midpoint(ptrdiff_t{0}, b - a); // std::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
}
template<typename T>
constexpr enable_if_t<is_floating_point_v<Fp>, Fp>
midpoint_fp(T a, T b) noexcept {
Fp lo = numeric_limits<Fp>::min()*2;
Fp hi = numeric_limits<Fp>::max()*2;
return abs(a) <= hi && abs(b) <= hi ? // typical case
(a + b) / 2: // alwas correctly rounded
abs(a) < lo ? a + b/2: // not safe to halve a
abs(b) < lo ? a/2 + b: // not safe to halve b
a/2 + b/2; // otherwise correctly rounded
}
May 13, 2018
[ACCU 2018] 105 STL Algorithms in Less Than an Hour - Jonathan Boccara
105 STL Algorithms in Less Than an Hour - Jonathan Boccara [ACCU 2018]
http://en.cppreference.com/w/cpp/algorithm
http://en.cppreference.com/w/cpp/algorithm
Why algorithms?
- off-by-one error
- empty loops
- naive complexity
STL algorithms can make code simpler
- Replace for-loops by the right algorithm
Improve code expressiveity
- Understand common vocabulary
- Write in the common vocabulary
Understand technical aspects
- complexity
- pre/post requisites
- look into the implementation
Write our own algorithms
- Combine existing algorithms together
- Enrich a family
- Start a new family
Subscribe to:
Posts (Atom)
