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 : Thu Dec 20 22:49:46 2018 |
---|
13 | // Update Count : 108 |
---|
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 int size = 10; |
---|
24 | int iarr[size]; |
---|
25 | |
---|
26 | sout | nlOff; // turn off auto newline |
---|
27 | |
---|
28 | for ( i; 0 ~ size ) { |
---|
29 | iarr[i] = size - i; |
---|
30 | sout | iarr[i] | ", "; |
---|
31 | } // for |
---|
32 | sout | nl | nl; |
---|
33 | |
---|
34 | // ascending sort/search by changing < to > |
---|
35 | qsort( iarr, size ); |
---|
36 | for ( i; 0 ~ size ) { |
---|
37 | sout | iarr[i] | ", "; |
---|
38 | } // for |
---|
39 | sout | nl; |
---|
40 | for ( i; 0 ~ 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; 0 ~ size ) { |
---|
48 | int * v = bsearch( size - i, iarr, size ); |
---|
49 | sout | size - i | ':' | *v | ", "; |
---|
50 | } // for |
---|
51 | sout | nl; |
---|
52 | for ( i; 0 ~ size ) { |
---|
53 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
54 | sout | size - i | ':' | iarr[posn] | ", "; |
---|
55 | } // for |
---|
56 | sout | nl | nl; |
---|
57 | |
---|
58 | // descending sort/search by changing < to > |
---|
59 | for ( i; 0 ~ size ) { |
---|
60 | iarr[i] = i + 1; |
---|
61 | sout | iarr[i] | ", "; |
---|
62 | } // for |
---|
63 | sout | nl; |
---|
64 | { |
---|
65 | // redefinition of ?<? can't overlap the loop controls: |
---|
66 | { |
---|
67 | int (*?<?)(int, int) = ?>?; |
---|
68 | qsort( iarr, size ); |
---|
69 | } |
---|
70 | for ( i; 0 ~ size ) { |
---|
71 | sout | iarr[i] | ", "; |
---|
72 | } // for |
---|
73 | sout | nl; |
---|
74 | for ( i; 0 ~ size ) { |
---|
75 | int (*?<?)(int, int) = ?>?; |
---|
76 | int * v = bsearch( size - i, iarr, size ); |
---|
77 | sout | size - i | ':' | *v | ", "; |
---|
78 | } // for |
---|
79 | sout | nl; |
---|
80 | for ( i; 0 ~ size ) { |
---|
81 | int (*?<?)(int, int) = ?>?; |
---|
82 | unsigned int posn = bsearch( size - i, iarr, size ); |
---|
83 | sout | size - i | ':' | iarr[posn] | ", "; |
---|
84 | } // for |
---|
85 | } |
---|
86 | sout | nl | nl; |
---|
87 | |
---|
88 | double darr[size]; |
---|
89 | for ( i; 0 ~ size ) { |
---|
90 | darr[i] = size - i + 0.5; |
---|
91 | sout | darr[i] | ", "; |
---|
92 | } // for |
---|
93 | sout | nl; |
---|
94 | qsort( darr, size ); |
---|
95 | for ( i; 0 ~ size ) { |
---|
96 | sout | darr[i] | ", "; |
---|
97 | } // for |
---|
98 | sout | nl; |
---|
99 | for ( i; 0 ~ size ) { |
---|
100 | double * v = bsearch( size - i + 0.5, darr, size ); |
---|
101 | sout | size - i + 0.5 | ':' | *v | ", "; |
---|
102 | } // for |
---|
103 | sout | nl; |
---|
104 | for ( i; 0 ~ size ) { |
---|
105 | unsigned int posn = bsearch( size - i + 0.5, darr, size ); |
---|
106 | sout | size - i + 0.5 | ':' | darr[posn] | ", "; |
---|
107 | } // for |
---|
108 | sout | nl | nl; |
---|
109 | |
---|
110 | struct S { int i, j; } sarr[size]; |
---|
111 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
112 | ofstream & ?|?( ofstream & os, S v ) { return os | v.i | ' ' | v.j; } |
---|
113 | for ( i; 0 ~ size ) { |
---|
114 | sarr[i].i = size - i; |
---|
115 | sarr[i].j = size - i + 1; |
---|
116 | sout | sarr[i] | ", "; |
---|
117 | } // for |
---|
118 | sout | nl; |
---|
119 | qsort( sarr, size ); |
---|
120 | for ( i; 0 ~ size ) { |
---|
121 | sout | sarr[i] | ", "; |
---|
122 | } // for |
---|
123 | sout | nl; |
---|
124 | for ( i; 0 ~ size ) { |
---|
125 | S temp = { size - i, size - i + 1 }; |
---|
126 | S * v = bsearch( temp, sarr, size ); |
---|
127 | sout | temp | ':' | *v | ", "; |
---|
128 | } // for |
---|
129 | sout | nl; |
---|
130 | for ( i; 0 ~ size ) { |
---|
131 | S temp = { size - i, size - i + 1 }; |
---|
132 | unsigned int posn = bsearch( temp, sarr, size ); |
---|
133 | sout | temp | ':' | sarr[posn] | ", "; |
---|
134 | } // for |
---|
135 | sout | nl | nl; |
---|
136 | { |
---|
137 | int getKey( const S & s ) { return s.j; } |
---|
138 | for ( i; 0 ~ size ) { |
---|
139 | sout | sarr[i] | ", "; |
---|
140 | } // for |
---|
141 | sout | nl; |
---|
142 | for ( i; 0 ~ size ) { |
---|
143 | S * v = bsearch( size - i + 1, sarr, size ); |
---|
144 | sout | size - i + 1 | ':' | *v | ", "; |
---|
145 | } // for |
---|
146 | sout | nl; |
---|
147 | for ( i; 0 ~ size ) { |
---|
148 | unsigned int posn = bsearch( size - i + 1, sarr, size ); |
---|
149 | sout | size - i + 1 | ':' | sarr[posn] | ", "; |
---|
150 | } // for |
---|
151 | sout | nl | nl; |
---|
152 | } |
---|
153 | } // main |
---|
154 | |
---|
155 | // Local Variables: // |
---|
156 | // tab-width: 4 // |
---|
157 | // compile-command: "cfa searchsort.cfa" // |
---|
158 | // End: // |
---|