29template <ComputeData InputType = Kakshya::DataVariant, ComputeData OutputType = InputType>
41 this->set_direction(SortingDirection::ASCENDING);
42 this->set_strategy(SortingStrategy::COPY_SORT);
43 this->set_granularity(SortingGranularity::RAW_DATA);
51 return SortingType::STANDARD;
66 return "StandardSorter";
74 switch (this->get_strategy()) {
75 case SortingStrategy::IN_PLACE:
76 return sort_in_place(input);
77 case SortingStrategy::COPY_SORT:
78 return sort_copy(input);
79 case SortingStrategy::INDEX_ONLY:
80 return sort_indices_only(input);
81 case SortingStrategy::PARTIAL_SORT:
82 return sort_partial(input);
83 case SortingStrategy::CHUNKED_SORT:
84 return sort_chunked(input);
85 case SortingStrategy::PARALLEL_SORT:
86 return sort_parallel(input);
88 return sort_copy(input);
97 return validate_input_type(input.data);
105 if (name ==
"algorithm") {
106 if (
auto alg_result = safe_any_cast<SortingAlgorithm>(value)) {
107 m_algorithm = *alg_result.value;
110 if (
auto str_result = safe_any_cast<std::string>(value)) {
111 auto algorithm_enum = Reflect::string_to_enum_case_insensitive<SortingAlgorithm>(*str_result.value);
112 if (algorithm_enum) {
113 m_algorithm = *algorithm_enum;
118 if (name ==
"chunk_size") {
119 if (
auto size_result = safe_any_cast<size_t>(value)) {
120 m_chunk_size = *size_result.value;
124 base_type::set_sorting_parameter(name, std::move(value));
129 if (name ==
"algorithm") {
132 if (name ==
"chunk_size") {
135 return base_type::get_sorting_parameter(name);
140 size_t m_chunk_size = 1024;
148 std::vector<std::vector<double>> working_buffer;
149 auto [working_spans, structure_info] = OperationHelper::setup_operation_buffer(
150 const_cast<input_type&
>(input), working_buffer);
152 Kinesis::Discrete::sort_channels(working_spans, this->get_direction(), m_algorithm);
154 output_type result = this->convert_result(working_buffer, structure_info);
155 result.metadata = input.metadata;
156 result.container = input.container;
157 result.metadata[
"sort_type"] =
"copy";
161 }
catch (
const std::exception& e) {
163 error_result.metadata = input.metadata;
164 error_result.container = input.container;
165 error_result.metadata[
"error"] = std::string(
"Sorting failed: ") + e.what();
175 if constexpr (std::same_as<InputType, OutputType>) {
177 sort_data_in_place(result);
180 return sort_copy(input);
189 if constexpr (std::same_as<OutputType, std::vector<size_t>>) {
192 result.metadata = input.metadata;
193 result.metadata[
"sort_type"] =
"indices_only";
197 return sort_copy(input);
207 if constexpr (std::same_as<InputType, OutputType>) {
208 result.data = sort_data_partial(input);
211 return convert_and_sort(input);
220 auto old_algorithm = m_algorithm;
221 m_algorithm = SortingAlgorithm::PARALLEL;
222 auto result = sort_copy(input);
223 m_algorithm = old_algorithm;
224 result.metadata[
"sort_type"] =
"parallel";
251 auto old_algorithm = m_algorithm;
252 m_algorithm = SortingAlgorithm::PARTIAL;
254 m_algorithm = old_algorithm;
260 std::vector<InputType> chunks;
262 for (
size_t start = 0; start < channels[0].size(); start += m_chunk_size) {
263 std::vector<std::vector<double>> chunk_data;
264 chunk_data.resize(channels.size());
266 for (
size_t ch = 0; ch < channels.size(); ++ch) {
267 size_t end = std::min(start + m_chunk_size, channels[ch].size());
268 auto chunk_span = channels[ch].subspan(start, end - start);
270 chunk_data[ch].assign(chunk_span.begin(), chunk_span.end());
271 Kinesis::Discrete::sort_span(std::span<double>(chunk_data[ch]), this->get_direction(), m_algorithm);
274 chunks.push_back(OperationHelper::reconstruct_from_double<InputType>(chunk_data, info));
286 auto [data_span, structure_info] = OperationHelper::extract_structured_double(
288 return merge_chunks_to_result(extract_chunked_data(data_span, structure_info), data, structure_info);
291 return { sort_data_copy(data) };
301 result.metadata = original_input.metadata;
302 result.metadata[
"sort_type"] =
"chunked_merged";
304 if constexpr (std::same_as<InputType, OutputType>) {
305 if (chunks.empty()) {
310 std::vector<std::vector<double>> merged_channels;
312 for (
const auto& chunk : chunks) {
313 auto chunk_channels = OperationHelper::extract_numeric_data(chunk);
315 if (merged_channels.empty()) {
316 merged_channels.resize(chunk_channels.size());
319 for (
size_t ch = 0; ch < chunk_channels.size() && ch < merged_channels.size(); ++ch) {
320 merged_channels[ch].insert(merged_channels[ch].end(),
321 chunk_channels[ch].begin(), chunk_channels[ch].end());
325 result = this->convert_result(merged_channels, info);
326 result.metadata = original_input.metadata;
327 result.metadata[
"sort_type"] =
"chunked_merged";
330 result.data = chunks[0];
342 std::vector<std::vector<double>> working_buffer;
343 auto [working_spans, structure_info] = OperationHelper::setup_operation_buffer(
344 const_cast<input_type&
>(input), working_buffer);
346 Kinesis::Discrete::sort_channels(working_spans, this->get_direction(), SortingAlgorithm::PARTIAL);
347 return this->convert_result(working_buffer, structure_info);
356 return data.has_container();
384template <ComputeData InputType = std::vector<Kakshya::DataVariant>>
Modern, digital-first universal sorting framework for Maya Flux.
void set_algorithm(SortingAlgorithm algorithm)
Configure sorting algorithm.
output_type sort_parallel(const input_type &input)
Parallel sorting.
output_type sort_indices_only(const input_type &input)
Generate sort indices only.
InputType sort_data_partial(const input_type &data)
Partial sorting implementation.
output_type sort_chunked(const input_type &data)
Chunked sorting implementation.
output_type sort_partial(const input_type &input)
Partial sorting (top-K elements)
StandardSorter()
Constructor with default configuration.
InputType sort_data_copy(const input_type &data)
Sort data with copy semantics.
output_type sort_implementation(const input_type &input) override
Main sorting implementation with type dispatch.
output_type sort_in_place(const input_type &input)
In-place sorting (modifies input)
typename base_type::input_type input_type
void set_sorting_parameter(const std::string &name, std::any value) override
Custom parameter handling.
std::any get_sorting_parameter(const std::string &name) const override
bool validate_sorting_input(const input_type &input) const override
Input validation.
output_type sort_copy(const input_type &input)
Copy-based sorting (preserves input)
SortingAlgorithm get_algorithm() const
bool validate_input_type(const input_type &data) const
Validate input type for sorting.
std::string get_sorter_name() const override
Get sorter name.
SortingType get_sorting_type() const override
Get sorting type category.
output_type merge_chunks_to_result(const std::vector< InputType > &chunks, const input_type &original_input, DataStructureInfo info)
Merge chunks back to single result.
void sort_data_in_place(input_type &data)
Sort data in-place.
output_type convert_and_sort(const input_type &input)
Handle type conversion scenarios.
std::vector< InputType > extract_chunked_data(std::vector< std::span< double > > channels, DataStructureInfo info)
typename base_type::output_type output_type
Concrete implementation for standard comparison-based sorting.
Template-flexible sorter base with instance-defined I/O types.
SortingAlgorithm
Available sorting algorithm backends.
std::vector< std::vector< size_t > > generate_compute_data_indices(const Datum< T > &data, SortingDirection direction)
Generate sort indices for any ComputeData type.
SortingType
Categories of sorting operations for discovery and organization.
void sort_compute_data_inplace(Datum< T > &data, SortingDirection direction, SortingAlgorithm algorithm)
Universal sort function - handles extraction/conversion internally.
T sort_compute_data_extract(const T &data, SortingDirection direction, SortingAlgorithm algorithm)
Universal sort function - returns sorted copy.
Metadata about data structure for reconstruction.
Input/Output container for computation pipeline data flow with structure preservation.