Implement A_SORT_MEM

Sorts an array of structs by a given member.
This commit is contained in:
Nunuhara Cabbage
2022-01-23 15:11:48 -08:00
parent 67b9923ee9
commit b7aaa1897b
3 changed files with 29 additions and 0 deletions
+1
View File
@@ -126,6 +126,7 @@ struct page *array_popback(struct page *dst);
struct page *array_erase(struct page *page, int i, bool *success);
struct page *array_insert(struct page *page, int i, union vm_value v, enum ain_data_type data_type, int struct_type);
void array_sort(struct page *page, int compare_fno);
void array_sort_mem(struct page *page, int member_no);
int array_find(struct page *page, int start, int end, union vm_value v, int compare_fno);
void array_reverse(struct page *page);
+22
View File
@@ -542,6 +542,28 @@ void array_sort(struct page *page, int compare_fno)
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare);
}
static int current_sort_member;
static int array_compare_member(const void *_a, const void *_b)
{
union vm_value a_slot = *((union vm_value*)_a);
union vm_value b_slot = *((union vm_value*)_b);
struct page *a = heap_get_page(a_slot.i);
struct page *b = heap_get_page(b_slot.i);
return a->values[current_sort_member].i - b->values[current_sort_member].i;
}
void array_sort_mem(struct page *page, int member_no)
{
if (!page)
return;
if (page->type != ARRAY_PAGE || array_type(page->a_type) != AIN_STRUCT)
VM_ERROR("A_SORT_MEM called on something other than an array of structs");
current_sort_member = member_no;
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare_member);
}
int array_find(struct page *page, int start, int end, union vm_value v, int compare_fno)
{
if (!page)
+6
View File
@@ -2042,6 +2042,12 @@ static enum opcode execute_instruction(enum opcode opcode)
stack_push(i < get_argument(1) || i >= get_argument(2));
break;
}
case A_SORT_MEM: {
int mno = stack_pop().i;
int array = stack_pop_var()->i;
array_sort_mem(heap[array].page, mno);
break;
}
//case DG_ADD:
case DG_SET: {
int fun = stack_pop().i;