MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / sort_bindings

Function sort_bindings

src/cypher/cypher.c:4478–4529  ·  view source on GitHub ↗

Sort bindings by the ORDER BY key list (virtual variables), stably. */

Source from the content-addressed store, hash-verified

4476
4477/* Sort bindings by the ORDER BY key list (virtual variables), stably. */
4478static void sort_bindings(binding_t *vbindings, int count, const cbm_return_clause_t *wc) {
4479 if (count < PAIR_LEN || wc->order_key_count <= 0) {
4480 return;
4481 }
4482 size_t keys = (size_t)wc->order_key_count;
4483 size_t cells = (size_t)count * keys;
4484 with_order_ctx_t ctx = {.wc = wc,
4485 .values = cbm_calloc(CBM_MEM_CLASS_OTHER, cells * sizeof(char *)),
4486 .numbered = cbm_calloc(CBM_MEM_CLASS_OTHER, cells * sizeof(bool)),
4487 .numbers = cbm_calloc(CBM_MEM_CLASS_OTHER, cells * sizeof(double))};
4488 int *idx = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)count * sizeof(int));
4489 binding_t *sorted = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)count * sizeof(binding_t));
4490 bool ready = ctx.values && ctx.numbered && ctx.numbers && idx && sorted;
4491 for (int i = 0; ready && i < count; i++) {
4492 idx[i] = i;
4493 for (size_t k = 0; ready && k < keys; k++) {
4494 size_t cell = (size_t)i * keys + k;
4495 ctx.values[cell] = cbm_mem_strdup(CBM_MEM_CLASS_OTHER,
4496 order_key_value(&vbindings[i], wc->order_keys[k]));
4497 ready = ctx.values[cell] != NULL;
4498 char *end = NULL;
4499 ctx.numbers[cell] = ready ? strtod(ctx.values[cell], &end) : 0;
4500 ctx.numbered[cell] = ready && end != ctx.values[cell];
4501 }
4502 }
4503 if (ready) {
4504 cypher_stable_sort(idx, count, with_order_compare, &ctx);
4505 for (int i = 0; i < count; i++) {
4506 sorted[i] = vbindings[idx[i]];
4507 }
4508 memcpy(vbindings, sorted, (size_t)count * sizeof(binding_t));
4509 } else {
4510 /* No scratch memory: insertion sort in place, same comparisons. */
4511 for (int i = SKIP_ONE; i < count; i++) {
4512 for (int j = i; j > 0 && with_order_compare_bindings(wc, &vbindings[j - SKIP_ONE],
4513 &vbindings[j]) > 0;
4514 j--) {
4515 binding_t tmp = vbindings[j];
4516 vbindings[j] = vbindings[j - SKIP_ONE];
4517 vbindings[j - SKIP_ONE] = tmp;
4518 }
4519 }
4520 }
4521 for (size_t c = 0; ctx.values && c < cells; c++) {
4522 cbm_free(CBM_MEM_CLASS_OTHER, ctx.values[c]);
4523 }
4524 cbm_free(CBM_MEM_CLASS_OTHER, ctx.values);
4525 cbm_free(CBM_MEM_CLASS_OTHER, ctx.numbered);
4526 cbm_free(CBM_MEM_CLASS_OTHER, ctx.numbers);
4527 cbm_free(CBM_MEM_CLASS_OTHER, idx);
4528 cbm_free(CBM_MEM_CLASS_OTHER, sorted);
4529}
4530
4531/* Apply skip and limit to a binding array, freeing discarded entries */
4532static void bindings_skip_limit(binding_t *vbindings, int *count, int skip, int limit) {

Callers 1

with_sort_skip_limitFunction · 0.85

Calls 7

cbm_callocFunction · 0.85
cbm_allocFunction · 0.85
cbm_mem_strdupFunction · 0.85
order_key_valueFunction · 0.85
cypher_stable_sortFunction · 0.85
cbm_freeFunction · 0.85

Tested by

no test coverage detected