Add minor adjustments/clean-up

This commit is contained in:
2025-10-06 18:30:32 -05:00
parent 095ba92914
commit 3fcd09e7c0
4 changed files with 19 additions and 9 deletions

View File

@@ -8,5 +8,4 @@ void dlos_sort_heap(dlos_Heap *h) {
h->size -= 1; h->size -= 1;
dlos_sift_down(0, h); dlos_sift_down(0, h);
} }
h->size = original_size;
} }

View File

@@ -21,12 +21,6 @@ static int right_sibling(int i) {
return (i % 2 == 1) ? i + 1 : -1; return (i % 2 == 1) ? i + 1 : -1;
} }
void dlos_node_swap(dlos_ComparableNode *arr, int a, int b) {
dlos_ComparableNode temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
static int index_of_larger_child(int i, dlos_Heap *h) { static int index_of_larger_child(int i, dlos_Heap *h) {
int larger = left_child(i); int larger = left_child(i);
if (larger >= h->size) { if (larger >= h->size) {
@@ -38,6 +32,12 @@ static int index_of_larger_child(int i, dlos_Heap *h) {
return larger; return larger;
} }
void dlos_node_swap(dlos_ComparableNode *arr, int a, int b) {
dlos_ComparableNode temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void dlos_sift_down(int i, dlos_Heap *h) { void dlos_sift_down(int i, dlos_Heap *h) {
int target = index_of_larger_child(i, h); int target = index_of_larger_child(i, h);
while (target > 0 && h->compare(&h->arr[i], &h->arr[target]) > 0) { while (target > 0 && h->compare(&h->arr[i], &h->arr[target]) > 0) {

View File

@@ -7,6 +7,7 @@ typedef struct dlos_ComparableNode {
typedef struct dlos_Heap { typedef struct dlos_Heap {
int size; int size;
dlos_ComparableNode *arr; dlos_ComparableNode *arr;
int arr_size;
int (*compare)(dlos_ComparableNode *a, dlos_ComparableNode *b); int (*compare)(dlos_ComparableNode *a, dlos_ComparableNode *b);
} dlos_Heap; } dlos_Heap;
@@ -36,7 +37,12 @@ void dlos_max_heap(dlos_Heap *h);
// for (int i = 0; i < test_size; i++) { // for (int i = 0; i < test_size; i++) {
// a[i].value = &data_storage[i]; // a[i].value = &data_storage[i];
// } // }
// dlos_Heap heap = { .size = test_size, .arr = a, .compare = compare_nodes }; // dlos_Heap heap = {
// .size = test_size,
// .arr = a,
// .arr_size = test_size,
// .compare = compare_nodes
// };
// dlos_max_heap(&heap); // dlos_max_heap(&heap);
// dlos_sort_heap(&heap); // dlos_sort_heap(&heap);
// for (int i = 0; i < test_size; i++) { // for (int i = 0; i < test_size; i++) {

View File

@@ -22,7 +22,12 @@ int main(int argc, char *argv[]) {
for (int i = 0; i < test_size; i++) { for (int i = 0; i < test_size; i++) {
a[i].value = &data_storage[i]; a[i].value = &data_storage[i];
} }
dlos_Heap heap = { .size = test_size, .arr = a, .compare = compare_nodes }; dlos_Heap heap = {
.size = test_size,
.arr = a,
.arr_size = test_size,
.compare = compare_nodes
};
dlos_max_heap(&heap); dlos_max_heap(&heap);
dlos_sort_heap(&heap); dlos_sort_heap(&heap);
for (int i = 0; i < test_size; i++) { for (int i = 0; i < test_size; i++) {