Changeset 6799505 in mainline


Ignore:
Timestamp:
2005-09-16T10:42:10Z (19 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a5556b4
Parents:
0a50f59
Message:

Ok. The idea of _qsort() was not broken at all.
Revert the changes and make _qsort() call _qsort() rather than qsort() so that the idea has effect.
Add _bubblesort() to optimize number of allocations when bubblesort algorithm is invoked from _qsort().
Add doxygen-style comments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lib/sort.c

    r0a50f59 r6799505  
    3434#define EBUFSIZE        32
    3535
     36void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot);
     37void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot);
     38
     39/** Quicksort wrapper
     40 *
     41 * This is only a wrapper that takes care of memory allocations for storing
     42 * the pivot and temporary elements for generic quicksort algorithm.
     43 *
     44 * @param data Pointer to data to be sorted.
     45 * @param n Number of elements to be sorted.
     46 * @param e_size Size of one element.
     47 * @param cmp Comparator function.
     48 *
     49 */
    3650void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
    3751{
     
    5064        }
    5165
     66        _qsort(data, n, e_size, cmp, tmp, pivot);
     67       
     68        if (e_size > EBUFSIZE) {
     69                free(tmp);
     70                free(pivot);
     71        }
     72}
     73
     74/** Quicksort
     75 *
     76 * Apply generic quicksort algorithm on supplied data, using pre-allocated buffers.
     77 *
     78 * @param data Pointer to data to be sorted.
     79 * @param n Number of elements to be sorted.
     80 * @param e_size Size of one element.
     81 * @param cmp Comparator function.
     82 * @param tmp Pointer to scratch memory buffer e_size bytes long.
     83 * @param pivot Pointer to scratch memory buffer e_size bytes long.
     84 *
     85 */
     86void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot)
     87{
    5288        if (n > 4) {
    5389                int i = 0, j = n - 1;
     
    67103                }
    68104
    69                 qsort(data, j + 1, e_size, cmp);
    70                 qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp);
     105                _qsort(data, j + 1, e_size, cmp, tmp, pivot);
     106                _qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp, tmp, pivot);
    71107        } else {
    72                 bubblesort(data, n, e_size, cmp);
    73         }
    74 
    75        
    76         if (e_size > EBUFSIZE) {
    77                 free(tmp);
    78                 free(pivot);
     108                _bubblesort(data, n, e_size, cmp, tmp);
    79109        }
    80110}
    81111
    82 
     112/** Bubblesort wrapper
     113 *
     114 * This is only a wrapper that takes care of memory allocation for storing
     115 * the slot element for generic bubblesort algorithm.
     116 *
     117 * @param data Pointer to data to be sorted.
     118 * @param n Number of elements to be sorted.
     119 * @param e_size Size of one element.
     120 * @param cmp Comparator function.
     121 *
     122 */
    83123void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b))
    84124{
    85125        __u8 buf_slot[EBUFSIZE];
    86         bool done = false;
    87         void * p;
    88126        void * slot = buf_slot;
    89127       
     
    95133                }
    96134        }
     135
     136        _bubblesort(data, n, e_size, cmp, slot);
     137       
     138        if (e_size > EBUFSIZE) {
     139                free(slot);
     140        }
     141}
     142
     143/** Bubblesort
     144 *
     145 * Apply generic bubblesort algorithm on supplied data, using pre-allocated buffer.
     146 *
     147 * @param data Pointer to data to be sorted.
     148 * @param n Number of elements to be sorted.
     149 * @param e_size Size of one element.
     150 * @param cmp Comparator function.
     151 * @param slot Pointer to scratch memory buffer e_size bytes long.
     152 *
     153 */
     154void _bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot)
     155{
     156        bool done = false;
     157        void * p;
    97158
    98159        while (!done) {
     
    107168                }
    108169        }
    109        
    110         if (e_size > EBUFSIZE) {       
    111                 free(slot);
    112         }
     170
    113171}
    114172
Note: See TracChangeset for help on using the changeset viewer.