| 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, &rc, 0); |
| 58 | |
| 59 | /* get the result */ |
| 60 | val = *G_interpreter->get_r0(); |
| 61 | |
| 62 | /* get the result value */ |
| 63 | result = val.num_to_int(vmg0_); |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | /* compare the values */ |
| 68 | result = val_a.compare_to(vmg_ &val_b); |
| 69 | } |
| 70 | |
| 71 | /* if we're sorting in descending order, reverse the result */ |
| 72 | if (descending_) |
| 73 | result = -result; |
| 74 | |
| 75 | /* return the result */ |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * exchange two vm_val_t elements |
| 81 | */ |
| 82 | void CVmQSortVal::exchange(VMG_ size_t a, size_t b) |
| 83 | { |
| 84 | vm_val_t val_a; |
| 85 | vm_val_t val_b; |
| 86 | |
| 87 | /* get the two elements */ |
| 88 | get_ele(vmg_ a, &val_a); |
| 89 | get_ele(vmg_ b, &val_b); |
| 90 | |
| 91 | /* store the two elements, swapping the positions */ |
| 92 | set_ele(vmg_ b, &val_a); |
| 93 | set_ele(vmg_ a, &val_b); |
| 94 | } |
| 95 |