MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
OrderKey.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file OrderKey.hpp
5 * @brief Doubles as 64-bit keys whose unsigned order is their numeric order.
6 *
7 * A key is stored as two 32-bit words, low word first, which is the byte
8 * layout of a little-endian uint64_t and of a uvec2 (x low, y high). Comparing
9 * high words and then low words as unsigned integers orders doubles exactly,
10 * with no fp64 arithmetic. -0.0 and +0.0 share one key and every NaN maps to
11 * the largest key, so NaN sorts last.
12 */
13
15
16namespace detail {
17 inline constexpr uint64_t order_sign_bit = uint64_t { 1 } << 63U;
18 inline constexpr uint64_t order_nan_key = ~uint64_t { 0 };
19}
20
21/**
22 * @brief The order key of a value.
23 */
24[[nodiscard]] constexpr uint64_t order_key(double value) noexcept
25{
26 if (value != value) {
28 }
29 const auto bits = std::bit_cast<uint64_t>(value + 0.0);
30 return (bits & detail::order_sign_bit) != 0U ? ~bits : bits | detail::order_sign_bit;
31}
32
33/**
34 * @brief The value of an order key. The key of any NaN reads back as a quiet NaN.
35 */
36[[nodiscard]] constexpr double from_order_key(uint64_t key) noexcept
37{
38 if (key == detail::order_nan_key) {
39 return std::numeric_limits<double>::quiet_NaN();
40 }
41 const uint64_t bits = (key & detail::order_sign_bit) != 0U ? key & ~detail::order_sign_bit : ~key;
42 return std::bit_cast<double>(bits);
43}
44
45/**
46 * @brief Writes the order key of every value, stopping at the shorter span.
47 */
48inline void order_keys(std::span<const double> values, std::span<uint64_t> keys) noexcept
49{
50 const size_t count = std::min(values.size(), keys.size());
51 for (size_t i = 0; i < count; ++i) {
52 keys[i] = order_key(values[i]);
53 }
54}
55
56} // namespace MayaFlux::Kinesis::Discrete
constexpr uint64_t order_sign_bit
Definition OrderKey.hpp:17
constexpr uint64_t order_key(double value) noexcept
The order key of a value.
Definition OrderKey.hpp:24
void order_keys(std::span< const double > values, std::span< uint64_t > keys) noexcept
Writes the order key of every value, stopping at the shorter span.
Definition OrderKey.hpp:48
constexpr double from_order_key(uint64_t key) noexcept
The value of an order key.
Definition OrderKey.hpp:36
std::vector< double > count(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Sample count per window (as double for pipeline uniformity).
Definition Analysis.cpp:506