MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / sz_sequence_argsort_with_insertion

Function sz_sequence_argsort_with_insertion

include/stringzilla/sort.h:154–179  ·  view source on GitHub ↗

* @brief Quadratic complexity @b stable insertion sort adjust for our @b argsort usecase. * Needs no extra memory and is used as a fallback for small inputs. */

Source from the content-addressed store, hash-verified

152 * Needs no extra memory and is used as a fallback for small inputs.
153 */
154SZ_PUBLIC void sz_sequence_argsort_with_insertion(sz_sequence_t const *sequence, sz_sorted_idx_t *order) {
155 // Assume `order` is already initialized with 0, 1, 2, ... N.
156 for (sz_size_t i = 1; i < sequence->count; ++i) {
157 sz_sorted_idx_t current_idx = order[i];
158 sz_size_t j = i;
159 while (j > 0) {
160 // Get the two strings to compare.
161 sz_sorted_idx_t previous_idx = order[j - 1];
162 sz_cptr_t previous_start = sequence->get_start(sequence->handle, previous_idx);
163 sz_cptr_t current_start = sequence->get_start(sequence->handle, current_idx);
164 sz_size_t previous_length = sequence->get_length(sequence->handle, previous_idx);
165 sz_size_t current_length = sequence->get_length(sequence->handle, current_idx);
166
167 // Use the provided sz_order to compare.
168 sz_ordering_t ordering = sz_order(previous_start, previous_length, current_start, current_length);
169
170 // If the previous string is not greater than current_idx, we're done.
171 if (ordering != sz_greater_k) break;
172
173 // Otherwise, shift the previous element to the right.
174 order[j] = order[j - 1];
175 --j;
176 }
177 order[j] = current_idx;
178 }
179}
180
181/**
182 * @brief Quadratic complexity @b stable insertion sort adjust for our @b pgram-sorting usecase.

Calls 1

sz_orderFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…