Changeset 9c47a47
 Timestamp:
 Dec 28, 2017, 9:56:28 PM (6 years ago)
 Branches:
 ADT, aaronthesis, armeh, astexperimental, cleanupdtors, deferred_resn, demangler, enum, forallpointerdecay, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, newenv, no_list, persistentindexer, pthreademulation, qualifiedEnum, resolvnew, with_gc
 Children:
 853451b
 Parents:
 e672372
 Location:
 src
 Files:

 4 edited
Legend:
 Unmodified
 Added
 Removed

src/libcfa/stdlib
re672372 r9c47a47 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Dec 23 18:21:42 201713 // Update Count : 2 6712 // Last Modified On : Thu Dec 28 18:43:12 2017 13 // Update Count : 277 14 14 // 15 15 … … 185 185 // 186 186 187 forall( otype T  { int ?<?( T, T ); } ) 188 T * bsearch( T key, const T * arr, size_t dim ); 189 190 forall( otype T  { int ?<?( T, T ); } ) 191 unsigned int bsearch( T key, const T * arr, size_t dim ); 192 193 194 forall( otype T  { int ?<?( T, T ); } ) 195 void qsort( const T * arr, size_t dim ); 187 forall( otype E  { int ?<?( E, E ); } ) 188 E * bsearch( E key, const E * arr, size_t dim ); 189 190 forall( otype E  { int ?<?( E, E ); } ) 191 unsigned int bsearch( E key, const E * arr, size_t dim ); 192 193 forall( otype K, otype E  { int ?<?( K, K ); K getKey( E & ); } ) 194 E * bsearch( K key, const E * arr, size_t dim ); 195 196 forall( otype K, otype E  { int ?<?( K, K ); K getKey( E & ); } ) 197 unsigned int bsearch( K key, const E * arr, size_t dim ); 198 199 forall( otype E  { int ?<?( E, E ); } ) 200 void qsort( E * arr, size_t dim ); 196 201 197 202 // 
src/libcfa/stdlib.c
re672372 r9c47a47 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Dec 24 13:00:15201713 // Update Count : 3 4412 // Last Modified On : Thu Dec 28 18:43:16 2017 13 // Update Count : 378 14 14 // 15 15 … … 128 128 // 129 129 130 forall( otype T  { int ?<?( T, T); } )131 T * bsearch( T key, const T* arr, size_t dim ) {132 int comp( const void * t1, const void * t2 ) { return *( T *)t1 < *(T *)t2 ? 1 : *(T *)t2 < *(T*)t1 ? 1 : 0; }133 return ( T *)bsearch( &key, arr, dim, sizeof(T), comp );130 forall( otype E  { int ?<?( E, E ); } ) 131 E * bsearch( E key, const E * arr, size_t dim ) { 132 int comp( const void * t1, const void * t2 ) { return *(E *)t1 < *(E *)t2 ? 1 : *(E *)t2 < *(E *)t1 ? 1 : 0; } 133 return (E *)bsearch( &key, arr, dim, sizeof(E), comp ); 134 134 } // bsearch 135 135 136 forall( otype T  { int ?<?( T, T); } )137 unsigned int bsearch( T key, const T* arr, size_t dim ) {138 T* result = bsearch( key, arr, dim );139 return result ? result  arr : dim; // pointer subtraction includes sizeof( T)136 forall( otype E  { int ?<?( E, E ); } ) 137 unsigned int bsearch( E key, const E * arr, size_t dim ) { 138 E * result = bsearch( key, arr, dim ); 139 return result ? result  arr : dim; // pointer subtraction includes sizeof(E) 140 140 } // bsearch 141 141 142 forall( otype T  { int ?<?( T, T ); } ) 143 void qsort( const T * arr, size_t dim ) { 144 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? 1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } 145 qsort( arr, dim, sizeof(T), comp ); 142 forall( otype K, otype E  { int ?<?( K, K ); K getKey( E & ); } ) 143 E * bsearch( K key, const E * arr, size_t dim ) { 144 int comp( const void * t1, const void * t2 ) { return *(K *)t1 < getKey( *(E *)t2 ) ? 1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0; } 145 return (E *)bsearch( &key, arr, dim, sizeof(E), comp ); 146 } // bsearch 147 148 forall( otype K, otype E  { int ?<?( K, K ); K getKey( E & ); } ) 149 unsigned int bsearch( K key, const E * arr, size_t dim ) { 150 E * result = bsearch( key, arr, dim ); 151 return result ? result  arr : dim; // pointer subtraction includes sizeof(E) 152 } // bsearch 153 154 forall( otype E  { int ?<?( E, E ); } ) 155 void qsort( E * arr, size_t dim ) { 156 int comp( const void * t1, const void * t2 ) { return *(E *)t1 < *(E *)t2 ? 1 : *(E *)t2 < *(E *)t1 ? 1 : 0; } 157 qsort( arr, dim, sizeof(E), comp ); 146 158 } // qsort 147 159 
src/tests/.expect/searchsort.txt
re672372 r9c47a47 2 2 3 3 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 4 10 , 9, 8, 7, 6, 5, 4, 3, 2,1,5 10 , 9, 8, 7, 6, 5, 4, 3, 2,1,6 10 , 9, 8, 7, 6, 5, 4, 3, 2,1,4 10:10, 9:9, 8:8, 7:7, 6:6, 5:5, 4:4, 3:3, 2:2, 1:1, 5 10:10, 9:9, 8:8, 7:7, 6:6, 5:5, 4:4, 3:3, 2:2, 1:1, 6 10:10, 9:9, 8:8, 7:7, 6:6, 5:5, 4:4, 3:3, 2:2, 1:1, 7 7 8 8 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9 9 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 10 , 9, 8, 7, 6, 5, 4, 3, 2,1,11 10 , 9, 8, 7, 6, 5, 4, 3, 2,1,10 10:10, 9:9, 8:8, 7:7, 6:6, 5:5, 4:4, 3:3, 2:2, 1:1, 11 10:10, 9:9, 8:8, 7:7, 6:6, 5:5, 4:4, 3:3, 2:2, 1:1, 12 12 13 13 10.5, 9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5, 1.5, 14 14 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 15 10.5 , 9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5,1.5,16 10.5 , 9.5, 8.5, 7.5, 6.5, 5.5, 4.5, 3.5, 2.5,1.5,15 10.5:10.5, 9.5:9.5, 8.5:8.5, 7.5:7.5, 6.5:6.5, 5.5:5.5, 4.5:4.5, 3.5:3.5, 2.5:2.5, 1.5:1.5, 16 10.5:10.5, 9.5:9.5, 8.5:8.5, 7.5:7.5, 6.5:6.5, 5.5:5.5, 4.5:4.5, 3.5:3.5, 2.5:2.5, 1.5:1.5, 17 17 18 18 10 11, 9 10, 8 9, 7 8, 6 7, 5 6, 4 5, 3 4, 2 3, 1 2, 19 19 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10, 10 11, 20 10 11 , 9 10, 8 9, 7 8, 6 7, 5 6, 4 5, 3 4, 2 3,1 2,21 10 11 , 9 10, 8 9, 7 8, 6 7, 5 6, 4 5, 3 4, 2 3,1 2,20 10 11:10 11, 9 10:9 10, 8 9:8 9, 7 8:7 8, 6 7:6 7, 5 6:5 6, 4 5:4 5, 3 4:3 4, 2 3:2 3, 1 2:1 2, 21 10 11:10 11, 9 10:9 10, 8 9:8 9, 7 8:7 8, 6 7:6 7, 5 6:5 6, 4 5:4 5, 3 4:3 4, 2 3:2 3, 1 2:1 2, 22 22 23 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10, 10 11, 24 11:10 11, 10:9 10, 9:8 9, 8:7 8, 7:6 7, 6:5 6, 5:4 5, 4:3 4, 3:2 3, 2:1 2, 25 11:10 11, 10:9 10, 9:8 9, 8:7 8, 7:6 7, 6:5 6, 5:4 5, 4:3 4, 3:2 3, 2:1 2, 26 
src/tests/searchsort.c
re672372 r9c47a47 10 10 // Created On : Thu Feb 4 18:17:50 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Dec 7 09:14:06201713 // Update Count : 7712 // Last Modified On : Thu Dec 28 18:48:10 2017 13 // Update Count : 99 14 14 // 15 15 … … 38 38 for ( unsigned int i = 0; i < size; i += 1 ) { // C version 39 39 int key = size  i; 40 int * v = bsearch( &key, iarr, size, sizeof( iarr[0] ), comp );41 sout  *v  ", ";40 int * v = bsearch( &key, iarr, size, sizeof( iarr[0] ), comp ); 41 sout  key  ':'  *v  ", "; 42 42 } // for 43 43 sout  endl; 44 44 45 for ( unsigned int i = 0; i < size; i += 1 ) { 45 int * v = bsearch( size  i, iarr, size );46 sout  *v  ", ";46 int * v = bsearch( size  i, iarr, size ); 47 sout  size  i  ':'  *v  ", "; 47 48 } // for 48 49 sout  endl; 49 50 for ( unsigned int i = 0; i < size; i += 1 ) { 50 51 unsigned int posn = bsearch( size  i, iarr, size ); 51 sout  iarr[posn]  ", ";52 sout  size  i  ':'  iarr[posn]  ", "; 52 53 } // for 53 54 sout  endl  endl; … … 67 68 sout  endl; 68 69 for ( unsigned int i = 0; i < size; i += 1 ) { 69 int * v = bsearch( size  i, iarr, size );70 sout  *v  ", ";70 int * v = bsearch( size  i, iarr, size ); 71 sout  size  i  ':'  *v  ", "; 71 72 } // for 72 73 sout  endl; 73 74 for ( unsigned int i = 0; i < size; i += 1 ) { 74 75 unsigned int posn = bsearch( size  i, iarr, size ); 75 sout  iarr[posn]  ", ";76 sout  size  i  ':'  iarr[posn]  ", "; 76 77 } // for 77 78 } … … 90 91 sout  endl; 91 92 for ( unsigned int i = 0; i < size; i += 1 ) { 92 double * v = bsearch( size  i + 0.5, darr, size );93 sout  *v  ", ";93 double * v = bsearch( size  i + 0.5, darr, size ); 94 sout  size  i + 0.5  ':'  *v  ", "; 94 95 } // for 95 96 sout  endl; 96 97 for ( unsigned int i = 0; i < size; i += 1 ) { 97 98 unsigned int posn = bsearch( size  i + 0.5, darr, size ); 98 sout  darr[posn]  ", ";99 sout  size  i + 0.5  ':'  darr[posn]  ", "; 99 100 } // for 100 101 sout  endl  endl; … … 116 117 for ( unsigned int i = 0; i < size; i += 1 ) { 117 118 S temp = { size  i, size  i + 1 }; 118 S * v = bsearch( temp, sarr, size );119 sout  *v  ", ";119 S * v = bsearch( temp, sarr, size ); 120 sout  temp  ':'  *v  ", "; 120 121 } // for 121 122 sout  endl; … … 123 124 S temp = { size  i, size  i + 1 }; 124 125 unsigned int posn = bsearch( temp, sarr, size ); 125 sout  sarr[posn]  ", ";126 sout  temp  ':'  sarr[posn]  ", "; 126 127 } // for 127 128 sout  endl  endl; 129 { 130 unsigned int getKey( S & s ) { return s.j; } 131 for ( unsigned int i = 0; i < size; i += 1 ) { 132 sout  sarr[i]  ", "; 133 } // for 134 sout  endl; 135 for ( unsigned int i = 0; i < size; i += 1 ) { 136 S * v = bsearch( size  i + 1, sarr, size ); 137 sout  size  i + 1  ':'  *v  ", "; 138 } // for 139 sout  endl; 140 for ( unsigned int i = 0; i < size; i += 1 ) { 141 unsigned int posn = bsearch( size  i + 1, sarr, size ); 142 sout  size  i + 1  ':'  sarr[posn]  ", "; 143 } // for 144 sout  endl  endl; 145 } 128 146 } // main 129 147
Note: See TracChangeset
for help on using the changeset viewer.