30template <ComputeData InputType = Kakshya::DataVariant, ComputeData OutputType = InputType>
42 this->set_direction(SortingDirection::ASCENDING);
43 this->set_strategy(SortingStrategy::COPY_SORT);
44 this->set_granularity(SortingGranularity::RAW_DATA);
52 return SortingType::STANDARD;
67 return "StandardSorter";
75 switch (this->get_strategy()) {
76 case SortingStrategy::IN_PLACE:
77 return sort_in_place(input);
78 case SortingStrategy::COPY_SORT:
79 return sort_copy(input);
80 case SortingStrategy::INDEX_ONLY:
81 return sort_indices_only(input);
82 case SortingStrategy::PARTIAL_SORT:
83 return sort_partial(input);
84 case SortingStrategy::CHUNKED_SORT:
85 return sort_chunked(input);
86 case SortingStrategy::PARALLEL_SORT:
87 return sort_parallel(input);
89 return sort_copy(input);
98 return validate_input_type(input.data);
106 if (name ==
"algorithm") {
107 if (
auto alg_result = safe_any_cast<SortingAlgorithm>(value)) {
108 m_algorithm = *alg_result.value;
111 if (
auto str_result = safe_any_cast<std::string>(value)) {
112 auto algorithm_enum = Utils::string_to_enum_case_insensitive<SortingAlgorithm>(*str_result.value);
113 if (algorithm_enum) {
114 m_algorithm = *algorithm_enum;
119 if (name ==
"chunk_size") {
120 if (
auto size_result = safe_any_cast<size_t>(value)) {
121 m_chunk_size = *size_result.value;
125 base_type::set_sorting_parameter(name, std::move(value));
130 if (name ==
"algorithm") {
133 if (name ==
"chunk_size") {
136 return base_type::get_sorting_parameter(name);
141 size_t m_chunk_size = 1024;
149 std::vector<std::vector<double>> working_buffer;
150 auto [working_spans, structure_info] = OperationHelper::setup_operation_buffer(
151 const_cast<input_type&
>(input), working_buffer);
155 output_type result = this->convert_result(working_buffer, structure_info);
156 result.metadata = input.metadata;
157 result.container = input.container;
158 result.metadata[
"sort_type"] =
"copy";
162 }
catch (
const std::exception& e) {
164 error_result.metadata = input.metadata;
165 error_result.container = input.container;
166 error_result.metadata[
"error"] = std::string(
"Sorting failed: ") + e.what();
176 if constexpr (std::same_as<InputType, OutputType>) {
178 sort_data_in_place(result);
181 return sort_copy(input);
190 if constexpr (std::same_as<OutputType, std::vector<size_t>>) {
193 result.metadata = input.metadata;
194 result.metadata[
"sort_type"] =
"indices_only";
198 return sort_copy(input);
208 if constexpr (std::same_as<InputType, OutputType>) {
209 result.data = sort_data_partial(input);
212 return convert_and_sort(input);
221 auto old_algorithm = m_algorithm;
222 m_algorithm = SortingAlgorithm::PARALLEL;
223 auto result = sort_copy(input);
224 m_algorithm = old_algorithm;
225 result.metadata[
"sort_type"] =
"parallel";
252 auto old_algorithm = m_algorithm;
253 m_algorithm = SortingAlgorithm::PARTIAL;
255 m_algorithm = old_algorithm;
261 std::vector<InputType> chunks;
263 for (
size_t start = 0; start < channels[0].size(); start += m_chunk_size) {
264 std::vector<std::vector<double>> chunk_data;
265 chunk_data.resize(channels.size());
267 for (
size_t ch = 0; ch < channels.size(); ++ch) {
268 size_t end = std::min(start + m_chunk_size, channels[ch].size());
269 auto chunk_span = channels[ch].subspan(start, end - start);
271 chunk_data[ch].assign(chunk_span.begin(), chunk_span.end());
272 sort_span_inplace(std::span<double>(chunk_data[ch]), this->get_direction(), m_algorithm);
275 chunks.push_back(OperationHelper::reconstruct_from_double<InputType>(chunk_data, info));
287 auto [data_span, structure_info] = OperationHelper::extract_structured_double(
289 return merge_chunks_to_result(extract_chunked_data(data_span, structure_info), data, structure_info);
292 return { sort_data_copy(data) };
302 result.metadata = original_input.metadata;
303 result.metadata[
"sort_type"] =
"chunked_merged";
305 if constexpr (std::same_as<InputType, OutputType>) {
306 if (chunks.empty()) {
311 std::vector<std::vector<double>> merged_channels;
313 for (
const auto& chunk : chunks) {
314 auto chunk_channels = OperationHelper::extract_numeric_data(chunk);
316 if (merged_channels.empty()) {
317 merged_channels.resize(chunk_channels.size());
320 for (
size_t ch = 0; ch < chunk_channels.size() && ch < merged_channels.size(); ++ch) {
321 merged_channels[ch].insert(merged_channels[ch].end(),
322 chunk_channels[ch].begin(), chunk_channels[ch].end());
326 result = this->convert_result(merged_channels, info);
327 result.metadata = original_input.metadata;
328 result.metadata[
"sort_type"] =
"chunked_merged";
331 result.data = chunks[0];
343 std::vector<std::vector<double>> working_buffer;
344 auto [working_spans, structure_info] = OperationHelper::setup_operation_buffer(
345 const_cast<input_type&
>(input), working_buffer);
348 return this->convert_result(working_buffer, structure_info);
357 return data.has_container();
385template <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.
void sort_channels_inplace(std::vector< std::span< double > > &channels, SortingDirection direction, SortingAlgorithm algorithm)
Sort multiple channels (spans) in-place.
std::vector< std::vector< size_t > > generate_compute_data_indices(const IO< T > &data, SortingDirection direction)
Generate sort indices for any ComputeData type.
void sort_span_inplace(std::span< double > data, SortingDirection direction, SortingAlgorithm algorithm)
Sort a single span of doubles in-place.
SortingType
Categories of sorting operations for discovery and organization.
SortingAlgorithm
Available sorting algorithms for different use cases.
T sort_compute_data_extract(const T &data, SortingDirection direction, SortingAlgorithm algorithm)
Universal sort function - returns sorted copy.
void sort_compute_data_inplace(IO< T > &data, SortingDirection direction, SortingAlgorithm algorithm)
Universal sort function - handles extraction/conversion internally.
Metadata about data structure for reconstruction.
Input/Output container for computation pipeline data flow with structure preservation.