refactor out sort functions
This commit is contained in:
parent
d670d018b7
commit
8b1dedbed6
@ -1,4 +1,5 @@
|
||||
#include "base_math.c"
|
||||
#include "base_memory.c"
|
||||
#include "base_strings.c"
|
||||
#include "base_thread_context.c"
|
||||
#include "base_thread_context.c"
|
||||
#include "base_sort.c"
|
||||
|
||||
@ -7,5 +7,6 @@
|
||||
#include "base_memory.h"
|
||||
#include "base_strings.h"
|
||||
#include "base_thread_context.h"
|
||||
#include "base_sort.h"
|
||||
|
||||
#endif //BASE_H
|
||||
|
||||
60
src/base/base_sort.c
Normal file
60
src/base/base_sort.c
Normal file
@ -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);
|
||||
}
|
||||
15
src/base/base_sort.h
Normal file
15
src/base/base_sort.h
Normal file
@ -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
|
||||
77
src/main.c
77
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();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user