Changeset 707446a for src/libcfa


Ignore:
Timestamp:
Apr 1, 2017, 7:14:11 PM (8 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:
06cf47f
Parents:
24c18ea
Message:

add alternate bsearch returning posn of key

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib

    r24c18ea r707446a  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Mar  4 22:03:54 2017
    13 // Update Count     : 102
     12// Last Modified On : Sat Apr  1 17:35:24 2017
     13// Update Count     : 104
    1414//
    1515
     
    8484forall( otype T | { int ?<?( T, T ); } )
    8585T * bsearch( T key, const T * arr, size_t dimension );
     86forall( otype T | { int ?<?( T, T ); } )
     87unsigned int bsearch( T key, const T * arr, size_t dimension );
    8688
    8789forall( otype T | { int ?<?( T, T ); } )
  • src/libcfa/stdlib.c

    r24c18ea r707446a  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Mar  4 22:02:22 2017
    13 // Update Count     : 172
     12// Last Modified On : Sat Apr  1 18:31:26 2017
     13// Update Count     : 181
    1414//
    1515
     
    228228
    229229forall( otype T | { int ?<?( T, T ); } )
     230unsigned int bsearch( T key, const T * arr, size_t dimension ) {
     231        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
     232        T *result = (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
     233        return result ? result - arr : dimension;                       // pointer subtraction includes sizeof(T)
     234} // bsearch
     235
     236forall( otype T | { int ?<?( T, T ); } )
    230237void qsort( const T * arr, size_t dimension ) {
    231238        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
Note: See TracChangeset for help on using the changeset viewer.