[73abe95] | 1 | // |
---|
[658f6de0] | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
[73abe95] | 6 | // |
---|
[dc8511c] | 7 | // searchsort.cfa -- |
---|
[73abe95] | 8 | // |
---|
[658f6de0] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Feb 4 18:17:50 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[200fcb3] | 12 | // Last Modified On : Wed Dec 5 08:18:42 2018 |
---|
| 13 | // Update Count : 106 |
---|
[73abe95] | 14 | // |
---|
[658f6de0] | 15 | |
---|
[73abe95] | 16 | #include <fstream.hfa> |
---|
[200fcb3] | 17 | #include <stdlib.hfa> // bsearch, qsort |
---|
[f2cdc44] | 18 | #include <stdlib.h> // C version of bsearch |
---|
| 19 | |
---|
| 20 | int comp( const void * t1, const void * t2 ) { return *(int *)t1 < *(int *)t2 ? -1 : *(int *)t2 < *(int *)t1 ? 1 : 0; } |
---|
[658f6de0] | 21 | |
---|
| 22 | int main( void ) { |
---|
| 23 | const unsigned int size = 10; |
---|
| 24 | int iarr[size]; |
---|
| 25 | |
---|
[200fcb3] | 26 | sout | nlOff; // turn off auto newline |
---|
| 27 | |
---|
[8f34661] | 28 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 29 | iarr[i] = size - i; |
---|
| 30 | sout | iarr[i] | ", "; |
---|
| 31 | } // for |
---|
[200fcb3] | 32 | sout | nl; |
---|
[f2cdc44] | 33 | |
---|
| 34 | // ascending sort/search by changing < to > |
---|
[658f6de0] | 35 | qsort( iarr, size ); |
---|
[8f34661] | 36 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 37 | sout | iarr[i] | ", "; |
---|
| 38 | } // for |
---|
[200fcb3] | 39 | sout | nl; |
---|
[8f34661] | 40 | for ( i; 0u ~ size ) { // C version |
---|
[f2cdc44] | 41 | int key = size - i; |
---|
[9c47a47] | 42 | int * v = bsearch( &key, iarr, size, sizeof( iarr[0] ), comp ); |
---|
| 43 | sout | key | ':' | *v | ", "; |
---|
[f2cdc44] | 44 | } // for |
---|
[200fcb3] | 45 | sout | nl; |
---|
[9c47a47] | 46 | |
---|
[8f34661] | 47 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 48 | int * v = bsearch( size - i, iarr, size ); |
---|
| 49 | sout | size - i | ':' | *v | ", "; |
---|
[658f6de0] | 50 | } // for |
---|
[200fcb3] | 51 | sout | nl; |
---|
[8f34661] | 52 | for ( i; 0u ~ size ) { |
---|
[707446a] | 53 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
[9c47a47] | 54 | sout | size - i | ':' | iarr[posn] | ", "; |
---|
[707446a] | 55 | } // for |
---|
[200fcb3] | 56 | sout | nl; |
---|
[658f6de0] | 57 | |
---|
[94980502] | 58 | // descending sort/search by changing < to > |
---|
[8f34661] | 59 | for ( i; 0u ~ size ) { |
---|
[94980502] | 60 | iarr[i] = i + 1; |
---|
| 61 | sout | iarr[i] | ", "; |
---|
| 62 | } // for |
---|
[200fcb3] | 63 | sout | nl; |
---|
[94980502] | 64 | { |
---|
| 65 | int ?<?( int x, int y ) { return x > y; } |
---|
| 66 | qsort( iarr, size ); |
---|
[8f34661] | 67 | for ( i; 0u ~ size ) { |
---|
[94980502] | 68 | sout | iarr[i] | ", "; |
---|
| 69 | } // for |
---|
[200fcb3] | 70 | sout | nl; |
---|
[8f34661] | 71 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 72 | int * v = bsearch( size - i, iarr, size ); |
---|
| 73 | sout | size - i | ':' | *v | ", "; |
---|
[94980502] | 74 | } // for |
---|
[200fcb3] | 75 | sout | nl; |
---|
[8f34661] | 76 | for ( i; 0u ~ size ) { |
---|
[707446a] | 77 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
[9c47a47] | 78 | sout | size - i | ':' | iarr[posn] | ", "; |
---|
[707446a] | 79 | } // for |
---|
[94980502] | 80 | } |
---|
[200fcb3] | 81 | sout | nl; |
---|
[94980502] | 82 | |
---|
[658f6de0] | 83 | double darr[size]; |
---|
[8f34661] | 84 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 85 | darr[i] = size - i + 0.5; |
---|
| 86 | sout | darr[i] | ", "; |
---|
| 87 | } // for |
---|
[200fcb3] | 88 | sout | nl; |
---|
[658f6de0] | 89 | qsort( darr, size ); |
---|
[8f34661] | 90 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 91 | sout | darr[i] | ", "; |
---|
| 92 | } // for |
---|
[200fcb3] | 93 | sout | nl; |
---|
[8f34661] | 94 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 95 | double * v = bsearch( size - i + 0.5, darr, size ); |
---|
| 96 | sout | size - i + 0.5 | ':' | *v | ", "; |
---|
[658f6de0] | 97 | } // for |
---|
[200fcb3] | 98 | sout | nl; |
---|
[8f34661] | 99 | for ( i; 0u ~ size ) { |
---|
[707446a] | 100 | unsigned int posn = bsearch( size - i + 0.5, darr, size ); |
---|
[9c47a47] | 101 | sout | size - i + 0.5 | ':' | darr[posn] | ", "; |
---|
[707446a] | 102 | } // for |
---|
[200fcb3] | 103 | sout | nl; |
---|
[658f6de0] | 104 | |
---|
| 105 | struct S { int i, j; } sarr[size]; |
---|
| 106 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
[09687aa] | 107 | ofstream & ?|?( ofstream & os, S v ) { return os | v.i | ' ' | v.j; } |
---|
[8f34661] | 108 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 109 | sarr[i].i = size - i; |
---|
| 110 | sarr[i].j = size - i + 1; |
---|
| 111 | sout | sarr[i] | ", "; |
---|
| 112 | } // for |
---|
[200fcb3] | 113 | sout | nl; |
---|
[658f6de0] | 114 | qsort( sarr, size ); |
---|
[8f34661] | 115 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 116 | sout | sarr[i] | ", "; |
---|
| 117 | } // for |
---|
[200fcb3] | 118 | sout | nl; |
---|
[8f34661] | 119 | for ( i; 0u ~ size ) { |
---|
[658f6de0] | 120 | S temp = { size - i, size - i + 1 }; |
---|
[9c47a47] | 121 | S * v = bsearch( temp, sarr, size ); |
---|
| 122 | sout | temp | ':' | *v | ", "; |
---|
[658f6de0] | 123 | } // for |
---|
[200fcb3] | 124 | sout | nl; |
---|
[8f34661] | 125 | for ( i; 0u ~ size ) { |
---|
[707446a] | 126 | S temp = { size - i, size - i + 1 }; |
---|
| 127 | unsigned int posn = bsearch( temp, sarr, size ); |
---|
[9c47a47] | 128 | sout | temp | ':' | sarr[posn] | ", "; |
---|
[707446a] | 129 | } // for |
---|
[200fcb3] | 130 | sout | nl; |
---|
[9c47a47] | 131 | { |
---|
[93cdd5c] | 132 | unsigned int getKey( const S & s ) { return s.j; } |
---|
[8f34661] | 133 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 134 | sout | sarr[i] | ", "; |
---|
| 135 | } // for |
---|
[200fcb3] | 136 | sout | nl; |
---|
[8f34661] | 137 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 138 | S * v = bsearch( size - i + 1, sarr, size ); |
---|
| 139 | sout | size - i + 1 | ':' | *v | ", "; |
---|
| 140 | } // for |
---|
[200fcb3] | 141 | sout | nl; |
---|
[8f34661] | 142 | for ( i; 0u ~ size ) { |
---|
[9c47a47] | 143 | unsigned int posn = bsearch( size - i + 1, sarr, size ); |
---|
| 144 | sout | size - i + 1 | ':' | sarr[posn] | ", "; |
---|
| 145 | } // for |
---|
[200fcb3] | 146 | sout | nl; |
---|
[9c47a47] | 147 | } |
---|
[658f6de0] | 148 | } // main |
---|
| 149 | |
---|
| 150 | // Local Variables: // |
---|
| 151 | // tab-width: 4 // |
---|
[dc8511c] | 152 | // compile-command: "cfa searchsort.cfa" // |
---|
[658f6de0] | 153 | // End: // |
---|