mirror of
https://github.com/nunuhara/xsystem4.git
synced 2026-09-22 23:18:01 +03:00
+41
-6
@@ -514,15 +514,33 @@ struct page *array_insert(struct page *page, int i, union vm_value v, enum ain_d
|
||||
return page;
|
||||
}
|
||||
|
||||
static int array_compare_int(const void *_a, const void *_b)
|
||||
{
|
||||
union vm_value a = *((union vm_value*)_a);
|
||||
union vm_value b = *((union vm_value*)_b);
|
||||
return (a.i > b.i) - (a.i < b.i);
|
||||
}
|
||||
|
||||
static int array_compare_float(const void *_a, const void *_b)
|
||||
{
|
||||
union vm_value a = *((union vm_value*)_a);
|
||||
union vm_value b = *((union vm_value*)_b);
|
||||
return (a.f > b.f) - (a.f < b.f);
|
||||
}
|
||||
|
||||
static int array_compare_string(const void *_a, const void *_b)
|
||||
{
|
||||
union vm_value a = *((union vm_value*)_a);
|
||||
union vm_value b = *((union vm_value*)_b);
|
||||
return strcmp(heap_get_string(a.i)->text, heap_get_string(b.i)->text);
|
||||
}
|
||||
|
||||
static int current_sort_function;
|
||||
|
||||
static int array_compare(const void *_a, const void *_b)
|
||||
static int array_compare_custom(const void *_a, const void *_b)
|
||||
{
|
||||
union vm_value a = *((union vm_value*)_a);
|
||||
union vm_value b = *((union vm_value*)_b);
|
||||
if (!current_sort_function) {
|
||||
return a.i - b.i;
|
||||
}
|
||||
stack_push(a);
|
||||
stack_push(b);
|
||||
vm_call(current_sort_function, -1);
|
||||
@@ -534,8 +552,25 @@ void array_sort(struct page *page, int compare_fno)
|
||||
if (!page)
|
||||
return;
|
||||
|
||||
current_sort_function = compare_fno;
|
||||
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare);
|
||||
if (compare_fno) {
|
||||
current_sort_function = compare_fno;
|
||||
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare_custom);
|
||||
} else {
|
||||
switch (page->a_type) {
|
||||
case AIN_ARRAY_INT:
|
||||
case AIN_ARRAY_LONG_INT:
|
||||
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare_int);
|
||||
break;
|
||||
case AIN_ARRAY_FLOAT:
|
||||
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare_float);
|
||||
break;
|
||||
case AIN_ARRAY_STRING:
|
||||
qsort(page->values, page->nr_vars, sizeof(union vm_value), array_compare_string);
|
||||
break;
|
||||
default:
|
||||
VM_ERROR("A_SORT(&NULL) called on ain_data_type %d", page->a_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int current_sort_member;
|
||||
|
||||
+47
-2
@@ -171,12 +171,54 @@ void test_array_insert(void)
|
||||
test_bool("array.Insert()", ar.Numof() == 3 && ar[0] == 0 && ar[1] == 1 && ar[2] == 2, true);
|
||||
}
|
||||
|
||||
void test_array_sort_int(void)
|
||||
{
|
||||
int i;
|
||||
bool failed = false;
|
||||
array@int ar[2];
|
||||
ar[0] = 0x7fffffff;
|
||||
ar[1] = -0x80000000;
|
||||
ar.Sort();
|
||||
failed = (ar[0] != -0x80000000 || ar[1] != 0x7fffffff);
|
||||
test_bool("array@int.Sort()", !failed, true);
|
||||
}
|
||||
|
||||
void test_array_sort_float(void)
|
||||
{
|
||||
int i;
|
||||
bool failed = false;
|
||||
array@float ar[8];
|
||||
for (i = 0; i < 8; i++) {
|
||||
ar[i] = (7 - i) * 0.1;
|
||||
}
|
||||
ar.Sort();
|
||||
for (i = 0; i < 8 && !failed; i++) {
|
||||
failed = ar[i] != i * 0.1;
|
||||
}
|
||||
test_bool("array@float.Sort()", !failed, true);
|
||||
}
|
||||
|
||||
void test_array_sort_string(void)
|
||||
{
|
||||
int i;
|
||||
bool failed = false;
|
||||
array@string ar[8];
|
||||
for (i = 0; i < 8; i++) {
|
||||
ar[i] = string(7 - i);
|
||||
}
|
||||
ar.Sort();
|
||||
for (i = 0; i < 8 && !failed; i++) {
|
||||
failed = ar[i] != string(i);
|
||||
}
|
||||
test_bool("array@string.Sort()", !failed, true);
|
||||
}
|
||||
|
||||
int compare_int(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
|
||||
void test_array_sort(void)
|
||||
void test_array_sort_custom(void)
|
||||
{
|
||||
int i;
|
||||
bool failed = false;
|
||||
@@ -239,7 +281,10 @@ void test_arrays(void)
|
||||
test_array_empty();
|
||||
test_array_erase();
|
||||
test_array_insert();
|
||||
test_array_sort();
|
||||
test_array_sort_int();
|
||||
test_array_sort_float();
|
||||
test_array_sort_string();
|
||||
test_array_sort_custom();
|
||||
test_array_constructors();
|
||||
test_array_push_struct_with_ref();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user