Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib.c

    r3ce0d440 rbbf3fda  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun  2 06:15:05 2018
    13 // Update Count     : 448
     12// Last Modified On : Wed Jan  3 08:29:29 2018
     13// Update Count     : 444
    1414//
    1515
     
    130130//---------------------------------------
    131131
    132 forall( otype E | { int ?<?( E, E ); } ) {
    133         E * bsearch( E key, const E * vals, size_t dim ) {
    134                 int cmp( const void * t1, const void * t2 ) {
    135                         return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
    136                 } // cmp
    137                 return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
    138         } // bsearch
    139 
    140         size_t bsearch( E key, const E * vals, size_t dim ) {
    141                 E * result = bsearch( key, vals, dim );
    142                 return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
    143         } // bsearch
    144 
    145         size_t bsearchl( E key, const E * vals, size_t dim ) {
    146                 size_t l = 0, m, h = dim;
    147                 while ( l < h ) {
    148                         m = (l + h) / 2;
    149                         if ( (E &)(vals[m]) < key ) {                           // cast away const
    150                                 l = m + 1;
    151                         } else {
    152                                 h = m;
    153                         } // if
    154                 } // while
    155                 return l;
    156         } // bsearchl
    157 
    158         E * bsearchl( E key, const E * vals, size_t dim ) {
    159                 size_t posn = bsearchl( key, vals, dim );
    160                 return (E *)(&vals[posn]);                                              // cast away const
    161         } // bsearchl
    162 
    163         size_t bsearchu( E key, const E * vals, size_t dim ) {
    164                 size_t l = 0, m, h = dim;
    165                 while ( l < h ) {
    166                         m = (l + h) / 2;
    167                         if ( ! ( key < (E &)(vals[m]) ) ) {                     // cast away const
    168                                 l = m + 1;
    169                         } else {
    170                                 h = m;
    171                         } // if
    172                 } // while
    173                 return l;
    174         } // bsearchu
    175 
    176         E * bsearchu( E key, const E * vals, size_t dim ) {
    177                 size_t posn = bsearchu( key, vals, dim );
    178                 return (E *)(&vals[posn]);
    179         } // bsearchu
    180 
    181 
    182         void qsort( E * vals, size_t dim ) {
    183                 int cmp( const void * t1, const void * t2 ) {
    184                         return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
    185                 } // cmp
    186                 qsort( vals, dim, sizeof(E), cmp );
    187         } // qsort
    188 } // distribution
    189 
    190 
    191 forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
    192         E * bsearch( K key, const E * vals, size_t dim ) {
    193                 int cmp( const void * t1, const void * t2 ) {
    194                         return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
    195                 } // cmp
    196                 return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
    197         } // bsearch
    198 
    199         size_t bsearch( K key, const E * vals, size_t dim ) {
    200                 E * result = bsearch( key, vals, dim );
    201                 return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
    202         } // bsearch
    203 
    204         size_t bsearchl( K key, const E * vals, size_t dim ) {
    205                 size_t l = 0, m, h = dim;
    206                 while ( l < h ) {
    207                         m = (l + h) / 2;
    208                         if ( getKey( vals[m] ) < key ) {
    209                                 l = m + 1;
    210                         } else {
    211                                 h = m;
    212                         } // if
    213                 } // while
    214                 return l;
    215         } // bsearchl
    216 
    217         E * bsearchl( K key, const E * vals, size_t dim ) {
    218                 size_t posn = bsearchl( key, vals, dim );
    219                 return (E *)(&vals[posn]);                                              // cast away const
    220         } // bsearchl
    221 
    222         size_t bsearchu( K key, const E * vals, size_t dim ) {
    223                 size_t l = 0, m, h = dim;
    224                 while ( l < h ) {
    225                         m = (l + h) / 2;
    226                         if ( ! ( key < getKey( vals[m] ) ) ) {
    227                                 l = m + 1;
    228                         } else {
    229                                 h = m;
    230                         } // if
    231                 } // while
    232                 return l;
    233         } // bsearchu
    234 
    235         E * bsearchu( K key, const E * vals, size_t dim ) {
    236                 size_t posn = bsearchu( key, vals, dim );
    237                 return (E *)(&vals[posn]);
    238         } // bsearchu
    239 } // distribution
     132forall( otype E | { int ?<?( E, E ); } )
     133E * bsearch( E key, const E * vals, size_t dim ) {
     134        int cmp( const void * t1, const void * t2 ) {
     135                return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
     136        } // cmp
     137        return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
     138} // bsearch
     139
     140forall( otype E | { int ?<?( E, E ); } )
     141size_t bsearch( E key, const E * vals, size_t dim ) {
     142        E * result = bsearch( key, vals, dim );
     143        return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
     144} // bsearch
     145
     146forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     147E * bsearch( K key, const E * vals, size_t dim ) {
     148        int cmp( const void * t1, const void * t2 ) {
     149                return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
     150        } // cmp
     151        return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
     152} // bsearch
     153
     154forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     155size_t bsearch( K key, const E * vals, size_t dim ) {
     156        E * result = bsearch( key, vals, dim );
     157        return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
     158} // bsearch
     159
     160
     161forall( otype E | { int ?<?( E, E ); } )
     162size_t bsearchl( E key, const E * vals, size_t dim ) {
     163        size_t l = 0, m, h = dim;
     164        while ( l < h ) {
     165                m = (l + h) / 2;
     166                if ( (E &)(vals[m]) < key ) {                                   // cast away const
     167                        l = m + 1;
     168                } else {
     169                        h = m;
     170                } // if
     171        } // while
     172        return l;
     173} // bsearchl
     174
     175forall( otype E | { int ?<?( E, E ); } )
     176E * bsearchl( E key, const E * vals, size_t dim ) {
     177        size_t posn = bsearchl( key, vals, dim );
     178        return (E *)(&vals[posn]);                                                      // cast away const
     179} // bsearchl
     180
     181forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     182size_t bsearchl( K key, const E * vals, size_t dim ) {
     183        size_t l = 0, m, h = dim;
     184        while ( l < h ) {
     185                m = (l + h) / 2;
     186                if ( getKey( vals[m] ) < key ) {
     187                        l = m + 1;
     188                } else {
     189                        h = m;
     190                } // if
     191        } // while
     192        return l;
     193} // bsearchl
     194
     195forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     196E * bsearchl( K key, const E * vals, size_t dim ) {
     197        size_t posn = bsearchl( key, vals, dim );
     198        return (E *)(&vals[posn]);                                                      // cast away const
     199} // bsearchl
     200
     201
     202forall( otype E | { int ?<?( E, E ); } )
     203size_t bsearchu( E key, const E * vals, size_t dim ) {
     204        size_t l = 0, m, h = dim;
     205        while ( l < h ) {
     206                m = (l + h) / 2;
     207                if ( ! ( key < (E &)(vals[m]) ) ) {                             // cast away const
     208                        l = m + 1;
     209                } else {
     210                        h = m;
     211                } // if
     212        } // while
     213        return l;
     214} // bsearchu
     215
     216forall( otype E | { int ?<?( E, E ); } )
     217E * bsearchu( E key, const E * vals, size_t dim ) {
     218        size_t posn = bsearchu( key, vals, dim );
     219        return (E *)(&vals[posn]);
     220} // bsearchu
     221
     222forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     223size_t bsearchu( K key, const E * vals, size_t dim ) {
     224        size_t l = 0, m, h = dim;
     225        while ( l < h ) {
     226                m = (l + h) / 2;
     227                if ( ! ( key < getKey( vals[m] ) ) ) {
     228                        l = m + 1;
     229                } else {
     230                        h = m;
     231                } // if
     232        } // while
     233        return l;
     234} // bsearchu
     235
     236forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     237E * bsearchu( K key, const E * vals, size_t dim ) {
     238        size_t posn = bsearchu( key, vals, dim );
     239        return (E *)(&vals[posn]);
     240} // bsearchu
     241
     242
     243forall( otype E | { int ?<?( E, E ); } )
     244void qsort( E * vals, size_t dim ) {
     245        int cmp( const void * t1, const void * t2 ) {
     246                return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
     247        } // cmp
     248        qsort( vals, dim, sizeof(E), cmp );
     249} // qsort
    240250
    241251//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.