Changeset 9c47a47 for src/libcfa


Ignore:
Timestamp:
Dec 28, 2017, 9:56:28 PM (6 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
853451b
Parents:
e672372
Message:

extend stdlib bsearch

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib

    re672372 r9c47a47  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Dec 23 18:21:42 2017
    13 // Update Count     : 267
     12// Last Modified On : Thu Dec 28 18:43:12 2017
     13// Update Count     : 277
    1414//
    1515
     
    185185//---------------------------------------
    186186
    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 );
     187forall( otype E | { int ?<?( E, E ); } )
     188E * bsearch( E key, const E * arr, size_t dim );
     189
     190forall( otype E | { int ?<?( E, E ); } )
     191unsigned int bsearch( E key, const E * arr, size_t dim );
     192
     193forall( otype K, otype E | { int ?<?( K, K ); K getKey( E & ); } )
     194E * bsearch( K key, const E * arr, size_t dim );
     195
     196forall( otype K, otype E | { int ?<?( K, K ); K getKey( E & ); } )
     197unsigned int bsearch( K key, const E * arr, size_t dim );
     198
     199forall( otype E | { int ?<?( E, E ); } )
     200void qsort( E * arr, size_t dim );
    196201
    197202//---------------------------------------
  • src/libcfa/stdlib.c

    re672372 r9c47a47  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Dec 24 13:00:15 2017
    13 // Update Count     : 344
     12// Last Modified On : Thu Dec 28 18:43:16 2017
     13// Update Count     : 378
    1414//
    1515
     
    128128//---------------------------------------
    129129
    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 );
     130forall( otype E | { int ?<?( E, E ); } )
     131E * 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 );
    134134} // bsearch
    135135
    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)
     136forall( otype E | { int ?<?( E, E ); } )
     137unsigned 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)
    140140} // bsearch
    141141
    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 );
     142forall( otype K, otype E | { int ?<?( K, K ); K getKey( E & ); } )
     143E * 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
     148forall( otype K, otype E | { int ?<?( K, K ); K getKey( E & ); } )
     149unsigned 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
     154forall( otype E | { int ?<?( E, E ); } )
     155void 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 );
    146158} // qsort
    147159
Note: See TracChangeset for help on using the changeset viewer.