MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ sort_implementation()

Datum< Kakshya::RegionGroup > MayaFlux::Yantra::RegionKeySorter::sort_implementation ( const Datum< Kakshya::RegionGroup > & input)
inlineoverrideprotected

Definition at line 81 of file RegionKeySorter.hpp.

83{
84 const auto& cfg = config<RegionKeySorterConfig>();
85 Datum<Kakshya::RegionGroup> out = input;
86 const auto n = out.data.regions.size();
87
88 if (n == 0 || cfg.keys.empty()) {
89 return out;
90 }
91
92 std::vector<double> scores(n);
93 for (size_t i = 0; i < n; ++i) {
94 const auto score = weighted_sort_score(out.data.regions[i], cfg.keys);
95 if (!score) {
96 error<std::runtime_error>(Journal::Component::Yantra, Journal::Context::ComputeMatrix, std::source_location::current(), "RegionKeySorter: a region lacks a sort key feature");
97 }
98 scores[i] = *score;
99 }
100
101 std::vector<size_t> order(n);
102 if (cfg.gpu_sort_threshold > 0 && n >= cfg.gpu_sort_threshold) {
103 order = gpu_order(scores);
104 } else {
105 std::iota(order.begin(), order.end(), size_t { 0 });
106 std::ranges::stable_sort(order, [&](size_t a, size_t b) { return scores[a] < scores[b]; });
107 }
108
109 std::vector<Kakshya::Region> reordered;
110 reordered.reserve(n);
111 for (size_t idx : order) {
112 reordered.push_back(out.data.regions[idx]);
113 }
114 out.data.regions = std::move(reordered);
115 out.data.current_region_index = 0;
116 out.data.active_indices.clear();
117 return out;
118}
static std::vector< size_t > gpu_order(const std::vector< double > &scores)
Ascending order of the scores, computed exactly on the GPU.
@ ComputeMatrix
Compute operations (Yantra - algorithms, matrices, DSP).
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
std::optional< double > weighted_sort_score(const Element &element, std::span< const SortKey > keys)
Weighted score of one element across the sort keys.

References MayaFlux::Journal::ComputeMatrix, MayaFlux::Yantra::ComputeOperation< InputType, OutputType >::config(), MayaFlux::Yantra::Datum< T >::data, gpu_order(), MayaFlux::Yantra::weighted_sort_score(), and MayaFlux::Journal::Yantra.

Here is the call graph for this function: