MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Sort.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @file Sort.hpp
7 * @brief Discrete sequence sorting primitives for MayaFlux::Kinesis
8 *
9 * Span-level sorting functions and algorithm dispatch for contiguous
10 * double-precision sequences. No MayaFlux type dependencies.
11 *
12 * SortingDirection and SortingAlgorithm are defined here and are the
13 * canonical definitions; MayaFlux::Yantra re-exports them.
14 *
15 * Unimplemented algorithm variants (RADIX, COUNTING, BUCKET,
16 * MERGE_EXTERNAL, QUICK_OPTIMIZED, LAZY_STREAMING, PREDICTIVE_ML,
17 * GPU_ACCELERATED) fall back to STANDARD and are marked as such.
18 */
19
21
22// ============================================================================
23// Enumerations
24// ============================================================================
25
26/**
27 * @enum SortingDirection
28 * @brief Ascending or descending sort order
29 */
30enum class SortingDirection : uint8_t {
33 CUSTOM, ///< Use custom comparator function
34 BIDIRECTIONAL ///< Sort with both directions (for special algorithms)
35};
36
37/**
38 * @enum SortingAlgorithm
39 * @brief Available sorting algorithm backends
40 */
41enum class SortingAlgorithm : uint8_t {
42 STANDARD, ///< std::ranges::sort
43 STABLE, ///< std::ranges::stable_sort — preserves equal-element order
44 PARTIAL, ///< std::partial_sort — sorts first half by default
45 NTH_ELEMENT, ///< std::nth_element — partitions at midpoint
46 HEAP, ///< Heap sort via make_heap / sort_heap
47 PARALLEL, ///< MayaFlux::Parallel::sort with par_unseq
48 RADIX, ///< Not yet implemented, falls back to STANDARD
49 COUNTING, ///< Not yet implemented, falls back to STANDARD
50 BUCKET, ///< Not yet implemented, falls back to STANDARD
51 MERGE_EXTERNAL, ///< Not yet implemented, falls back to STANDARD
52 QUICK_OPTIMIZED, ///< Not yet implemented, falls back to STANDARD
53 LAZY_STREAMING, ///< Reserved for Vruta/Kriya coroutine integration
54 PREDICTIVE_ML, ///< Reserved for ML-guided sort
55 EIGEN_OPTIMIZED, ///< Reserved for Eigen-specific use
56 GPU_ACCELERATED ///< Reserved for Vulkan compute integration
57};
58
59// ============================================================================
60// Comparator factories
61// ============================================================================
62
63/**
64 * @brief Direction-based comparator for doubles
65 * @param direction Sort direction
66 * @return Binary predicate suitable for std algorithms
67 */
68[[nodiscard]] inline auto double_comparator(SortingDirection direction) noexcept
69{
70 return [direction](double a, double b) noexcept -> bool {
71 return direction == SortingDirection::ASCENDING ? a < b : a > b;
72 };
73}
74
75/**
76 * @brief Magnitude-based comparator for complex-like types
77 * @tparam T Type with std::abs support
78 * @param direction Sort direction
79 * @return Binary predicate comparing by magnitude
80 */
81template <typename T>
82 requires requires(T v) { { std::abs(v) } -> std::convertible_to<double>; }
83[[nodiscard]] auto magnitude_comparator(SortingDirection direction) noexcept
84{
85 return [direction](const T& a, const T& b) noexcept -> bool {
86 return direction == SortingDirection::ASCENDING
87 ? std::abs(a) < std::abs(b)
88 : std::abs(a) > std::abs(b);
89 };
90}
91
92/**
93 * @brief Generic sort-index generator for any random-access container
94 * @tparam Container Container type supporting size() and operator[]
95 * @tparam Comparator Binary predicate
96 * @param container Source container
97 * @param comp Comparator
98 * @return Indices that would sort container according to comp
99 */
100template <typename Container, typename Comparator>
101[[nodiscard]] std::vector<size_t> sort_indices(const Container& container, Comparator comp)
102{
103 std::vector<size_t> idx(container.size());
104 std::iota(idx.begin(), idx.end(), 0);
105 std::ranges::sort(idx, [&](size_t a, size_t b) {
106 return comp(container[a], container[b]);
107 });
108 return idx;
109}
110
111// ============================================================================
112// Algorithm dispatch
113// ============================================================================
114
115/**
116 * @brief Execute a sorting algorithm on an iterator range
117 *
118 * Unimplemented algorithm variants fall back to STANDARD.
119 *
120 * @tparam Iterator Random access iterator
121 * @tparam Comparator Binary predicate
122 * @param begin Start of range
123 * @param end End of range
124 * @param comp Comparator
125 * @param algorithm Algorithm to use
126 */
127template <std::random_access_iterator Iterator, typename Comparator>
128void execute(Iterator begin, Iterator end, Comparator comp, SortingAlgorithm algorithm)
129{
130 switch (algorithm) {
132 std::ranges::stable_sort(begin, end, comp);
133 return;
134
136 const auto dist = std::distance(begin, end);
137 if (dist > 1)
138 std::partial_sort(begin, begin + dist / 2, end, comp);
139 return;
140 }
141
143 const auto dist = std::distance(begin, end);
144 if (dist > 1)
145 std::nth_element(begin, begin + dist / 2, end, comp);
146 return;
147 }
148
150 std::make_heap(begin, end, comp);
151 std::sort_heap(begin, end, comp);
152 return;
153
155 MayaFlux::Parallel::sort(MayaFlux::Parallel::par_unseq, begin, end, comp);
156 return;
157
159 default:
160 std::ranges::sort(begin, end, comp);
161 return;
162 }
163}
164
165// ============================================================================
166// Span-level operations
167// ============================================================================
168
169/**
170 * @brief Sort a single span in-place
171 * @param data Mutable span
172 * @param direction Sort direction
173 * @param algorithm Algorithm to use
174 */
175void sort_span(
176 std::span<double> data,
177 SortingDirection direction,
179
180/**
181 * @brief Sort a span into a caller-owned output buffer
182 * @param data Immutable source span
183 * @param output_storage Destination vector (resized to match data)
184 * @param direction Sort direction
185 * @param algorithm Algorithm to use
186 * @return Span view into output_storage
187 */
188[[nodiscard]] std::span<double> sort_span_into(
189 std::span<const double> data,
190 std::vector<double>& output_storage,
191 SortingDirection direction,
193
194/**
195 * @brief Sort all channels in-place
196 * @param channels Vector of mutable spans, one per channel
197 * @param direction Sort direction
198 * @param algorithm Algorithm to use
199 */
200void sort_channels(
201 std::vector<std::span<double>>& channels,
202 SortingDirection direction,
204
205/**
206 * @brief Sort all channels into caller-owned output buffers
207 * @param channels Immutable source spans
208 * @param output_storage One output vector per channel (resized internally)
209 * @param direction Sort direction
210 * @param algorithm Algorithm to use
211 * @return Spans into each output_storage entry
212 */
213[[nodiscard]] std::vector<std::span<double>> sort_channels_into(
214 const std::vector<std::span<const double>>& channels,
215 std::vector<std::vector<double>>& output_storage,
216 SortingDirection direction,
218
219// ============================================================================
220// Index generation
221// ============================================================================
222
223/**
224 * @brief Indices that would sort a span in the given direction
225 * @param data Source span (not modified)
226 * @param direction Sort direction
227 * @return Sorted indices into data
228 */
229[[nodiscard]] std::vector<size_t> span_sort_indices(
230 std::span<double> data,
231 SortingDirection direction);
232
233/**
234 * @brief Per-channel sort indices
235 * @param channels Immutable source spans
236 * @param direction Sort direction
237 * @return One index vector per channel
238 */
239[[nodiscard]] std::vector<std::vector<size_t>> channels_sort_indices(
240 const std::vector<std::span<double>>& channels,
241 SortingDirection direction);
242
243} // namespace MayaFlux::Kinesis::Discrete
size_t a
size_t b
std::vector< size_t > span_sort_indices(std::span< double > data, SortingDirection direction)
Indices that would sort a span in the given direction.
Definition Sort.cpp:35
void sort_channels(std::vector< std::span< double > > &channels, SortingDirection direction, SortingAlgorithm algorithm)
Sort all channels in-place.
Definition Sort.cpp:19
void sort_span(std::span< double > data, SortingDirection direction, SortingAlgorithm algorithm)
Sort a single span in-place.
Definition Sort.cpp:5
std::span< double > sort_span_into(std::span< const double > data, std::vector< double > &output_storage, SortingDirection direction, SortingAlgorithm algorithm)
Sort a span into a caller-owned output buffer.
Definition Sort.cpp:12
auto magnitude_comparator(SortingDirection direction) noexcept
Magnitude-based comparator for complex-like types.
Definition Sort.hpp:83
std::vector< std::vector< size_t > > channels_sort_indices(const std::vector< std::span< double > > &channels, SortingDirection direction)
Per-channel sort indices.
Definition Sort.cpp:46
auto double_comparator(SortingDirection direction) noexcept
Direction-based comparator for doubles.
Definition Sort.hpp:68
void execute(Iterator begin, Iterator end, Comparator comp, SortingAlgorithm algorithm)
Execute a sorting algorithm on an iterator range.
Definition Sort.hpp:128
SortingAlgorithm
Available sorting algorithm backends.
Definition Sort.hpp:41
@ QUICK_OPTIMIZED
Not yet implemented, falls back to STANDARD.
@ STABLE
std::ranges::stable_sort — preserves equal-element order
@ GPU_ACCELERATED
Reserved for Vulkan compute integration.
@ EIGEN_OPTIMIZED
Reserved for Eigen-specific use.
@ RADIX
Not yet implemented, falls back to STANDARD.
@ COUNTING
Not yet implemented, falls back to STANDARD.
@ LAZY_STREAMING
Reserved for Vruta/Kriya coroutine integration.
@ HEAP
Heap sort via make_heap / sort_heap.
@ MERGE_EXTERNAL
Not yet implemented, falls back to STANDARD.
@ NTH_ELEMENT
std::nth_element — partitions at midpoint
@ PREDICTIVE_ML
Reserved for ML-guided sort.
@ PARALLEL
MayaFlux::Parallel::sort with par_unseq.
@ BUCKET
Not yet implemented, falls back to STANDARD.
@ PARTIAL
std::partial_sort — sorts first half by default
std::vector< size_t > sort_indices(const Container &container, Comparator comp)
Generic sort-index generator for any random-access container.
Definition Sort.hpp:101
std::vector< std::span< double > > sort_channels_into(const std::vector< std::span< const double > > &channels, std::vector< std::vector< double > > &output_storage, SortingDirection direction, SortingAlgorithm algorithm)
Sort all channels into caller-owned output buffers.
Definition Sort.cpp:25
SortingDirection
Ascending or descending sort order.
Definition Sort.hpp:30
@ BIDIRECTIONAL
Sort with both directions (for special algorithms)
@ CUSTOM
Use custom comparator function.