From 8b1dedbed6766c69796e4778e2db9d137bf9a6fd Mon Sep 17 00:00:00 2001 From: antonl Date: Tue, 24 Mar 2026 12:13:05 +0100 Subject: [PATCH] refactor out sort functions --- src/base/base_inc.c | 3 +- src/base/base_inc.h | 1 + src/base/base_sort.c | 60 ++++++++++++++++++++++++++++++++++ src/base/base_sort.h | 15 +++++++++ src/main.c | 77 ++++++-------------------------------------- 5 files changed, 88 insertions(+), 68 deletions(-) create mode 100644 src/base/base_sort.c create mode 100644 src/base/base_sort.h diff --git a/src/base/base_inc.c b/src/base/base_inc.c index 7194c58..b937b0d 100644 --- a/src/base/base_inc.c +++ b/src/base/base_inc.c @@ -1,4 +1,5 @@ #include "base_math.c" #include "base_memory.c" #include "base_strings.c" -#include "base_thread_context.c" \ No newline at end of file +#include "base_thread_context.c" +#include "base_sort.c" diff --git a/src/base/base_inc.h b/src/base/base_inc.h index 0598745..414fab3 100644 --- a/src/base/base_inc.h +++ b/src/base/base_inc.h @@ -7,5 +7,6 @@ #include "base_memory.h" #include "base_strings.h" #include "base_thread_context.h" +#include "base_sort.h" #endif //BASE_H diff --git a/src/base/base_sort.c b/src/base/base_sort.c new file mode 100644 index 0000000..33ec3b8 --- /dev/null +++ b/src/base/base_sort.c @@ -0,0 +1,60 @@ + +U64 qsort_partition_F64(SortPair_F64 *array, U64 size, U64 low, U64 high) { + F64 pivot = array[high].value; + U64 i = low - 1; + + SortPair_F64 temp = {0}; + for (U64 j = low; j < high; j++) { + if (array[j].value <= pivot) { + i += 1; + temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + + U64 final_pivot_pos = i + 1; + temp = array[final_pivot_pos]; + array[final_pivot_pos] = array[high]; + array[high] = temp; + + return final_pivot_pos; +} + +function void qsort_F64(SortPair_F64 *array, U64 size, U64 low, U64 high) { + if (low < high) { + U64 pivot_index = qsort_partition_F64(array, size, low, high); + qsort_F64(array, size, low, pivot_index - 1); + qsort_F64(array, size, pivot_index + 1, high); + } +} + +function void sort_and_get_indices_F64(F64 *array, U64 *indices, U64 size) { + SortPair_F64 *pairs = malloc(size * sizeof(SortPair_F64)); + for (U64 i = 0; i < size; i++) { + pairs[i].value = array[i]; + pairs[i].original_index = i; + } + + qsort_F64(pairs, size, 0, size - 1); + + for (U32 i = 0; i < size; i++) { + array[i] = pairs[i].value; + indices[i] = pairs[i].original_index; + } + + free(pairs); +} + +function void sort_by_indices_F64(F64 *array, U64 *indices, U64 size) { + + F64 *temp = malloc(size * sizeof(F64)); + for (U64 i = 0; i < size; i++) { + U64 original_index = indices[i]; + temp[i] = array[original_index]; + } + for (U64 i = 0; i < size; i++) { + array[i] = temp[i]; + } + free(temp); +} diff --git a/src/base/base_sort.h b/src/base/base_sort.h new file mode 100644 index 0000000..a965e3f --- /dev/null +++ b/src/base/base_sort.h @@ -0,0 +1,15 @@ +#ifndef BASE_SORT_H +#define BASE_SORT_H + +typedef struct SortPair_F64 SortPair_F64; +struct SortPair_F64 { + F64 value; + U64 original_index; +}; + +function U64 qsort_partition_F64(SortPair_F64 *array, U64 size, U64 low, U64 high); +function void qsort_F64(SortPair_F64 *array, U64 size, U64 low, U64 high); +function void sort_and_get_indices_F64(F64 *array, U64 *indices, U64 size); +function void sort_by_indices_F64(F64 *array, U64 *indices, U64 size); + +#endif //BASE_SORT_H diff --git a/src/main.c b/src/main.c index edcbbd8..8cbad1b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,4 @@ -// --- +//---- // Header includes #include "base/base_inc.h" #include "os/os_inc.h" @@ -7,7 +7,7 @@ #include "hf/file_io.h" #include "hf/hf_base.h" -// --- +//---- // .C includes #include "base/base_inc.c" #include "os/os_entry_point.c" @@ -20,6 +20,9 @@ // TODO make this a separate module that can be compiled instead // #include "hf/tests.c" +////// +//~ + typedef struct Eigensolution_F64 Eigensolution_F64; struct Eigensolution_F64 { F64 *eigenvalues_re; @@ -42,77 +45,14 @@ struct Atom { Orbital *orbitals; }; -typedef struct SortPair_F64 SortPair_F64; -struct SortPair_F64 { - F64 value; - U64 original_index; -}; - global Arena *g_base_arena = 0; global Arena *g_filename_arena = 0; #define NUM_ANGULAR_MOMENTA 3 global F64 angular_momenta[NUM_ANGULAR_MOMENTA] = {0.0, 1.0, 2.0}; global F64 *temp_wavefunction_F64; -U64 qsort_partition_F64(SortPair_F64 *array, U64 size, U64 low, U64 high) { - F64 pivot = array[high].value; - U64 i = low - 1; - - SortPair_F64 temp = {0}; - for (U64 j = low; j < high; j++) { - if (array[j].value <= pivot) { - i += 1; - temp = array[i]; - array[i] = array[j]; - array[j] = temp; - } - } - - U64 final_pivot_pos = i + 1; - temp = array[final_pivot_pos]; - array[final_pivot_pos] = array[high]; - array[high] = temp; - - return final_pivot_pos; -} - -function void qsort_F64(SortPair_F64 *array, U64 size, U64 low, U64 high) { - if (low < high) { - U64 pivot_index = qsort_partition_F64(array, size, low, high); - qsort_F64(array, size, low, pivot_index - 1); - qsort_F64(array, size, pivot_index + 1, high); - } -} - -function void sort_and_get_indices_F64(F64 *array, U64 *indices, U64 size) { - SortPair_F64 *pairs = malloc(size * sizeof(SortPair_F64)); - for (U64 i = 0; i < size; i++) { - pairs[i].value = array[i]; - pairs[i].original_index = i; - } - - qsort_F64(pairs, size, 0, size - 1); - - for (U32 i = 0; i < size; i++) { - array[i] = pairs[i].value; - indices[i] = pairs[i].original_index; - } - - free(pairs); -} - -function void sort_by_indices_F64(F64 *array, U64 *indices, U64 size) { - - F64 *temp = malloc(size * sizeof(F64)); - for (U64 i = 0; i < size; i++) { - U64 original_index = indices[i]; - temp[i] = array[original_index]; - } - for (U64 i = 0; i < size; i++) { - array[i] = temp[i]; - } - free(temp); -} +////// +//~ /* Auxiliary routine: printing a matrix */ function void print_eigenvalues(char *desc, int n, F64 *wr, F64 *wi) { @@ -267,6 +207,9 @@ function void compute_wf_norm_F64(F64 *coeffs, U64 coeff_size, U64 n, U64 l) { scratch_release(scratch); } +///////////////// +//~ +// Main entry point function void EntryPoint(void) { OS_InitReceipt os_receipt = OS_init();