| 1 | /* $Header$ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmsort.h - T3 VM quicksort implementation |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 05/14/00 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMSORT_H |
| 21 | #define VMSORT_H |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include "t3std.h" |
| 25 | #include "vmglob.h" |
| 26 | #include "vmtype.h" |
| 27 | #include "vmrun.h" |
| 28 | |
| 29 | |
| 30 | /* ------------------------------------------------------------------------ */ |
| 31 | /* |
| 32 | * Quicksort data interface |
| 33 | */ |
| 34 | class CVmQSortData |
| 35 | { |
| 36 | public: |
| 37 | /* |
| 38 | * sort a range; to sort the entire array, provide the indices of |
| 39 | * the first and last elements of the array, inclusive |
| 40 | */ |
| 41 | void sort(VMG_ size_t l, size_t r); |
| 42 | |
| 43 | /* |
| 44 | * compare two elements by index - returns -1 if the first element |
| 45 | * is less than the second, 0 if they're equal, 1 if the first is |
| 46 | * greater than the second |
| 47 | */ |
| 48 | virtual int compare(VMG_ size_t idx_a, size_t idx_b) = 0; |
| 49 | |
| 50 | /* exchange two elements */ |
| 51 | virtual void exchange(VMG_ size_t idx_a, size_t idx_b) = 0; |
| 52 | }; |
| 53 | |
| 54 | /* ------------------------------------------------------------------------ */ |
| 55 | /* |
| 56 | * Sorter implementation for sets of vm_val_t data |
| 57 | */ |
| 58 | class CVmQSortVal: public CVmQSortData |
| 59 | { |
| 60 | public: |
| 61 | CVmQSortVal() |
| 62 | { |
| 63 | compare_fn_.set_nil(); |
| 64 | descending_ = FALSE; |
| 65 | } |
| 66 | |
| 67 | /* get/set an element */ |
| 68 | virtual void get_ele(VMG_ size_t idx, vm_val_t *val) = 0; |
| 69 | virtual void set_ele(VMG_ size_t idx, const vm_val_t *val) = 0; |
| 70 | |
| 71 | /* compare */ |
| 72 | virtual int compare(VMG_ size_t idx_a, size_t idx_b); |
| 73 | |
| 74 | /* exchange */ |
| 75 | virtual void exchange(VMG_ size_t idx_a, size_t idx_b); |
| 76 | |
| 77 | /* |
| 78 | * comparison function - if this is nil, we'll compare the values as |
| 79 | * ordinary values |
| 80 | */ |
| 81 | vm_val_t compare_fn_; |
| 82 | |
| 83 | /* flag: sort descending */ |
| 84 | int descending_; |
| 85 | |
| 86 | /* recursive native caller context */ |
| 87 | vm_rcdesc rc; |
| 88 | }; |
| 89 | |
| 90 | #endif /* VMSORT_H */ |