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