| | 1 | #ifdef RCSID |
| | 2 | static char RCSid[] = |
| | 3 | "$Header$"; |
| | 4 | #endif |
| | 5 | |
| | 6 | /* |
| | 7 | * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved. |
| | 8 | * |
| | 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| | 10 | * on using and copying this software. |
| | 11 | */ |
| | 12 | /* |
| | 13 | Name |
| | 14 | vmsortv.cpp - CVmSortVal implementation |
| | 15 | Function |
| | 16 | |
| | 17 | Notes |
| | 18 | |
| | 19 | Modified |
| | 20 | 05/14/00 MJRoberts - Creation |
| | 21 | */ |
| | 22 | |
| | 23 | #include <stdlib.h> |
| | 24 | #include "t3std.h" |
| | 25 | #include "vmglob.h" |
| | 26 | #include "vmsort.h" |
| | 27 | #include "vmstack.h" |
| | 28 | #include "vmrun.h" |
| | 29 | #include "vmerr.h" |
| | 30 | #include "vmerrnum.h" |
| | 31 | |
| | 32 | |
| | 33 | /* ------------------------------------------------------------------------ */ |
| | 34 | /* |
| | 35 | * compare two vm_val_t values |
| | 36 | */ |
| | 37 | int CVmQSortVal::compare(VMG_ size_t a, size_t b) |
| | 38 | { |
| | 39 | int result; |
| | 40 | vm_val_t val_a; |
| | 41 | vm_val_t val_b; |
| | 42 | |
| | 43 | /* get the two values */ |
| | 44 | get_ele(vmg_ a, &val_a); |
| | 45 | get_ele(vmg_ b, &val_b); |
| | 46 | |
| | 47 | /* check for an explicit comparison function */ |
| | 48 | if (compare_fn_.typ != VM_NIL) |
| | 49 | { |
| | 50 | vm_val_t val; |
| | 51 | |
| | 52 | /* push the values (in reverse order) */ |
| | 53 | G_stk->push(&val_b); |
| | 54 | G_stk->push(&val_a); |
| | 55 | |
| | 56 | /* invoke the callback */ |
| | 57 | G_interpreter->call_func_ptr(vmg_ &compare_fn_, 2, "sort.compare", 0); |
| | 58 | |
| | 59 | /* get the result */ |
| | 60 | val = *G_interpreter->get_r0(); |
| | 61 | |
| | 62 | /* if it's not an integer, it's a problem */ |
| | 63 | if (val.typ != VM_INT) |
| | 64 | err_throw(VMERR_INT_VAL_REQD); |
| | 65 | |
| | 66 | /* get the result value */ |
| | 67 | result = val.val.intval; |
| | 68 | } |
| | 69 | else |
| | 70 | { |
| | 71 | /* compare the values */ |
| | 72 | result = val_a.compare_to(vmg_ &val_b); |
| | 73 | } |
| | 74 | |
| | 75 | /* if we're sorting in descending order, reverse the result */ |
| | 76 | if (descending_) |
| | 77 | result = -result; |
| | 78 | |
| | 79 | /* return the result */ |
| | 80 | return result; |
| | 81 | } |
| | 82 | |
| | 83 | /* |
| | 84 | * exchange two vm_val_t elements |
| | 85 | */ |
| | 86 | void CVmQSortVal::exchange(VMG_ size_t a, size_t b) |
| | 87 | { |
| | 88 | vm_val_t val_a; |
| | 89 | vm_val_t val_b; |
| | 90 | |
| | 91 | /* get the two elements */ |
| | 92 | get_ele(vmg_ a, &val_a); |
| | 93 | get_ele(vmg_ b, &val_b); |
| | 94 | |
| | 95 | /* store the two elements, swapping the positions */ |
| | 96 | set_ele(vmg_ b, &val_a); |
| | 97 | set_ele(vmg_ a, &val_b); |
| | 98 | } |
| | 99 | |